Merge ~cjwatson/launchpad:code-import-worker-require-argparse into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: efa730b092749f18248f01c777e9729ceca8fdc1
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:code-import-worker-require-argparse
Merge into: launchpad:master
Diff against target: 313 lines (+34/-178)
2 files modified
lib/lp/codehosting/codeimport/tests/test_worker.py (+8/-104)
lib/lp/codehosting/codeimport/worker.py (+26/-74)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+392998@code.launchpad.net

Commit message

Remove old-style argument parsing from code import worker

Description of the change

The scheduler now always dispatches code imports using argparse-style arguments.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/codehosting/codeimport/tests/test_worker.py b/lib/lp/codehosting/codeimport/tests/test_worker.py
2index 931c065..892ec3c 100644
3--- a/lib/lp/codehosting/codeimport/tests/test_worker.py
4+++ b/lib/lp/codehosting/codeimport/tests/test_worker.py
5@@ -991,25 +991,6 @@ class TestCVSImport(WorkerTest, CSCVSActualImportMixin):
6 self.foreign_commit_count = 2
7
8 return [
9- str(self.factory.getUniqueInteger()), 'cvs:bzr',
10- cvs_server.getRoot(), 'trunk',
11- ]
12-
13-
14-class TestCVSImportArgparse(TestCVSImport):
15- """Like `TestCVSImport`, but with argparse-style arguments."""
16-
17- def makeWorkerArguments(self, module_name, files, stacked_on_url=None):
18- """Make CVS worker arguments pointing at a real CVS repo."""
19- cvs_server = CVSServer(self.makeTemporaryDirectory())
20- cvs_server.start_server()
21- self.addCleanup(cvs_server.stop_server)
22-
23- cvs_server.makeModule('trunk', [('README', 'original\n')])
24-
25- self.foreign_commit_count = 2
26-
27- return [
28 str(self.factory.getUniqueInteger()), 'cvs', 'bzr',
29 cvs_server.getRoot(), '--cvs-module', 'trunk',
30 ]
31@@ -1047,11 +1028,11 @@ class SubversionImportHelpers:
32 svn_branch_url = svn_branch_url.replace('://localhost/', ':///')
33 self.foreign_commit_count = 2
34 arguments = [
35- str(self.factory.getUniqueInteger()), '%s:bzr' % self.rcstype,
36+ str(self.factory.getUniqueInteger()), self.rcstype, 'bzr',
37 svn_branch_url,
38 ]
39 if stacked_on_url is not None:
40- arguments.append(stacked_on_url)
41+ arguments.extend(['--stacked-on', stacked_on_url])
42 return arguments
43
44
45@@ -1213,11 +1194,11 @@ class TestGitImport(WorkerTest, TestActualImportMixin,
46 self.foreign_commit_count = 1
47
48 arguments = [
49- str(self.factory.getUniqueInteger()), 'git:bzr',
50+ str(self.factory.getUniqueInteger()), 'git', 'bzr',
51 git_server.get_url('source'),
52 ]
53 if stacked_on_url is not None:
54- arguments.append(stacked_on_url)
55+ arguments.extend(['--stacked-on', stacked_on_url])
56 return arguments
57
58 def test_non_master(self):
59@@ -1244,28 +1225,6 @@ class TestGitImport(WorkerTest, TestActualImportMixin,
60 self.assertEqual(lastrev.message, "Message for other")
61
62
63-class TestGitImportArgparse(TestGitImport):
64- """Like `TestGitImport`, but with argparse-style arguments."""
65-
66- def makeWorkerArguments(self, branch_name, files, stacked_on_url=None):
67- """Make Git worker arguments pointing at a real Git repo."""
68- repository_store = self.makeTemporaryDirectory()
69- git_server = GitServer(repository_store)
70- git_server.start_server()
71- self.addCleanup(git_server.stop_server)
72-
73- git_server.makeRepo('source', files)
74- self.foreign_commit_count = 1
75-
76- arguments = [
77- str(self.factory.getUniqueInteger()), 'git', 'bzr',
78- git_server.get_url('source'),
79- ]
80- if stacked_on_url is not None:
81- arguments.extend(['--stacked-on', stacked_on_url])
82- return arguments
83-
84-
85 class TestBzrSvnImport(WorkerTest, SubversionImportHelpers,
86 TestActualImportMixin, PullingImportWorkerTests):
87
88@@ -1327,27 +1286,6 @@ class TestBzrSvnImport(WorkerTest, SubversionImportHelpers,
89 self.assertEqual(content, cache_file.read())
90
91
92-class TestBzrSvnImportArgparse(TestBzrSvnImport):
93- """Like `TestBzrSvnImport`, but with argparse-style arguments."""
94-
95- def makeWorkerArguments(self, branch_name, files, stacked_on_url=None):
96- """Make SVN worker arguments pointing at a real SVN repo."""
97- svn_server = SubversionServer(self.makeTemporaryDirectory())
98- svn_server.start_server()
99- self.addCleanup(svn_server.stop_server)
100-
101- svn_branch_url = svn_server.makeBranch(branch_name, files)
102- svn_branch_url = svn_branch_url.replace('://localhost/', ':///')
103- self.foreign_commit_count = 2
104- arguments = [
105- str(self.factory.getUniqueInteger()), self.rcstype, 'bzr',
106- svn_branch_url,
107- ]
108- if stacked_on_url is not None:
109- arguments.extend(['--stacked-on', stacked_on_url])
110- return arguments
111-
112-
113 class TestBzrImport(WorkerTest, TestActualImportMixin,
114 PullingImportWorkerTests):
115
116@@ -1382,11 +1320,11 @@ class TestBzrImport(WorkerTest, TestActualImportMixin,
117 self.foreign_commit_count = 1
118
119 arguments = [
120- str(self.factory.getUniqueInteger()), 'bzr:bzr',
121+ str(self.factory.getUniqueInteger()), 'bzr', 'bzr',
122 bzr_server.get_url(),
123 ]
124 if stacked_on_url is not None:
125- arguments.append(stacked_on_url)
126+ arguments.extend(['--stacked-on', stacked_on_url])
127 return arguments
128
129 def test_partial(self):
130@@ -1428,36 +1366,14 @@ class TestBzrImport(WorkerTest, TestActualImportMixin,
131 branch.repository.get_revision(branch.last_revision()).committer)
132
133
134-class TestBzrImportArgparse(TestBzrImport):
135- """Like `TestBzrImport`, but with argparse-style arguments."""
136-
137- def makeWorkerArguments(self, branch_name, files, stacked_on_url=None):
138- """Make Bzr worker arguments pointing at a real Bzr repo."""
139- repository_path = self.makeTemporaryDirectory()
140- bzr_server = BzrServer(repository_path)
141- bzr_server.start_server()
142- self.addCleanup(bzr_server.stop_server)
143-
144- bzr_server.makeRepo(files)
145- self.foreign_commit_count = 1
146-
147- arguments = [
148- str(self.factory.getUniqueInteger()), 'bzr', 'bzr',
149- bzr_server.get_url(),
150- ]
151- if stacked_on_url is not None:
152- arguments.extend(['--stacked-on', stacked_on_url])
153- return arguments
154-
155-
156 class TestGitToGitImportWorker(TestCase):
157
158 def makeWorkerArguments(self):
159 """Make Git worker arguments, pointing at a fake URL for now."""
160 return [
161- 'git-unique-name', 'git:git',
162+ 'git-unique-name', 'git', 'git',
163 self.factory.getUniqueURL(scheme='git'),
164- Macaroon().serialize(),
165+ '--macaroon', Macaroon().serialize(),
166 ]
167
168 def test_throttleProgress(self):
169@@ -1505,18 +1421,6 @@ class TestGitToGitImportWorker(TestCase):
170 ContainsAll([u"%d ...\r" % i for i in (9, 19, 29, 39, 49)]))
171
172
173-class TestGitToGitImportWorkerArgparse(TestCase):
174- """Like `TestGitToGitImportWorker`, but with argparse-style arguments."""
175-
176- def makeWorkerArguments(self):
177- """Make Git worker arguments, pointing at a fake URL for now."""
178- return [
179- 'git-unique-name', 'git', 'git',
180- self.factory.getUniqueURL(scheme='git'),
181- '--macaroon', Macaroon().serialize(),
182- ]
183-
184-
185 class CodeImportBranchOpenPolicyTests(TestCase):
186
187 def setUp(self):
188diff --git a/lib/lp/codehosting/codeimport/worker.py b/lib/lp/codehosting/codeimport/worker.py
189index 9cd5258..6bf5d8d 100644
190--- a/lib/lp/codehosting/codeimport/worker.py
191+++ b/lib/lp/codehosting/codeimport/worker.py
192@@ -284,17 +284,6 @@ def get_default_bazaar_branch_store():
193 get_transport_from_url(config.codeimport.bazaar_branch_store))
194
195
196-class ArgumentParserError(Exception):
197- """An argument parsing error."""
198-
199-
200-class TolerantArgumentParser(ArgumentParser):
201- """An `ArgumentParser` that raises exceptions on errors."""
202-
203- def error(self, message):
204- raise ArgumentParserError(message)
205-
206-
207 class CodeImportSourceDetails:
208 """The information needed to process an import.
209
210@@ -336,7 +325,7 @@ class CodeImportSourceDetails:
211 def fromArguments(cls, arguments):
212 """Convert command line-style arguments to an instance."""
213 # Keep this in sync with CodeImportJob.makeWorkerArguments.
214- parser = TolerantArgumentParser(description='Code import worker.')
215+ parser = ArgumentParser(description='Code import worker.')
216 parser.add_argument(
217 'target_id', help='Target branch ID or repository unique name')
218 parser.add_argument(
219@@ -357,69 +346,32 @@ class CodeImportSourceDetails:
220 help=(
221 'Macaroon authorising push (only valid for target_rcstype '
222 'git)'))
223- arguments = list(arguments)
224- try:
225- args = parser.parse_args(arguments)
226- target_id = args.target_id
227- rcstype = args.rcstype
228- target_rcstype = args.target_rcstype
229- url = args.url if rcstype in ('bzr', 'bzr-svn', 'git') else None
230- if rcstype == 'cvs':
231- if args.cvs_module is None:
232- parser.error('rcstype cvs requires --cvs-module')
233- cvs_root = args.url
234- cvs_module = args.cvs_module
235- else:
236- cvs_root = None
237- cvs_module = None
238- if target_rcstype == 'bzr':
239- try:
240- target_id = int(target_id)
241- except ValueError:
242- parser.error(
243- 'rcstype bzr requires target_id to be an integer')
244- stacked_on_url = args.stacked_on
245- macaroon = None
246- elif target_rcstype == 'git':
247- if args.macaroon is None:
248- parser.error('target_rcstype git requires --macaroon')
249- stacked_on_url = None
250- macaroon = args.macaroon
251- except ArgumentParserError:
252- # XXX cjwatson 2020-10-05: Remove this old-style argument
253- # handling once the scheduler always passes something argparse
254- # can handle.
255- target_id = arguments.pop(0)
256- rcstype = arguments.pop(0)
257- if ':' not in rcstype:
258- raise AssertionError(
259- "'%s' does not contain both source and target types." %
260- rcstype)
261- rcstype, target_rcstype = rcstype.split(':', 1)
262- if rcstype in ['bzr-svn', 'git', 'bzr']:
263- url = arguments.pop(0)
264- if target_rcstype == 'bzr':
265- try:
266- stacked_on_url = arguments.pop(0)
267- except IndexError:
268- stacked_on_url = None
269- else:
270- stacked_on_url = None
271- cvs_root = cvs_module = None
272- elif rcstype == 'cvs':
273- url = None
274- stacked_on_url = None
275- [cvs_root, cvs_module] = arguments
276- else:
277- raise AssertionError("Unknown rcstype %r." % rcstype)
278- if target_rcstype == 'bzr':
279+ args = parser.parse_args(arguments)
280+ target_id = args.target_id
281+ rcstype = args.rcstype
282+ target_rcstype = args.target_rcstype
283+ url = args.url if rcstype in ('bzr', 'bzr-svn', 'git') else None
284+ if rcstype == 'cvs':
285+ if args.cvs_module is None:
286+ parser.error('rcstype cvs requires --cvs-module')
287+ cvs_root = args.url
288+ cvs_module = args.cvs_module
289+ else:
290+ cvs_root = None
291+ cvs_module = None
292+ if target_rcstype == 'bzr':
293+ try:
294 target_id = int(target_id)
295- macaroon = None
296- elif target_rcstype == 'git':
297- macaroon = Macaroon.deserialize(arguments.pop(0))
298- else:
299- raise AssertionError(
300- "Unknown target_rcstype %r." % target_rcstype)
301+ except ValueError:
302+ parser.error(
303+ 'rcstype bzr requires target_id to be an integer')
304+ stacked_on_url = args.stacked_on
305+ macaroon = None
306+ elif target_rcstype == 'git':
307+ if args.macaroon is None:
308+ parser.error('target_rcstype git requires --macaroon')
309+ stacked_on_url = None
310+ macaroon = args.macaroon
311 return cls(
312 target_id, rcstype, target_rcstype, url, cvs_root, cvs_module,
313 stacked_on_url, macaroon)

Subscribers

People subscribed via source and target branches

to status/vote changes: