Merge ~cjwatson/launchpad:code-import-worker-require-argparse into launchpad:master
- Git
- lp:~cjwatson/launchpad
- code-import-worker-require-argparse
- Merge into 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) |
Related bugs: |
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.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/lib/lp/codehosting/codeimport/tests/test_worker.py b/lib/lp/codehosting/codeimport/tests/test_worker.py | |||
2 | index 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 | 991 | self.foreign_commit_count = 2 | 991 | self.foreign_commit_count = 2 |
7 | 992 | 992 | ||
8 | 993 | return [ | 993 | return [ |
9 | 994 | str(self.factory.getUniqueInteger()), 'cvs:bzr', | ||
10 | 995 | cvs_server.getRoot(), 'trunk', | ||
11 | 996 | ] | ||
12 | 997 | |||
13 | 998 | |||
14 | 999 | class TestCVSImportArgparse(TestCVSImport): | ||
15 | 1000 | """Like `TestCVSImport`, but with argparse-style arguments.""" | ||
16 | 1001 | |||
17 | 1002 | def makeWorkerArguments(self, module_name, files, stacked_on_url=None): | ||
18 | 1003 | """Make CVS worker arguments pointing at a real CVS repo.""" | ||
19 | 1004 | cvs_server = CVSServer(self.makeTemporaryDirectory()) | ||
20 | 1005 | cvs_server.start_server() | ||
21 | 1006 | self.addCleanup(cvs_server.stop_server) | ||
22 | 1007 | |||
23 | 1008 | cvs_server.makeModule('trunk', [('README', 'original\n')]) | ||
24 | 1009 | |||
25 | 1010 | self.foreign_commit_count = 2 | ||
26 | 1011 | |||
27 | 1012 | return [ | ||
28 | 1013 | str(self.factory.getUniqueInteger()), 'cvs', 'bzr', | 994 | str(self.factory.getUniqueInteger()), 'cvs', 'bzr', |
29 | 1014 | cvs_server.getRoot(), '--cvs-module', 'trunk', | 995 | cvs_server.getRoot(), '--cvs-module', 'trunk', |
30 | 1015 | ] | 996 | ] |
31 | @@ -1047,11 +1028,11 @@ class SubversionImportHelpers: | |||
32 | 1047 | svn_branch_url = svn_branch_url.replace('://localhost/', ':///') | 1028 | svn_branch_url = svn_branch_url.replace('://localhost/', ':///') |
33 | 1048 | self.foreign_commit_count = 2 | 1029 | self.foreign_commit_count = 2 |
34 | 1049 | arguments = [ | 1030 | arguments = [ |
36 | 1050 | str(self.factory.getUniqueInteger()), '%s:bzr' % self.rcstype, | 1031 | str(self.factory.getUniqueInteger()), self.rcstype, 'bzr', |
37 | 1051 | svn_branch_url, | 1032 | svn_branch_url, |
38 | 1052 | ] | 1033 | ] |
39 | 1053 | if stacked_on_url is not None: | 1034 | if stacked_on_url is not None: |
41 | 1054 | arguments.append(stacked_on_url) | 1035 | arguments.extend(['--stacked-on', stacked_on_url]) |
42 | 1055 | return arguments | 1036 | return arguments |
43 | 1056 | 1037 | ||
44 | 1057 | 1038 | ||
45 | @@ -1213,11 +1194,11 @@ class TestGitImport(WorkerTest, TestActualImportMixin, | |||
46 | 1213 | self.foreign_commit_count = 1 | 1194 | self.foreign_commit_count = 1 |
47 | 1214 | 1195 | ||
48 | 1215 | arguments = [ | 1196 | arguments = [ |
50 | 1216 | str(self.factory.getUniqueInteger()), 'git:bzr', | 1197 | str(self.factory.getUniqueInteger()), 'git', 'bzr', |
51 | 1217 | git_server.get_url('source'), | 1198 | git_server.get_url('source'), |
52 | 1218 | ] | 1199 | ] |
53 | 1219 | if stacked_on_url is not None: | 1200 | if stacked_on_url is not None: |
55 | 1220 | arguments.append(stacked_on_url) | 1201 | arguments.extend(['--stacked-on', stacked_on_url]) |
56 | 1221 | return arguments | 1202 | return arguments |
57 | 1222 | 1203 | ||
58 | 1223 | def test_non_master(self): | 1204 | def test_non_master(self): |
59 | @@ -1244,28 +1225,6 @@ class TestGitImport(WorkerTest, TestActualImportMixin, | |||
60 | 1244 | self.assertEqual(lastrev.message, "Message for other") | 1225 | self.assertEqual(lastrev.message, "Message for other") |
61 | 1245 | 1226 | ||
62 | 1246 | 1227 | ||
63 | 1247 | class TestGitImportArgparse(TestGitImport): | ||
64 | 1248 | """Like `TestGitImport`, but with argparse-style arguments.""" | ||
65 | 1249 | |||
66 | 1250 | def makeWorkerArguments(self, branch_name, files, stacked_on_url=None): | ||
67 | 1251 | """Make Git worker arguments pointing at a real Git repo.""" | ||
68 | 1252 | repository_store = self.makeTemporaryDirectory() | ||
69 | 1253 | git_server = GitServer(repository_store) | ||
70 | 1254 | git_server.start_server() | ||
71 | 1255 | self.addCleanup(git_server.stop_server) | ||
72 | 1256 | |||
73 | 1257 | git_server.makeRepo('source', files) | ||
74 | 1258 | self.foreign_commit_count = 1 | ||
75 | 1259 | |||
76 | 1260 | arguments = [ | ||
77 | 1261 | str(self.factory.getUniqueInteger()), 'git', 'bzr', | ||
78 | 1262 | git_server.get_url('source'), | ||
79 | 1263 | ] | ||
80 | 1264 | if stacked_on_url is not None: | ||
81 | 1265 | arguments.extend(['--stacked-on', stacked_on_url]) | ||
82 | 1266 | return arguments | ||
83 | 1267 | |||
84 | 1268 | |||
85 | 1269 | class TestBzrSvnImport(WorkerTest, SubversionImportHelpers, | 1228 | class TestBzrSvnImport(WorkerTest, SubversionImportHelpers, |
86 | 1270 | TestActualImportMixin, PullingImportWorkerTests): | 1229 | TestActualImportMixin, PullingImportWorkerTests): |
87 | 1271 | 1230 | ||
88 | @@ -1327,27 +1286,6 @@ class TestBzrSvnImport(WorkerTest, SubversionImportHelpers, | |||
89 | 1327 | self.assertEqual(content, cache_file.read()) | 1286 | self.assertEqual(content, cache_file.read()) |
90 | 1328 | 1287 | ||
91 | 1329 | 1288 | ||
92 | 1330 | class TestBzrSvnImportArgparse(TestBzrSvnImport): | ||
93 | 1331 | """Like `TestBzrSvnImport`, but with argparse-style arguments.""" | ||
94 | 1332 | |||
95 | 1333 | def makeWorkerArguments(self, branch_name, files, stacked_on_url=None): | ||
96 | 1334 | """Make SVN worker arguments pointing at a real SVN repo.""" | ||
97 | 1335 | svn_server = SubversionServer(self.makeTemporaryDirectory()) | ||
98 | 1336 | svn_server.start_server() | ||
99 | 1337 | self.addCleanup(svn_server.stop_server) | ||
100 | 1338 | |||
101 | 1339 | svn_branch_url = svn_server.makeBranch(branch_name, files) | ||
102 | 1340 | svn_branch_url = svn_branch_url.replace('://localhost/', ':///') | ||
103 | 1341 | self.foreign_commit_count = 2 | ||
104 | 1342 | arguments = [ | ||
105 | 1343 | str(self.factory.getUniqueInteger()), self.rcstype, 'bzr', | ||
106 | 1344 | svn_branch_url, | ||
107 | 1345 | ] | ||
108 | 1346 | if stacked_on_url is not None: | ||
109 | 1347 | arguments.extend(['--stacked-on', stacked_on_url]) | ||
110 | 1348 | return arguments | ||
111 | 1349 | |||
112 | 1350 | |||
113 | 1351 | class TestBzrImport(WorkerTest, TestActualImportMixin, | 1289 | class TestBzrImport(WorkerTest, TestActualImportMixin, |
114 | 1352 | PullingImportWorkerTests): | 1290 | PullingImportWorkerTests): |
115 | 1353 | 1291 | ||
116 | @@ -1382,11 +1320,11 @@ class TestBzrImport(WorkerTest, TestActualImportMixin, | |||
117 | 1382 | self.foreign_commit_count = 1 | 1320 | self.foreign_commit_count = 1 |
118 | 1383 | 1321 | ||
119 | 1384 | arguments = [ | 1322 | arguments = [ |
121 | 1385 | str(self.factory.getUniqueInteger()), 'bzr:bzr', | 1323 | str(self.factory.getUniqueInteger()), 'bzr', 'bzr', |
122 | 1386 | bzr_server.get_url(), | 1324 | bzr_server.get_url(), |
123 | 1387 | ] | 1325 | ] |
124 | 1388 | if stacked_on_url is not None: | 1326 | if stacked_on_url is not None: |
126 | 1389 | arguments.append(stacked_on_url) | 1327 | arguments.extend(['--stacked-on', stacked_on_url]) |
127 | 1390 | return arguments | 1328 | return arguments |
128 | 1391 | 1329 | ||
129 | 1392 | def test_partial(self): | 1330 | def test_partial(self): |
130 | @@ -1428,36 +1366,14 @@ class TestBzrImport(WorkerTest, TestActualImportMixin, | |||
131 | 1428 | branch.repository.get_revision(branch.last_revision()).committer) | 1366 | branch.repository.get_revision(branch.last_revision()).committer) |
132 | 1429 | 1367 | ||
133 | 1430 | 1368 | ||
134 | 1431 | class TestBzrImportArgparse(TestBzrImport): | ||
135 | 1432 | """Like `TestBzrImport`, but with argparse-style arguments.""" | ||
136 | 1433 | |||
137 | 1434 | def makeWorkerArguments(self, branch_name, files, stacked_on_url=None): | ||
138 | 1435 | """Make Bzr worker arguments pointing at a real Bzr repo.""" | ||
139 | 1436 | repository_path = self.makeTemporaryDirectory() | ||
140 | 1437 | bzr_server = BzrServer(repository_path) | ||
141 | 1438 | bzr_server.start_server() | ||
142 | 1439 | self.addCleanup(bzr_server.stop_server) | ||
143 | 1440 | |||
144 | 1441 | bzr_server.makeRepo(files) | ||
145 | 1442 | self.foreign_commit_count = 1 | ||
146 | 1443 | |||
147 | 1444 | arguments = [ | ||
148 | 1445 | str(self.factory.getUniqueInteger()), 'bzr', 'bzr', | ||
149 | 1446 | bzr_server.get_url(), | ||
150 | 1447 | ] | ||
151 | 1448 | if stacked_on_url is not None: | ||
152 | 1449 | arguments.extend(['--stacked-on', stacked_on_url]) | ||
153 | 1450 | return arguments | ||
154 | 1451 | |||
155 | 1452 | |||
156 | 1453 | class TestGitToGitImportWorker(TestCase): | 1369 | class TestGitToGitImportWorker(TestCase): |
157 | 1454 | 1370 | ||
158 | 1455 | def makeWorkerArguments(self): | 1371 | def makeWorkerArguments(self): |
159 | 1456 | """Make Git worker arguments, pointing at a fake URL for now.""" | 1372 | """Make Git worker arguments, pointing at a fake URL for now.""" |
160 | 1457 | return [ | 1373 | return [ |
162 | 1458 | 'git-unique-name', 'git:git', | 1374 | 'git-unique-name', 'git', 'git', |
163 | 1459 | self.factory.getUniqueURL(scheme='git'), | 1375 | self.factory.getUniqueURL(scheme='git'), |
165 | 1460 | Macaroon().serialize(), | 1376 | '--macaroon', Macaroon().serialize(), |
166 | 1461 | ] | 1377 | ] |
167 | 1462 | 1378 | ||
168 | 1463 | def test_throttleProgress(self): | 1379 | def test_throttleProgress(self): |
169 | @@ -1505,18 +1421,6 @@ class TestGitToGitImportWorker(TestCase): | |||
170 | 1505 | ContainsAll([u"%d ...\r" % i for i in (9, 19, 29, 39, 49)])) | 1421 | ContainsAll([u"%d ...\r" % i for i in (9, 19, 29, 39, 49)])) |
171 | 1506 | 1422 | ||
172 | 1507 | 1423 | ||
173 | 1508 | class TestGitToGitImportWorkerArgparse(TestCase): | ||
174 | 1509 | """Like `TestGitToGitImportWorker`, but with argparse-style arguments.""" | ||
175 | 1510 | |||
176 | 1511 | def makeWorkerArguments(self): | ||
177 | 1512 | """Make Git worker arguments, pointing at a fake URL for now.""" | ||
178 | 1513 | return [ | ||
179 | 1514 | 'git-unique-name', 'git', 'git', | ||
180 | 1515 | self.factory.getUniqueURL(scheme='git'), | ||
181 | 1516 | '--macaroon', Macaroon().serialize(), | ||
182 | 1517 | ] | ||
183 | 1518 | |||
184 | 1519 | |||
185 | 1520 | class CodeImportBranchOpenPolicyTests(TestCase): | 1424 | class CodeImportBranchOpenPolicyTests(TestCase): |
186 | 1521 | 1425 | ||
187 | 1522 | def setUp(self): | 1426 | def setUp(self): |
188 | diff --git a/lib/lp/codehosting/codeimport/worker.py b/lib/lp/codehosting/codeimport/worker.py | |||
189 | index 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 | 284 | get_transport_from_url(config.codeimport.bazaar_branch_store)) | 284 | get_transport_from_url(config.codeimport.bazaar_branch_store)) |
194 | 285 | 285 | ||
195 | 286 | 286 | ||
196 | 287 | class ArgumentParserError(Exception): | ||
197 | 288 | """An argument parsing error.""" | ||
198 | 289 | |||
199 | 290 | |||
200 | 291 | class TolerantArgumentParser(ArgumentParser): | ||
201 | 292 | """An `ArgumentParser` that raises exceptions on errors.""" | ||
202 | 293 | |||
203 | 294 | def error(self, message): | ||
204 | 295 | raise ArgumentParserError(message) | ||
205 | 296 | |||
206 | 297 | |||
207 | 298 | class CodeImportSourceDetails: | 287 | class CodeImportSourceDetails: |
208 | 299 | """The information needed to process an import. | 288 | """The information needed to process an import. |
209 | 300 | 289 | ||
210 | @@ -336,7 +325,7 @@ class CodeImportSourceDetails: | |||
211 | 336 | def fromArguments(cls, arguments): | 325 | def fromArguments(cls, arguments): |
212 | 337 | """Convert command line-style arguments to an instance.""" | 326 | """Convert command line-style arguments to an instance.""" |
213 | 338 | # Keep this in sync with CodeImportJob.makeWorkerArguments. | 327 | # Keep this in sync with CodeImportJob.makeWorkerArguments. |
215 | 339 | parser = TolerantArgumentParser(description='Code import worker.') | 328 | parser = ArgumentParser(description='Code import worker.') |
216 | 340 | parser.add_argument( | 329 | parser.add_argument( |
217 | 341 | 'target_id', help='Target branch ID or repository unique name') | 330 | 'target_id', help='Target branch ID or repository unique name') |
218 | 342 | parser.add_argument( | 331 | parser.add_argument( |
219 | @@ -357,69 +346,32 @@ class CodeImportSourceDetails: | |||
220 | 357 | help=( | 346 | help=( |
221 | 358 | 'Macaroon authorising push (only valid for target_rcstype ' | 347 | 'Macaroon authorising push (only valid for target_rcstype ' |
222 | 359 | 'git)')) | 348 | 'git)')) |
279 | 360 | arguments = list(arguments) | 349 | args = parser.parse_args(arguments) |
280 | 361 | try: | 350 | target_id = args.target_id |
281 | 362 | args = parser.parse_args(arguments) | 351 | rcstype = args.rcstype |
282 | 363 | target_id = args.target_id | 352 | target_rcstype = args.target_rcstype |
283 | 364 | rcstype = args.rcstype | 353 | url = args.url if rcstype in ('bzr', 'bzr-svn', 'git') else None |
284 | 365 | target_rcstype = args.target_rcstype | 354 | if rcstype == 'cvs': |
285 | 366 | url = args.url if rcstype in ('bzr', 'bzr-svn', 'git') else None | 355 | if args.cvs_module is None: |
286 | 367 | if rcstype == 'cvs': | 356 | parser.error('rcstype cvs requires --cvs-module') |
287 | 368 | if args.cvs_module is None: | 357 | cvs_root = args.url |
288 | 369 | parser.error('rcstype cvs requires --cvs-module') | 358 | cvs_module = args.cvs_module |
289 | 370 | cvs_root = args.url | 359 | else: |
290 | 371 | cvs_module = args.cvs_module | 360 | cvs_root = None |
291 | 372 | else: | 361 | cvs_module = None |
292 | 373 | cvs_root = None | 362 | if target_rcstype == 'bzr': |
293 | 374 | cvs_module = None | 363 | try: |
238 | 375 | if target_rcstype == 'bzr': | ||
239 | 376 | try: | ||
240 | 377 | target_id = int(target_id) | ||
241 | 378 | except ValueError: | ||
242 | 379 | parser.error( | ||
243 | 380 | 'rcstype bzr requires target_id to be an integer') | ||
244 | 381 | stacked_on_url = args.stacked_on | ||
245 | 382 | macaroon = None | ||
246 | 383 | elif target_rcstype == 'git': | ||
247 | 384 | if args.macaroon is None: | ||
248 | 385 | parser.error('target_rcstype git requires --macaroon') | ||
249 | 386 | stacked_on_url = None | ||
250 | 387 | macaroon = args.macaroon | ||
251 | 388 | except ArgumentParserError: | ||
252 | 389 | # XXX cjwatson 2020-10-05: Remove this old-style argument | ||
253 | 390 | # handling once the scheduler always passes something argparse | ||
254 | 391 | # can handle. | ||
255 | 392 | target_id = arguments.pop(0) | ||
256 | 393 | rcstype = arguments.pop(0) | ||
257 | 394 | if ':' not in rcstype: | ||
258 | 395 | raise AssertionError( | ||
259 | 396 | "'%s' does not contain both source and target types." % | ||
260 | 397 | rcstype) | ||
261 | 398 | rcstype, target_rcstype = rcstype.split(':', 1) | ||
262 | 399 | if rcstype in ['bzr-svn', 'git', 'bzr']: | ||
263 | 400 | url = arguments.pop(0) | ||
264 | 401 | if target_rcstype == 'bzr': | ||
265 | 402 | try: | ||
266 | 403 | stacked_on_url = arguments.pop(0) | ||
267 | 404 | except IndexError: | ||
268 | 405 | stacked_on_url = None | ||
269 | 406 | else: | ||
270 | 407 | stacked_on_url = None | ||
271 | 408 | cvs_root = cvs_module = None | ||
272 | 409 | elif rcstype == 'cvs': | ||
273 | 410 | url = None | ||
274 | 411 | stacked_on_url = None | ||
275 | 412 | [cvs_root, cvs_module] = arguments | ||
276 | 413 | else: | ||
277 | 414 | raise AssertionError("Unknown rcstype %r." % rcstype) | ||
278 | 415 | if target_rcstype == 'bzr': | ||
294 | 416 | target_id = int(target_id) | 364 | target_id = int(target_id) |
301 | 417 | macaroon = None | 365 | except ValueError: |
302 | 418 | elif target_rcstype == 'git': | 366 | parser.error( |
303 | 419 | macaroon = Macaroon.deserialize(arguments.pop(0)) | 367 | 'rcstype bzr requires target_id to be an integer') |
304 | 420 | else: | 368 | stacked_on_url = args.stacked_on |
305 | 421 | raise AssertionError( | 369 | macaroon = None |
306 | 422 | "Unknown target_rcstype %r." % target_rcstype) | 370 | elif target_rcstype == 'git': |
307 | 371 | if args.macaroon is None: | ||
308 | 372 | parser.error('target_rcstype git requires --macaroon') | ||
309 | 373 | stacked_on_url = None | ||
310 | 374 | macaroon = args.macaroon | ||
311 | 423 | return cls( | 375 | return cls( |
312 | 424 | target_id, rcstype, target_rcstype, url, cvs_root, cvs_module, | 376 | target_id, rcstype, target_rcstype, url, cvs_root, cvs_module, |
313 | 425 | stacked_on_url, macaroon) | 377 | stacked_on_url, macaroon) |
LGTM