Merge lp:~jelmer/bzr-builder/iter-branches into lp:bzr-builder

Proposed by Jelmer Vernooij
Status: Merged
Approved by: James Westby
Approved revision: 137
Merged at revision: 134
Proposed branch: lp:~jelmer/bzr-builder/iter-branches
Merge into: lp:bzr-builder
Prerequisite: lp:~jelmer/bzr-builder/resolve-revids
Diff against target: 223 lines (+87/-30)
2 files modified
recipe.py (+57/-30)
tests/test_recipe.py (+30/-0)
To merge this branch: bzr merge lp:~jelmer/bzr-builder/iter-branches
Reviewer Review Type Date Requested Status
James Westby Approve
Review via email: mp+64212@code.launchpad.net

Description of the change

Add RecipeBranch.iter_all_branches() and Recipebranch.iter_all_instructions().

To post a comment you must log in.
Revision history for this message
James Westby (james-w) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'recipe.py'
2--- recipe.py 2011-06-09 12:47:24 +0000
3+++ recipe.py 2011-06-10 16:13:46 +0000
4@@ -18,7 +18,7 @@
5 import subprocess
6
7 from bzrlib import (
8- branch,
9+ branch as _mod_branch,
10 bzrdir,
11 errors,
12 lazy_regex,
13@@ -315,28 +315,25 @@
14 self.branch_name)
15
16
17-ok_to_preserve = [DebUpstreamVariable.name, DebUpstreamBaseVariable.name,
18- DebVersionVariable.name]
19+ok_to_preserve = [DebUpstreamVariable, DebUpstreamBaseVariable,
20+ DebVersionVariable]
21 # The variables that don't require substitution in their name
22-simple_vars = [TimeVariable.name, DateVariable.name, RevnoVariable.name,
23- SubversionRevnumVariable.name, DebUpstreamVariable.name,
24- DebUpstreamBaseVariable.name, GitCommitVariable.name,
25- LatestTagVariable.name, DebVersionVariable.name]
26-
27+simple_vars = [TimeVariable, DateVariable, DebUpstreamVariable,
28+ DebUpstreamBaseVariable, DebVersionVariable]
29+branch_vars = [RevnoVariable, SubversionRevnumVariable,
30+ GitCommitVariable, LatestTagVariable]
31
32 def check_expanded_deb_version(base_branch):
33 checked_version = base_branch.deb_version
34 if checked_version is None:
35 return
36 for token in ok_to_preserve:
37- checked_version = checked_version.replace(token, "")
38+ checked_version = checked_version.replace(token.name, "")
39 if "{" in checked_version:
40- available_tokens = simple_vars
41+ available_tokens = [var.name for var in simple_vars + branch_vars]
42 for name in base_branch.list_branch_names():
43- available_tokens.append(RevnoVariable(name, None).name)
44- available_tokens.append(SubversionRevnumVariable(name, None).name)
45- available_tokens.append(GitCommitVariable(name, None).name)
46- available_tokens.append(LatestTagVariable(name, None).name)
47+ for var_kls in branch_vars:
48+ available_tokens.append(var_kls(name, None).name)
49 raise errors.BzrCommandError("deb-version not fully "
50 "expanded: %s. Valid substitutions are: %s"
51 % (base_branch.deb_version, available_tokens))
52@@ -459,7 +456,7 @@
53 :param br_to: the Branch to merge in to.
54 """
55 if child_branch.branch is None:
56- child_branch.branch = branch.Branch.open(child_branch.url,
57+ child_branch.branch = _mod_branch.Branch.open(child_branch.url,
58 possible_transports=possible_transports)
59 child_branch.branch.lock_read()
60 try:
61@@ -522,7 +519,7 @@
62 :param target_subdir: (optional) directory in target to merge that
63 subpath into. Defaults to basename of subpath.
64 """
65- child_branch.branch = branch.Branch.open(child_branch.url)
66+ child_branch.branch = _mod_branch.Branch.open(child_branch.url)
67 child_branch.branch.lock_read()
68 try:
69 child_branch.resolve_revision_id()
70@@ -552,7 +549,7 @@
71 def update_branch(base_branch, tree_to, br_to, to_transport,
72 possible_transports=None):
73 if base_branch.branch is None:
74- base_branch.branch = branch.Branch.open(base_branch.url,
75+ base_branch.branch = _mod_branch.Branch.open(base_branch.url,
76 possible_transports=possible_transports)
77 base_branch.branch.lock_read()
78 try:
79@@ -568,7 +565,7 @@
80 def _resolve_revisions_recurse(new_branch, substitute_revno,
81 if_changed_from=None):
82 changed = False
83- new_branch.branch = branch.Branch.open(new_branch.url)
84+ new_branch.branch = _mod_branch.Branch.open(new_branch.url)
85 new_branch.branch.lock_read()
86 try:
87 new_branch.resolve_revision_id()
88@@ -711,6 +708,9 @@
89 revid_part = ""
90 return revid_part
91
92+ def __repr__(self):
93+ return "<%s %r>" % (self.__class__.__name__, self.nest_path)
94+
95
96 class CommandInstruction(ChildBranch):
97
98@@ -744,6 +744,9 @@
99 MERGE_INSTRUCTION, self.recipe_branch.name,
100 self.recipe_branch.url, revid_part)
101
102+ def __repr__(self):
103+ return "<%s %r>" % (self.__class__.__name__, self.recipe_branch.name)
104+
105
106 class NestPartInstruction(ChildBranch):
107
108@@ -788,6 +791,10 @@
109 NEST_INSTRUCTION, self.recipe_branch.name,
110 self.recipe_branch.url, self.nest_path, revid_part)
111
112+ def __repr__(self):
113+ return "<%s %r>" % (self.__class__.__name__,
114+ self.recipe_branch.name)
115+
116
117 class RecipeBranch(object):
118 """A nested structure that represents a Recipe.
119@@ -893,14 +900,33 @@
120 return True
121 return False
122
123- def _list_child_names(self):
124- child_names = []
125- for instruction in self.child_branches:
126- child_branch = instruction.recipe_branch
127- if child_branch is None:
128- continue
129- child_names += child_branch.list_branch_names()
130- return child_names
131+ def iter_all_instructions(self):
132+ """Iter over all instructions under this branch."""
133+ for instruction in self.child_branches:
134+ yield instruction
135+ child_branch = instruction.recipe_branch
136+ if child_branch is None:
137+ continue
138+ for instruction in child_branch.iter_all_instructions():
139+ yield instruction
140+
141+ def iter_all_branches(self):
142+ """Iterate over all branches."""
143+ yield self
144+ for instruction in self.child_branches:
145+ child_branch = instruction.recipe_branch
146+ if child_branch is None:
147+ continue
148+ for subbranch in child_branch.iter_all_branches():
149+ yield subbranch
150+
151+ def lookup_branch(self, name):
152+ """Lookup a branch by its name."""
153+ for branch in self.iter_all_branches():
154+ if branch.name == name:
155+ return branch
156+ else:
157+ raise KeyError(name)
158
159 def list_branch_names(self):
160 """List all of the branch names under this one.
161@@ -908,7 +934,11 @@
162 :return: a list of the branch names.
163 :rtype: list(str)
164 """
165- return [self.name] + self._list_child_names()
166+ return [branch.name for branch in self.iter_all_branches()
167+ if branch.name is not None]
168+
169+ def __repr__(self):
170+ return "<%s %r>" % (self.__class__.__name__, self.name)
171
172
173 class BaseRecipeBranch(RecipeBranch):
174@@ -983,9 +1013,6 @@
175 def __str__(self):
176 return self.get_recipe_text(validate=True)
177
178- def list_branch_names(self):
179- return self._list_child_names()
180-
181 def get_recipe_text(self, validate=False):
182 manifest = "# bzr-builder format %s" % str(self.format)
183 if self.deb_version is not None:
184
185=== modified file 'tests/test_recipe.py'
186--- tests/test_recipe.py 2011-06-05 13:56:06 +0000
187+++ tests/test_recipe.py 2011-06-10 16:13:46 +0000
188@@ -1351,6 +1351,36 @@
189 ["merged", "nested", "merged_into_nested", "another_nested"],
190 base_branch.list_branch_names())
191
192+ def test_iter_all_branches(self):
193+ base_branch = BaseRecipeBranch("base_url", "1", 0.2)
194+ merged_branch = RecipeBranch("merged", "merged_url")
195+ base_branch.merge_branch(merged_branch)
196+ nested_branch = RecipeBranch("nested", "nested_url")
197+ merge_into_nested_branch = RecipeBranch("merged_into_nested", "another_url")
198+ nested_branch.merge_branch(merge_into_nested_branch)
199+ base_branch.nest_branch("subdir", nested_branch)
200+ another_nested_branch = RecipeBranch("another_nested", "yet_another_url")
201+ base_branch.merge_branch(another_nested_branch)
202+ base_branch.run_command("a command")
203+ self.assertEqual([
204+ base_branch, merged_branch, nested_branch, merge_into_nested_branch,
205+ another_nested_branch],
206+ list(base_branch.iter_all_branches()))
207+
208+ def test_iter_all_instructions(self):
209+ base_branch = BaseRecipeBranch("base_url", "1", 0.2)
210+ nested_branch = RecipeBranch("nested", "nested_url")
211+ merge_into_nested_branch = RecipeBranch("merged_into_nested", "another_url")
212+ nested_branch.merge_branch(merge_into_nested_branch)
213+ merge_into_nested = nested_branch.child_branches[-1]
214+ base_branch.run_command("a command")
215+ cmd = base_branch.child_branches[-1]
216+ base_branch.nest_branch("subdir", nested_branch)
217+ nest = base_branch.child_branches[-1]
218+ self.assertEqual([
219+ cmd, nest, merge_into_nested],
220+ list(base_branch.iter_all_instructions()))
221+
222
223 class DebUpstreamVariableTests(TestCase):
224

Subscribers

People subscribed via source and target branches