Merge lp://staging/~spiv/bzr-builder/refactor into lp://staging/~james-w/bzr-builder/trunk-old
- refactor
- Merge into trunk-old
Status: | Merged |
---|---|
Merge reported by: | James Westby |
Merged at revision: | not available |
Proposed branch: | lp://staging/~spiv/bzr-builder/refactor |
Merge into: | lp://staging/~james-w/bzr-builder/trunk-old |
Prerequisite: | lp://staging/~lifeless/bzr-builder/bug-469874 |
Diff against target: |
274 lines (+82/-49) 2 files modified
recipe.py (+67/-36) tests/test_recipe.py (+15/-13) |
To merge this branch: | bzr merge lp://staging/~spiv/bzr-builder/refactor |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
James Westby | Approve | ||
Review via email: mp+14978@code.staging.launchpad.net |
Commit message
Description of the change
Andrew Bennetts (spiv) wrote : | # |
James Westby (james-w) wrote : | # |
On Wed Nov 18 03:45:21 UTC 2009 Andrew Bennetts wrote:
> Andrew Bennetts has proposed merging lp:~spiv/bzr-builder/refactor into
> lp:bzr-builder with lp:~lifeless/bzr-builder/bug-469874 as a prerequisite.
>
> Requested reviews:
> James Westby (james-w)
>
>
> This basically a replaces a list of 2-tuples where (*, None), (None, *) and
> (None, None) all mean different things, and replaces them with objects. This
> also replaces if/elif/... blocks about those cases with good polymorphism.
Thanks, code elegance is always appreciated.
> The names I gave to the objects and methods can probably be improved a fair
> bit, and I'm happy to do that if you have any suggestions about what these
> items should be called.
I think the names are good.
My only concern is with .as_tuple(), as the tuple isn't something that has
a natural definition based on the object, and it may make no sense for
some instructions. However, as these were issues with the original code
and doing this means less test changes are needed then I'm happy to
merge it, thanks.
Thanks,
James
James Westby (james-w) : | # |
James Westby (james-w) wrote : | # |
Landing this is blocked on reviewing/landing the prerequisite branch.
Thanks,
James
Preview Diff
1 | === modified file 'recipe.py' | |||
2 | --- recipe.py 2009-11-18 03:45:21 +0000 | |||
3 | +++ recipe.py 2009-11-18 03:45:21 +0000 | |||
4 | @@ -247,11 +247,12 @@ | |||
5 | 247 | changed_revision_id = br_from.last_revision() | 247 | changed_revision_id = br_from.last_revision() |
6 | 248 | if revision_id != changed_revision_id: | 248 | if revision_id != changed_revision_id: |
7 | 249 | changed = True | 249 | changed = True |
10 | 250 | for index, (child_branch, nest_location) in \ | 250 | for index, instruction in enumerate(new_branch.child_branches): |
11 | 251 | enumerate(new_branch.child_branches): | 251 | child_branch = instruction.recipe_branch |
12 | 252 | nest_location = instruction.nest_path | ||
13 | 252 | if_changed_child = None | 253 | if_changed_child = None |
14 | 253 | if if_changed_from is not None: | 254 | if if_changed_from is not None: |
16 | 254 | if_changed_child = if_changed_from.child_branches[index][0] | 255 | if_changed_child = if_changed_from.child_branches[index].recipe_branch |
17 | 255 | if child_branch is not None: | 256 | if child_branch is not None: |
18 | 256 | child_changed = _resolve_revisions_recurse(child_branch, | 257 | child_changed = _resolve_revisions_recurse(child_branch, |
19 | 257 | substitute_revno, | 258 | substitute_revno, |
20 | @@ -327,22 +328,8 @@ | |||
21 | 327 | try: | 328 | try: |
22 | 328 | tree_to, br_to = update_branch(base_branch, tree_to, br_to, | 329 | tree_to, br_to = update_branch(base_branch, tree_to, br_to, |
23 | 329 | to_transport) | 330 | to_transport) |
40 | 330 | for child_branch, nest_location in base_branch.child_branches: | 331 | for instruction in base_branch.child_branches: |
41 | 331 | if child_branch is None: | 332 | instruction.apply(target_path, tree_to, br_to) |
26 | 332 | # it's a command | ||
27 | 333 | proc = subprocess.Popen(nest_location, cwd=target_path, | ||
28 | 334 | preexec_fn=subprocess_setup, shell=True, | ||
29 | 335 | stdin=subprocess.PIPE) | ||
30 | 336 | proc.communicate() | ||
31 | 337 | if proc.returncode != 0: | ||
32 | 338 | raise CommandFailedError(nest_location) | ||
33 | 339 | elif nest_location is not None: | ||
34 | 340 | # FIXME: pass possible_transports around | ||
35 | 341 | build_tree(child_branch, | ||
36 | 342 | target_path=os.path.join(target_path, | ||
37 | 343 | nest_location)) | ||
38 | 344 | else: | ||
39 | 345 | merge_branch(child_branch, tree_to, br_to) | ||
42 | 346 | finally: | 333 | finally: |
43 | 347 | # Is this ok if tree_to is created by pull_or_branch? | 334 | # Is this ok if tree_to is created by pull_or_branch? |
44 | 348 | if br_to is not None: | 335 | if br_to is not None: |
45 | @@ -354,7 +341,9 @@ | |||
46 | 354 | 341 | ||
47 | 355 | def _add_child_branches_to_manifest(child_branches, indent_level): | 342 | def _add_child_branches_to_manifest(child_branches, indent_level): |
48 | 356 | manifest = "" | 343 | manifest = "" |
50 | 357 | for child_branch, nest_location in child_branches: | 344 | for instruction in child_branches: |
51 | 345 | child_branch = instruction.recipe_branch | ||
52 | 346 | nest_location = instruction.nest_path | ||
53 | 358 | if child_branch is None: | 347 | if child_branch is None: |
54 | 359 | manifest += "%s%s %s\n" % (" " * indent_level, RUN_INSTRUCTION, | 348 | manifest += "%s%s %s\n" % (" " * indent_level, RUN_INSTRUCTION, |
55 | 360 | nest_location) | 349 | nest_location) |
56 | @@ -390,6 +379,50 @@ | |||
57 | 390 | return manifest | 379 | return manifest |
58 | 391 | 380 | ||
59 | 392 | 381 | ||
60 | 382 | class ChildBranch(object): | ||
61 | 383 | """A child branch in a recipe. | ||
62 | 384 | |||
63 | 385 | If the nest path is not None it is the path relative to the recipe branch | ||
64 | 386 | where the child branch should be placed. If it is None then the child | ||
65 | 387 | branch should be merged instead of nested. | ||
66 | 388 | """ | ||
67 | 389 | |||
68 | 390 | def __init__(self, recipe_branch, nest_path=None): | ||
69 | 391 | self.recipe_branch = recipe_branch | ||
70 | 392 | self.nest_path = nest_path | ||
71 | 393 | |||
72 | 394 | def apply(self, target_path, tree_to, br_to): | ||
73 | 395 | raise NotImplementedError(self.apply) | ||
74 | 396 | |||
75 | 397 | def as_tuple(self): | ||
76 | 398 | return (self.recipe_branch, self.nest_path) | ||
77 | 399 | |||
78 | 400 | |||
79 | 401 | class CommandInstruction(ChildBranch): | ||
80 | 402 | |||
81 | 403 | def apply(self, target_path, tree_to, br_to): | ||
82 | 404 | # it's a command | ||
83 | 405 | proc = subprocess.Popen(self.nest_path, cwd=target_path, | ||
84 | 406 | preexec_fn=subprocess_setup, shell=True, stdin=subprocess.PIPE) | ||
85 | 407 | proc.communicate() | ||
86 | 408 | if proc.returncode != 0: | ||
87 | 409 | raise CommandFailedError(self.nest_path) | ||
88 | 410 | |||
89 | 411 | |||
90 | 412 | class MergeInstruction(ChildBranch): | ||
91 | 413 | |||
92 | 414 | def apply(self, target_path, tree_to, br_to): | ||
93 | 415 | merge_branch(self.recipe_branch, tree_to, br_to) | ||
94 | 416 | |||
95 | 417 | |||
96 | 418 | class NestInstruction(ChildBranch): | ||
97 | 419 | |||
98 | 420 | def apply(self, target_path, tree_to, br_to): | ||
99 | 421 | # FIXME: pass possible_transports around | ||
100 | 422 | build_tree(self.recipe_branch, | ||
101 | 423 | target_path=os.path.join(target_path, self.nest_path)) | ||
102 | 424 | |||
103 | 425 | |||
104 | 393 | class RecipeBranch(object): | 426 | class RecipeBranch(object): |
105 | 394 | """A nested structure that represents a Recipe. | 427 | """A nested structure that represents a Recipe. |
106 | 395 | 428 | ||
107 | @@ -397,11 +430,7 @@ | |||
108 | 397 | root branch), and optionally child branches that are either merged | 430 | root branch), and optionally child branches that are either merged |
109 | 398 | or nested. | 431 | or nested. |
110 | 399 | 432 | ||
116 | 400 | The child_branches attribute is a list of tuples of (RecipeBranch, | 433 | The child_branches attribute is a list of tuples of ChildBranch objects. |
112 | 401 | relative path), where if the relative branch is not None it is the | ||
113 | 402 | path relative to this branch where the child branch should be placed. | ||
114 | 403 | If it is None then the child branch should be merged instead. | ||
115 | 404 | |||
117 | 405 | The revid attribute records the revid that the url and revspec resolved | 434 | The revid attribute records the revid that the url and revspec resolved |
118 | 406 | to when the RecipeBranch was built, or None if it has not been built. | 435 | to when the RecipeBranch was built, or None if it has not been built. |
119 | 407 | """ | 436 | """ |
120 | @@ -425,7 +454,7 @@ | |||
121 | 425 | 454 | ||
122 | 426 | :param branch: the RecipeBranch to merge. | 455 | :param branch: the RecipeBranch to merge. |
123 | 427 | """ | 456 | """ |
125 | 428 | self.child_branches.append((branch, None)) | 457 | self.child_branches.append(MergeInstruction(branch)) |
126 | 429 | 458 | ||
127 | 430 | def nest_branch(self, location, branch): | 459 | def nest_branch(self, location, branch): |
128 | 431 | """Nest a child branch in to this one. | 460 | """Nest a child branch in to this one. |
129 | @@ -433,16 +462,16 @@ | |||
130 | 433 | :param location: the relative path at which this branch should be nested. | 462 | :param location: the relative path at which this branch should be nested. |
131 | 434 | :param branch: the RecipeBranch to nest. | 463 | :param branch: the RecipeBranch to nest. |
132 | 435 | """ | 464 | """ |
134 | 436 | assert location not in [b[1] for b in self.child_branches],\ | 465 | assert location not in [b.nest_path for b in self.child_branches],\ |
135 | 437 | "%s already has branch nested there" % location | 466 | "%s already has branch nested there" % location |
137 | 438 | self.child_branches.append((branch, location)) | 467 | self.child_branches.append(NestInstruction(branch, location)) |
138 | 439 | 468 | ||
139 | 440 | def run_command(self, command): | 469 | def run_command(self, command): |
140 | 441 | """Set a command to be run. | 470 | """Set a command to be run. |
141 | 442 | 471 | ||
142 | 443 | :param command: the command to be run | 472 | :param command: the command to be run |
143 | 444 | """ | 473 | """ |
145 | 445 | self.child_branches.append((None, command)) | 474 | self.child_branches.append(CommandInstruction(None, command)) |
146 | 446 | 475 | ||
147 | 447 | def different_shape_to(self, other_branch): | 476 | def different_shape_to(self, other_branch): |
148 | 448 | """Tests whether the name, url and child_branches are the same""" | 477 | """Tests whether the name, url and child_branches are the same""" |
149 | @@ -452,16 +481,18 @@ | |||
150 | 452 | return True | 481 | return True |
151 | 453 | if len(self.child_branches) != len(other_branch.child_branches): | 482 | if len(self.child_branches) != len(other_branch.child_branches): |
152 | 454 | return True | 483 | return True |
157 | 455 | for index, (child_branch, nest_location) in \ | 484 | for index, instruction in enumerate(self.child_branches): |
158 | 456 | enumerate(self.child_branches): | 485 | child_branch = instruction.recipe_branch |
159 | 457 | other_child, other_nest_location = \ | 486 | nest_location = instruction.nest_path |
160 | 458 | other_branch.child_branches[index] | 487 | other_instruction = other_branch.child_branches[index] |
161 | 488 | other_child_branch = other_instruction.recipe_branch | ||
162 | 489 | other_nest_location = other_instruction.nest_path | ||
163 | 459 | if nest_location != other_nest_location: | 490 | if nest_location != other_nest_location: |
164 | 460 | return True | 491 | return True |
167 | 461 | if ((child_branch is None and other_child is not None) | 492 | if ((child_branch is None and other_child_branch is not None) |
168 | 462 | or (child_branch is not None and other_child is None)): | 493 | or (child_branch is not None and other_child_branch is None)): |
169 | 463 | return True | 494 | return True |
171 | 464 | if child_branch.different_shape_to(other_child): | 495 | if child_branch.different_shape_to(other_child_branch): |
172 | 465 | return True | 496 | return True |
173 | 466 | return False | 497 | return False |
174 | 467 | 498 | ||
175 | 468 | 499 | ||
176 | === modified file 'tests/test_recipe.py' | |||
177 | --- tests/test_recipe.py 2009-11-18 03:45:21 +0000 | |||
178 | +++ tests/test_recipe.py 2009-11-18 03:45:21 +0000 | |||
179 | @@ -216,7 +216,7 @@ | |||
180 | 216 | + "merge bar http://bar.org") | 216 | + "merge bar http://bar.org") |
181 | 217 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 217 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
182 | 218 | num_child_branches=1) | 218 | num_child_branches=1) |
184 | 219 | child_branch, location = base_branch.child_branches[0] | 219 | child_branch, location = base_branch.child_branches[0].as_tuple() |
185 | 220 | self.assertEqual(None, location) | 220 | self.assertEqual(None, location) |
186 | 221 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") | 221 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") |
187 | 222 | 222 | ||
188 | @@ -225,7 +225,7 @@ | |||
189 | 225 | + "nest bar http://bar.org baz") | 225 | + "nest bar http://bar.org baz") |
190 | 226 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 226 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
191 | 227 | num_child_branches=1) | 227 | num_child_branches=1) |
193 | 228 | child_branch, location = base_branch.child_branches[0] | 228 | child_branch, location = base_branch.child_branches[0].as_tuple() |
194 | 229 | self.assertEqual("baz", location) | 229 | self.assertEqual("baz", location) |
195 | 230 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") | 230 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") |
196 | 231 | 231 | ||
197 | @@ -234,10 +234,10 @@ | |||
198 | 234 | + "nest bar http://bar.org baz\nmerge zam lp:zam") | 234 | + "nest bar http://bar.org baz\nmerge zam lp:zam") |
199 | 235 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 235 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
200 | 236 | num_child_branches=2) | 236 | num_child_branches=2) |
202 | 237 | child_branch, location = base_branch.child_branches[0] | 237 | child_branch, location = base_branch.child_branches[0].as_tuple() |
203 | 238 | self.assertEqual("baz", location) | 238 | self.assertEqual("baz", location) |
204 | 239 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") | 239 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") |
206 | 240 | child_branch, location = base_branch.child_branches[1] | 240 | child_branch, location = base_branch.child_branches[1].as_tuple() |
207 | 241 | self.assertEqual(None, location) | 241 | self.assertEqual(None, location) |
208 | 242 | self.check_recipe_branch(child_branch, "zam", "lp:zam") | 242 | self.check_recipe_branch(child_branch, "zam", "lp:zam") |
209 | 243 | 243 | ||
210 | @@ -246,10 +246,10 @@ | |||
211 | 246 | + "merge zam lp:zam\nnest bar http://bar.org baz") | 246 | + "merge zam lp:zam\nnest bar http://bar.org baz") |
212 | 247 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 247 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
213 | 248 | num_child_branches=2) | 248 | num_child_branches=2) |
215 | 249 | child_branch, location = base_branch.child_branches[0] | 249 | child_branch, location = base_branch.child_branches[0].as_tuple() |
216 | 250 | self.assertEqual(None, location) | 250 | self.assertEqual(None, location) |
217 | 251 | self.check_recipe_branch(child_branch, "zam", "lp:zam") | 251 | self.check_recipe_branch(child_branch, "zam", "lp:zam") |
219 | 252 | child_branch, location = base_branch.child_branches[1] | 252 | child_branch, location = base_branch.child_branches[1].as_tuple() |
220 | 253 | self.assertEqual("baz", location) | 253 | self.assertEqual("baz", location) |
221 | 254 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") | 254 | self.check_recipe_branch(child_branch, "bar", "http://bar.org") |
222 | 255 | 255 | ||
223 | @@ -258,11 +258,11 @@ | |||
224 | 258 | + "nest bar http://bar.org baz\n merge zam lp:zam") | 258 | + "nest bar http://bar.org baz\n merge zam lp:zam") |
225 | 259 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 259 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
226 | 260 | num_child_branches=1) | 260 | num_child_branches=1) |
228 | 261 | child_branch, location = base_branch.child_branches[0] | 261 | child_branch, location = base_branch.child_branches[0].as_tuple() |
229 | 262 | self.assertEqual("baz", location) | 262 | self.assertEqual("baz", location) |
230 | 263 | self.check_recipe_branch(child_branch, "bar", "http://bar.org", | 263 | self.check_recipe_branch(child_branch, "bar", "http://bar.org", |
231 | 264 | num_child_branches=1) | 264 | num_child_branches=1) |
233 | 265 | child_branch, location = child_branch.child_branches[0] | 265 | child_branch, location = child_branch.child_branches[0].as_tuple() |
234 | 266 | self.assertEqual(None, location) | 266 | self.assertEqual(None, location) |
235 | 267 | self.check_recipe_branch(child_branch, "zam", "lp:zam") | 267 | self.check_recipe_branch(child_branch, "zam", "lp:zam") |
236 | 268 | 268 | ||
237 | @@ -271,11 +271,11 @@ | |||
238 | 271 | + "nest bar http://bar.org baz\n nest zam lp:zam zoo") | 271 | + "nest bar http://bar.org baz\n nest zam lp:zam zoo") |
239 | 272 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 272 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
240 | 273 | num_child_branches=1) | 273 | num_child_branches=1) |
242 | 274 | child_branch, location = base_branch.child_branches[0] | 274 | child_branch, location = base_branch.child_branches[0].as_tuple() |
243 | 275 | self.assertEqual("baz", location) | 275 | self.assertEqual("baz", location) |
244 | 276 | self.check_recipe_branch(child_branch, "bar", "http://bar.org", | 276 | self.check_recipe_branch(child_branch, "bar", "http://bar.org", |
245 | 277 | num_child_branches=1) | 277 | num_child_branches=1) |
247 | 278 | child_branch, location = child_branch.child_branches[0] | 278 | child_branch, location = child_branch.child_branches[0].as_tuple() |
248 | 279 | self.assertEqual("zoo", location) | 279 | self.assertEqual("zoo", location) |
249 | 280 | self.check_recipe_branch(child_branch, "zam", "lp:zam") | 280 | self.check_recipe_branch(child_branch, "zam", "lp:zam") |
250 | 281 | 281 | ||
251 | @@ -286,11 +286,13 @@ | |||
252 | 286 | + "merge zam lp:zam 2") | 286 | + "merge zam lp:zam 2") |
253 | 287 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 287 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
254 | 288 | num_child_branches=2, revspec="revid:a") | 288 | num_child_branches=2, revspec="revid:a") |
256 | 289 | child_branch, location = base_branch.child_branches[0] | 289 | instruction = base_branch.child_branches[0] |
257 | 290 | child_branch = instruction.recipe_branch | ||
258 | 291 | location = instruction.nest_path | ||
259 | 290 | self.assertEqual("baz", location) | 292 | self.assertEqual("baz", location) |
260 | 291 | self.check_recipe_branch(child_branch, "bar", "http://bar.org", | 293 | self.check_recipe_branch(child_branch, "bar", "http://bar.org", |
261 | 292 | revspec="tag:b") | 294 | revspec="tag:b") |
263 | 293 | child_branch, location = base_branch.child_branches[1] | 295 | child_branch, location = base_branch.child_branches[1].as_tuple() |
264 | 294 | self.assertEqual(None, location) | 296 | self.assertEqual(None, location) |
265 | 295 | self.check_recipe_branch(child_branch, "zam", "lp:zam", revspec="2") | 297 | self.check_recipe_branch(child_branch, "zam", "lp:zam", revspec="2") |
266 | 296 | 298 | ||
267 | @@ -300,7 +302,7 @@ | |||
268 | 300 | + "run touch test \n") | 302 | + "run touch test \n") |
269 | 301 | self.check_base_recipe_branch(base_branch, "http://foo.org/", | 303 | self.check_base_recipe_branch(base_branch, "http://foo.org/", |
270 | 302 | num_child_branches=1) | 304 | num_child_branches=1) |
272 | 303 | child_branch, command = base_branch.child_branches[0] | 305 | child_branch, command = base_branch.child_branches[0].as_tuple() |
273 | 304 | self.assertEqual(None, child_branch) | 306 | self.assertEqual(None, child_branch) |
274 | 305 | self.assertEqual("touch test", command) | 307 | self.assertEqual("touch test", command) |
275 | 306 | 308 |
This basically a replaces a list of 2-tuples where (*, None), (None, *) and (None, None) all mean different things, and replaces them with objects. This also replaces if/elif/... blocks about those cases with good polymorphism.
The names I gave to the objects and methods can probably be improved a fair bit, and I'm happy to do that if you have any suggestions about what these items should be called.
I made this change as part of developing lp:~spiv/bzr-builder/merge-subdirs-479705