Merge lp:~thumper/launchpad/bzr-format-matching-refactoring into lp:launchpad

Proposed by Tim Penhey
Status: Merged
Approved by: Michael Hudson-Doyle
Approved revision: no longer in the source branch.
Merged at revision: 11242
Proposed branch: lp:~thumper/launchpad/bzr-format-matching-refactoring
Merge into: lp:launchpad
Prerequisite: lp:~thumper/launchpad/stop-factory-mirroring
Diff against target: 238 lines (+103/-37)
5 files modified
lib/lp/code/bzr.py (+30/-3)
lib/lp/code/enums.py (+0/-9)
lib/lp/code/model/branchjob.py (+3/-17)
lib/lp/code/tests/test_bzr.py (+66/-0)
lib/lp/code/xmlrpc/codehosting.py (+4/-8)
To merge this branch: bzr merge lp:~thumper/launchpad/bzr-format-matching-refactoring
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle Approve
Review via email: mp+31014@code.launchpad.net

Commit message

Refactored the way to get enumerated values for the bazaar formats.

Description of the change

Simplify the say to get the enumerated values for bzr formats.

This will be used in the merge directive handling in the code mail handler, but can be reviewed independently.

To post a comment you must log in.
Revision history for this message
Michael Hudson-Doyle (mwhudson) wrote :

As discussed on IRC, 'default' means something different for branch formats already, so please change the tests to talk about 'unrecognized' format strings. Otherwise, thanks! Something like this change was overdue :-)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/bzr.py'
2--- lib/lp/code/bzr.py 2010-02-17 01:37:22 +0000
3+++ lib/lp/code/bzr.py 2010-07-27 05:12:51 +0000
4@@ -9,6 +9,7 @@
5 'ControlFormat',
6 'CURRENT_BRANCH_FORMATS',
7 'CURRENT_REPOSITORY_FORMATS',
8+ 'get_branch_formats',
9 'RepositoryFormat',
10 ]
11
12@@ -45,7 +46,20 @@
13 return DBItem(num, format_string, description)
14
15
16-class BranchFormat(DBEnumeratedType):
17+class BazaarFormatEnum(DBEnumeratedType):
18+ """Base class for the format enums."""
19+
20+ @classmethod
21+ def get_enum(klass, format_name):
22+ """Find the matching enum value for the format name specified."""
23+ for value in klass.items:
24+ if value.title == format_name:
25+ return value
26+ else:
27+ return klass.UNRECOGNIZED
28+
29+
30+class BranchFormat(BazaarFormatEnum):
31 """Branch on-disk format.
32
33 This indicates which (Bazaar) format is used on-disk. The list must be
34@@ -81,7 +95,7 @@
35 107, "Bazaar-NG Loom branch format 7\n", "Loom branch format 7")
36
37
38-class RepositoryFormat(DBEnumeratedType):
39+class RepositoryFormat(BazaarFormatEnum):
40 """Repository on-disk format.
41
42 This indicates which (Bazaar) format is used on-disk. The list must be
43@@ -194,7 +208,7 @@
44 BZR_CHK_2A = _format_enum(415, RepositoryFormat2a)
45
46
47-class ControlFormat(DBEnumeratedType):
48+class ControlFormat(BazaarFormatEnum):
49 """Control directory (BzrDir) format.
50
51 This indicates what control directory format is on disk. Must be updated
52@@ -236,3 +250,16 @@
53 RepositoryFormat.BZR_CHK1,
54 RepositoryFormat.BZR_CHK2,
55 RepositoryFormat.BZR_CHK_2A)
56+
57+
58+def get_branch_formats(bzr_branch):
59+ """Return a tuple of format enumerations for the bazaar branch.
60+
61+ :returns: tuple of (ControlFormat, BranchFormat, RepositoryFormat)
62+ """
63+ control_string = bzr_branch.bzrdir._format.get_format_string()
64+ branch_string = bzr_branch._format.get_format_string()
65+ repository_string = bzr_branch.repository._format.get_format_string()
66+ return (ControlFormat.get_enum(control_string),
67+ BranchFormat.get_enum(branch_string),
68+ RepositoryFormat.get_enum(repository_string))
69
70=== modified file 'lib/lp/code/enums.py'
71--- lib/lp/code/enums.py 2010-06-11 03:52:02 +0000
72+++ lib/lp/code/enums.py 2010-07-27 05:12:51 +0000
73@@ -22,7 +22,6 @@
74 'CodeImportReviewStatus',
75 'CodeReviewNotificationLevel',
76 'CodeReviewVote',
77- 'match_enum_title',
78 'RevisionControlSystems',
79 'TeamBranchVisibilityRule',
80 'UICreatableBranchType',
81@@ -32,14 +31,6 @@
82 DBEnumeratedType, DBItem, EnumeratedType, Item, use_template)
83
84
85-def match_enum_title(enum, title, default):
86- for value in enum.items:
87- if value.title == title:
88- return value
89- else:
90- return default
91-
92-
93 class BranchLifecycleStatus(DBEnumeratedType):
94 """Branch Lifecycle Status
95
96
97=== modified file 'lib/lp/code/model/branchjob.py'
98--- lib/lp/code/model/branchjob.py 2010-06-11 03:52:02 +0000
99+++ lib/lp/code/model/branchjob.py 2010-07-27 05:12:51 +0000
100@@ -46,8 +46,7 @@
101 from canonical.database.enumcol import EnumCol
102 from canonical.database.sqlbase import SQLBase
103 from canonical.launchpad.webapp import canonical_url, errorlog
104-from lp.code.bzr import BranchFormat, ControlFormat, RepositoryFormat
105-from lp.code.enums import match_enum_title
106+from lp.code.bzr import get_branch_formats
107 from lp.code.model.branch import Branch
108 from lp.code.model.branchmergeproposal import BranchMergeProposal
109 from lp.code.model.diff import StaticDiff
110@@ -369,25 +368,12 @@
111 source_branch = BzrBranch.open_from_transport(
112 source_branch_transport)
113
114- control_format = match_enum_title(
115- ControlFormat,
116- source_branch.bzrdir._format.get_format_string(),
117- ControlFormat.UNRECOGNIZED)
118- branch_format = match_enum_title(
119- BranchFormat,
120- source_branch._format.get_format_string(),
121- BranchFormat.UNRECOGNIZED)
122- repository_format = match_enum_title(
123- RepositoryFormat,
124- source_branch.repository._format.get_format_string(),
125- RepositoryFormat.UNRECOGNIZED)
126+ formats = get_branch_formats(source_branch)
127
128 self.branch.branchChanged(
129 self.branch.stacked_on,
130 self.branch.last_scanned_id,
131- control_format,
132- branch_format,
133- repository_format)
134+ *formats)
135 finally:
136 shutil.rmtree(upgrade_branch_path)
137
138
139=== added file 'lib/lp/code/tests/test_bzr.py'
140--- lib/lp/code/tests/test_bzr.py 1970-01-01 00:00:00 +0000
141+++ lib/lp/code/tests/test_bzr.py 2010-07-27 05:12:51 +0000
142@@ -0,0 +1,66 @@
143+# Copyright 2010 Canonical Ltd. This software is licensed under the
144+# GNU Affero General Public License version 3 (see the file LICENSE).
145+
146+"""Tests for lp.code.bzr."""
147+
148+__metaclass__ = type
149+
150+from lp.code.bzr import (
151+ BranchFormat, ControlFormat, get_branch_formats, RepositoryFormat)
152+from lp.testing import TestCase
153+from bzrlib.tests import TestCaseInTempDir
154+
155+
156+class TestBazaarFormatEnum(TestCase):
157+ """Tests for the bazaar formats."""
158+
159+ def test_branch_format_unrecognized(self):
160+ # Unknown branch formats are unrecognized.
161+ format = BranchFormat.get_enum('no-idea')
162+ self.assertEqual(BranchFormat.UNRECOGNIZED, format)
163+
164+ def test_control_format_unrecognized(self):
165+ # Unknown control formats are unrecognized.
166+ format = ControlFormat.get_enum('no-idea')
167+ self.assertEqual(ControlFormat.UNRECOGNIZED, format)
168+
169+ def test_repository_format_unrecognized(self):
170+ # Unknown repository formats are unrecognized.
171+ format = RepositoryFormat.get_enum('no-idea')
172+ self.assertEqual(RepositoryFormat.UNRECOGNIZED, format)
173+
174+
175+class TestGetBranchFormats(TestCaseInTempDir):
176+ """Tests for lp.code.bzr.get_branch_formats."""
177+
178+ def test_get_branch_format_2a(self):
179+ # Test the 2a branch format.
180+ branch = self.make_branch('test', '2a')
181+ formats = get_branch_formats(branch)
182+ self.assertEqual(ControlFormat.BZR_METADIR_1, formats[0])
183+ self.assertEqual(BranchFormat.BZR_BRANCH_7, formats[1])
184+ self.assertEqual(RepositoryFormat.BZR_CHK_2A, formats[2])
185+
186+ def test_get_branch_format_1_9(self):
187+ # Test the 1.9 branch format.
188+ branch = self.make_branch('test', '1.9')
189+ formats = get_branch_formats(branch)
190+ self.assertEqual(ControlFormat.BZR_METADIR_1, formats[0])
191+ self.assertEqual(BranchFormat.BZR_BRANCH_7, formats[1])
192+ self.assertEqual(RepositoryFormat.BZR_KNITPACK_6, formats[2])
193+
194+ def test_get_branch_format_packs(self):
195+ # Test the packs branch format.
196+ branch = self.make_branch('test', 'pack-0.92')
197+ formats = get_branch_formats(branch)
198+ self.assertEqual(ControlFormat.BZR_METADIR_1, formats[0])
199+ self.assertEqual(BranchFormat.BZR_BRANCH_6, formats[1])
200+ self.assertEqual(RepositoryFormat.BZR_KNITPACK_1, formats[2])
201+
202+ def test_get_branch_format_knits(self):
203+ # Test the knits branch format.
204+ branch = self.make_branch('test', 'knit')
205+ formats = get_branch_formats(branch)
206+ self.assertEqual(ControlFormat.BZR_METADIR_1, formats[0])
207+ self.assertEqual(BranchFormat.BZR_BRANCH_5, formats[1])
208+ self.assertEqual(RepositoryFormat.BZR_KNIT_1, formats[2])
209
210=== modified file 'lib/lp/code/xmlrpc/codehosting.py'
211--- lib/lp/code/xmlrpc/codehosting.py 2010-06-11 03:52:02 +0000
212+++ lib/lp/code/xmlrpc/codehosting.py 2010-07-27 05:12:51 +0000
213@@ -33,7 +33,7 @@
214
215 from lp.code.errors import UnknownBranchTypeError
216 from lp.code.bzr import BranchFormat, ControlFormat, RepositoryFormat
217-from lp.code.enums import BranchType, match_enum_title
218+from lp.code.enums import BranchType
219 from lp.code.interfaces.branch import BranchCreationException
220 from lp.code.interfaces.branchlookup import IBranchLookup
221 from lp.code.interfaces.branchnamespace import (
222@@ -206,13 +206,9 @@
223 if branch is None:
224 return faults.NoBranchWithID(branch_id)
225
226- control_format = match_enum_title(
227- ControlFormat, control_string, ControlFormat.UNRECOGNIZED)
228- branch_format = match_enum_title(
229- BranchFormat, branch_string, BranchFormat.UNRECOGNIZED)
230- repository_format = match_enum_title(
231- RepositoryFormat, repository_string,
232- RepositoryFormat.UNRECOGNIZED)
233+ control_format = ControlFormat.get_enum(control_string)
234+ branch_format = BranchFormat.get_enum(branch_string)
235+ repository_format = RepositoryFormat.get_enum(repository_string)
236
237 if requester == LAUNCHPAD_SERVICES:
238 branch = removeSecurityProxy(branch)