Merge lp:~jelmer/bzr-builddeb/refactor-upstream-2 into lp:~jelmer/bzr-builddeb/refactor-upstream
- refactor-upstream-2
- Merge into refactor-upstream
Proposed by
Jelmer Vernooij
Status: | Merged |
---|---|
Merge reported by: | Jelmer Vernooij |
Merged at revision: | not available |
Proposed branch: | lp:~jelmer/bzr-builddeb/refactor-upstream-2 |
Merge into: | lp:~jelmer/bzr-builddeb/refactor-upstream |
Diff against target: |
761 lines (+339/-316) 7 files modified
cmds.py (+2/-0) merge_upstream.py (+0/-143) tests/test_config.py (+1/-1) tests/test_merge_upstream.py (+3/-112) tests/test_upstream.py (+114/-1) upstream/__init__.py (+0/-59) upstream/branch.py (+219/-0) |
To merge this branch: | bzr merge lp:~jelmer/bzr-builddeb/refactor-upstream-2 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
James Westby (community) | Approve | ||
Jelmer Vernooij | Pending | ||
Review via email:
|
Commit message
Description of the change
Refactoring of the upstream module - move UpstreamBranchS
To post a comment you must log in.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
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 'cmds.py' |
2 | --- cmds.py 2011-01-24 06:23:40 +0000 |
3 | +++ cmds.py 2011-01-28 23:09:00 +0000 |
4 | @@ -96,6 +96,8 @@ |
5 | StackedUpstreamSource, |
6 | UScanSource, |
7 | UpstreamProvider, |
8 | + ) |
9 | +from bzrlib.plugins.builddeb.upstream.branch import ( |
10 | UpstreamBranchSource, |
11 | ) |
12 | from bzrlib.plugins.builddeb.util import ( |
13 | |
14 | === modified file 'merge_upstream.py' |
15 | --- merge_upstream.py 2011-01-24 02:16:17 +0000 |
16 | +++ merge_upstream.py 2011-01-28 23:09:00 +0000 |
17 | @@ -34,152 +34,9 @@ |
18 | # Prior to 0.1.15 the debian module was called debian_bundle |
19 | from debian_bundle.changelog import Version |
20 | |
21 | -from bzrlib.errors import InvalidRevisionId |
22 | -from bzrlib.revisionspec import RevisionSpec |
23 | - |
24 | -from bzrlib.plugins.builddeb.util import get_snapshot_revision |
25 | - |
26 | - |
27 | TAG_PREFIX = "upstream-" |
28 | |
29 | |
30 | -def extract_svn_revno(rev): |
31 | - """Extract the Subversion number of a revision from a revision. |
32 | - |
33 | - :param rev: Revision object |
34 | - :return: Revision number, None if this was not a Subversion revision or |
35 | - if the revision number could not be determined (bzr-svn not available). |
36 | - """ |
37 | - try: |
38 | - from bzrlib.plugins.svn import extract_svn_foreign_revid |
39 | - except ImportError: |
40 | - # No svn support |
41 | - return None |
42 | - else: |
43 | - try: |
44 | - (svn_uuid, branch_path, svn_revno) = extract_svn_foreign_revid(rev) |
45 | - except InvalidRevisionId: |
46 | - return None |
47 | - else: |
48 | - return svn_revno |
49 | - |
50 | - |
51 | -def upstream_version_add_revision(upstream_branch, version_string, revid): |
52 | - """Update the revision in a upstream version string. |
53 | - |
54 | - :param branch: Branch in which the revision can be found |
55 | - :param version_string: Original version string |
56 | - :param revid: Revision id of the revision |
57 | - """ |
58 | - revno = upstream_branch.revision_id_to_revno(revid) |
59 | - |
60 | - if "+bzr" in version_string: |
61 | - return "%s+bzr%d" % (version_string[:version_string.rfind("+bzr")], revno) |
62 | - |
63 | - if "~bzr" in version_string: |
64 | - return "%s~bzr%d" % (version_string[:version_string.rfind("~bzr")], revno) |
65 | - |
66 | - rev = upstream_branch.repository.get_revision(revid) |
67 | - svn_revno = extract_svn_revno(rev) |
68 | - |
69 | - # FIXME: Raise error if +svn/~svn is present and svn_revno is not set? |
70 | - if "+svn" in version_string and svn_revno: |
71 | - return "%s+svn%d" % (version_string[:version_string.rfind("+svn")], svn_revno) |
72 | - if "~svn" in version_string and svn_revno: |
73 | - return "%s~svn%d" % (version_string[:version_string.rfind("~svn")], svn_revno) |
74 | - |
75 | - if svn_revno: |
76 | - return "%s+svn%d" % (version_string, svn_revno) |
77 | - else: |
78 | - return "%s+bzr%d" % (version_string, revno) |
79 | - |
80 | - |
81 | -def _upstream_branch_version(revhistory, reverse_tag_dict, package, |
82 | - previous_version, add_rev): |
83 | - """Determine the version string of an upstream branch. |
84 | - |
85 | - The upstream version is determined from the most recent tag |
86 | - in the upstream branch. If that tag does not point at the last revision, |
87 | - the revision number is added to it (<version>+bzr<revno>). |
88 | - |
89 | - If there are no tags set on the upstream branch, the previous Debian |
90 | - version is used and combined with the bzr revision number |
91 | - (usually <version>+bzr<revno>). |
92 | - |
93 | - :param revhistory: Branch revision history. |
94 | - :param reverse_tag_dict: Reverse tag dictionary (revid -> list of tags) |
95 | - :param package: Name of package. |
96 | - :param previous_version: Previous upstream version in debian changelog. |
97 | - :param add_rev: Function that can add a revision suffix to a version string. |
98 | - :return: Name of the upstream revision. |
99 | - """ |
100 | - if revhistory == []: |
101 | - # No new version to merge |
102 | - return Version(previous_version) |
103 | - for r in reversed(revhistory): |
104 | - if r in reverse_tag_dict: |
105 | - # If there is a newer version tagged in branch, |
106 | - # convert to upstream version |
107 | - # return <upstream_version>+bzr<revno> |
108 | - for tag in reverse_tag_dict[r]: |
109 | - upstream_version = upstream_tag_to_version(tag, |
110 | - package=package) |
111 | - if upstream_version is not None: |
112 | - if r != revhistory[-1]: |
113 | - upstream_version.upstream_version = add_rev( |
114 | - upstream_version.upstream_version, revhistory[-1]) |
115 | - return upstream_version |
116 | - return Version(add_rev(previous_version, revhistory[-1])) |
117 | - |
118 | - |
119 | -def upstream_branch_version(upstream_branch, upstream_revision, package, |
120 | - previous_version): |
121 | - """Determine the version string for a revision in an upstream branch. |
122 | - |
123 | - :param upstream_branch: The upstream branch object |
124 | - :param upstream_revision: The revision id of the upstream revision |
125 | - :param package: The name of the package |
126 | - :param previous_version: The previous upstream version string |
127 | - :return: Upstream version string for `upstream_revision`. |
128 | - """ |
129 | - dotted_revno = upstream_branch.revision_id_to_dotted_revno(upstream_revision) |
130 | - if len(dotted_revno) > 1: |
131 | - revno = -2 |
132 | - else: |
133 | - revno = dotted_revno[0] |
134 | - revhistory = upstream_branch.revision_history() |
135 | - previous_revision = get_snapshot_revision(previous_version) |
136 | - if previous_revision is not None: |
137 | - previous_revspec = RevisionSpec.from_string(previous_revision) |
138 | - previous_revno, _ = previous_revspec.in_history(upstream_branch) |
139 | - # Trim revision history - we don't care about any revisions |
140 | - # before the revision of the previous version |
141 | - else: |
142 | - previous_revno = 0 |
143 | - revhistory = revhistory[previous_revno:revno+1] |
144 | - return _upstream_branch_version(revhistory, |
145 | - upstream_branch.tags.get_reverse_tag_dict(), package, |
146 | - previous_version, |
147 | - lambda version, revision: upstream_version_add_revision(upstream_branch, version, revision)) |
148 | - |
149 | - |
150 | -def upstream_tag_to_version(tag_name, package=None): |
151 | - """Take a tag name and return the upstream version, or None.""" |
152 | - if tag_name.startswith(TAG_PREFIX): |
153 | - return Version(tag_name[len(TAG_PREFIX):]) |
154 | - if (package is not None and ( |
155 | - tag_name.startswith("%s-" % package) or |
156 | - tag_name.startswith("%s_" % package))): |
157 | - return Version(tag_name[len(package)+1:]) |
158 | - if tag_name.startswith("release-"): |
159 | - return Version(tag_name[len("release-"):]) |
160 | - if tag_name[0] == "v" and tag_name[1].isdigit(): |
161 | - return Version(tag_name[1:]) |
162 | - if all([c.isdigit() or c in (".", "~") for c in tag_name]): |
163 | - return Version(tag_name) |
164 | - return None |
165 | - |
166 | - |
167 | def package_version(upstream_version, distribution_name): |
168 | """Determine the package version for a new upstream. |
169 | |
170 | |
171 | === modified file 'tests/test_config.py' |
172 | --- tests/test_config.py 2011-01-10 23:04:04 +0000 |
173 | +++ tests/test_config.py 2011-01-28 23:09:00 +0000 |
174 | @@ -79,7 +79,7 @@ |
175 | |
176 | def test_no_entry(self): |
177 | self.assertEqual(self.config.merge, False) |
178 | - self.assertEqual(self.config.build_type, BUILD_TYPE_NORMAL) |
179 | + self.assertEqual(self.config.build_type, None) |
180 | |
181 | def test_parse_error(self): |
182 | f = open('invalid.conf', 'wb') |
183 | |
184 | === modified file 'tests/test_merge_upstream.py' |
185 | --- tests/test_merge_upstream.py 2011-01-17 22:18:44 +0000 |
186 | +++ tests/test_merge_upstream.py 2011-01-28 23:09:00 +0000 |
187 | @@ -28,118 +28,9 @@ |
188 | from bzrlib.tests import TestCase, TestCaseWithTransport |
189 | |
190 | from bzrlib.plugins.builddeb.merge_upstream import ( |
191 | - upstream_merge_changelog_line, |
192 | - package_version, |
193 | - _upstream_branch_version, |
194 | - upstream_tag_to_version, |
195 | - upstream_version_add_revision |
196 | - ) |
197 | - |
198 | - |
199 | -class TestUpstreamVersionAddRevision(TestCaseWithTransport): |
200 | - """Test that updating the version string works.""" |
201 | - |
202 | - def setUp(self): |
203 | - super(TestUpstreamVersionAddRevision, self).setUp() |
204 | - self.revnos = {} |
205 | - self.svn_revnos = {"somesvnrev": 45} |
206 | - self.revnos = {"somerev": 42, "somesvnrev": 12} |
207 | - self.repository = self |
208 | - |
209 | - def revision_id_to_revno(self, revid): |
210 | - return self.revnos[revid] |
211 | - |
212 | - def get_revision(self, revid): |
213 | - rev = Revision(revid) |
214 | - if revid in self.svn_revnos: |
215 | - rev.foreign_revid = ("uuid", "bp", self.svn_revnos[revid]) |
216 | - return rev |
217 | - |
218 | - def test_update_plus_rev(self): |
219 | - self.assertEquals("1.3+bzr42", |
220 | - upstream_version_add_revision(self, "1.3+bzr23", "somerev")) |
221 | - |
222 | - def test_update_tilde_rev(self): |
223 | - self.assertEquals("1.3~bzr42", |
224 | - upstream_version_add_revision(self, "1.3~bzr23", "somerev")) |
225 | - |
226 | - def test_new_rev(self): |
227 | - self.assertEquals("1.3+bzr42", |
228 | - upstream_version_add_revision(self, "1.3", "somerev")) |
229 | - |
230 | - def test_svn_new_rev(self): |
231 | - self.assertEquals("1.3+svn45", |
232 | - upstream_version_add_revision(self, "1.3", "somesvnrev")) |
233 | - |
234 | - def test_svn_plus_rev(self): |
235 | - self.assertEquals("1.3+svn45", |
236 | - upstream_version_add_revision(self, "1.3+svn3", "somesvnrev")) |
237 | - |
238 | - def test_svn_tilde_rev(self): |
239 | - self.assertEquals("1.3~svn45", |
240 | - upstream_version_add_revision(self, "1.3~svn800", "somesvnrev")) |
241 | - |
242 | - |
243 | -class TestUpstreamBranchVersion(TestCase): |
244 | - """Test that the upstream version of a branch can be determined correctly. |
245 | - """ |
246 | - |
247 | - def get_suffix(self, version_string, revid): |
248 | - revno = self.revhistory.index(revid)+1 |
249 | - if "bzr" in version_string: |
250 | - return "%sbzr%d" % (version_string.split("bzr")[0], revno) |
251 | - return "%s+bzr%d" % (version_string, revno) |
252 | - |
253 | - def test_snapshot_none_existing(self): |
254 | - self.revhistory = ["somerevid"] |
255 | - self.assertEquals(Version("1.2+bzr1"), |
256 | - _upstream_branch_version(self.revhistory, {}, "bla", "1.2", self.get_suffix)) |
257 | - |
258 | - def test_snapshot_nothing_new(self): |
259 | - self.revhistory = [] |
260 | - self.assertEquals(Version("1.2"), |
261 | - _upstream_branch_version(self.revhistory, {}, "bla", "1.2", self.get_suffix)) |
262 | - |
263 | - def test_new_tagged_release(self): |
264 | - """Last revision is tagged - use as upstream version.""" |
265 | - self.revhistory = ["somerevid"] |
266 | - self.assertEquals(Version("1.3"), |
267 | - _upstream_branch_version(self.revhistory, {"somerevid": ["1.3"]}, "bla", "1.2", self.get_suffix)) |
268 | - |
269 | - def test_refresh_snapshot_pre(self): |
270 | - self.revhistory = ["oldrevid", "somerevid"] |
271 | - self.assertEquals(Version("1.3~bzr2"), |
272 | - _upstream_branch_version(self.revhistory, {}, "bla", "1.3~bzr1", self.get_suffix)) |
273 | - |
274 | - def test_refresh_snapshot_post(self): |
275 | - self.revhistory = ["oldrevid", "somerevid"] |
276 | - self.assertEquals(Version("1.3+bzr2"), |
277 | - _upstream_branch_version(self.revhistory, {}, "bla", "1.3+bzr1", self.get_suffix)) |
278 | - |
279 | - def test_new_tag_refresh_snapshot(self): |
280 | - self.revhistory = ["oldrevid", "somerevid", "newrevid"] |
281 | - self.assertEquals(Version("1.3+bzr3"), |
282 | - _upstream_branch_version(self.revhistory, |
283 | - {"somerevid": ["1.3"]}, "bla", "1.2+bzr1", self.get_suffix)) |
284 | - |
285 | - |
286 | -class TestUpstreamTagToVersion(TestCase): |
287 | - |
288 | - def test_prefix(self): |
289 | - self.assertEquals(Version("5.0"), upstream_tag_to_version("upstream-5.0")) |
290 | - |
291 | - def test_gibberish(self): |
292 | - self.assertIs(None, upstream_tag_to_version("blabla")) |
293 | - |
294 | - def test_vprefix(self): |
295 | - self.assertEquals(Version("2.0"), upstream_tag_to_version("v2.0")) |
296 | - |
297 | - def test_plain(self): |
298 | - self.assertEquals(Version("2.0"), upstream_tag_to_version("2.0")) |
299 | - |
300 | - def test_package_prefix(self): |
301 | - self.assertEquals(Version("42.0"), upstream_tag_to_version("bla-42.0", "bla")) |
302 | - |
303 | + upstream_merge_changelog_line, |
304 | + package_version, |
305 | + ) |
306 | |
307 | class TestPackageVersion(TestCase): |
308 | |
309 | |
310 | === modified file 'tests/test_upstream.py' |
311 | --- tests/test_upstream.py 2011-01-24 05:46:47 +0000 |
312 | +++ tests/test_upstream.py 2011-01-28 23:09:00 +0000 |
313 | @@ -25,6 +25,9 @@ |
314 | |
315 | import os |
316 | |
317 | +from bzrlib.revision import ( |
318 | + Revision, |
319 | + ) |
320 | from bzrlib.tests import ( |
321 | TestCase, |
322 | TestCaseWithTransport, |
323 | @@ -40,11 +43,16 @@ |
324 | from bzrlib.plugins.builddeb.upstream import ( |
325 | AptSource, |
326 | StackedUpstreamSource, |
327 | - UpstreamBranchSource, |
328 | UpstreamProvider, |
329 | UScanSource, |
330 | Version, |
331 | ) |
332 | +from bzrlib.plugins.builddeb.upstream.branch import ( |
333 | + UpstreamBranchSource, |
334 | + _upstream_branch_version, |
335 | + upstream_tag_to_version, |
336 | + upstream_version_add_revision |
337 | + ) |
338 | |
339 | |
340 | class MockSources(object): |
341 | @@ -328,6 +336,111 @@ |
342 | self.assertEquals(revid1, source.version_as_revision("foo", "2.1")) |
343 | |
344 | |
345 | +class TestUpstreamBranchVersion(TestCase): |
346 | + """Test that the upstream version of a branch can be determined correctly. |
347 | + """ |
348 | + |
349 | + def get_suffix(self, version_string, revid): |
350 | + revno = self.revhistory.index(revid)+1 |
351 | + if "bzr" in version_string: |
352 | + return "%sbzr%d" % (version_string.split("bzr")[0], revno) |
353 | + return "%s+bzr%d" % (version_string, revno) |
354 | + |
355 | + def test_snapshot_none_existing(self): |
356 | + self.revhistory = ["somerevid"] |
357 | + self.assertEquals(Version("1.2+bzr1"), |
358 | + _upstream_branch_version(self.revhistory, {}, "bla", "1.2", self.get_suffix)) |
359 | + |
360 | + def test_snapshot_nothing_new(self): |
361 | + self.revhistory = [] |
362 | + self.assertEquals(Version("1.2"), |
363 | + _upstream_branch_version(self.revhistory, {}, "bla", "1.2", self.get_suffix)) |
364 | + |
365 | + def test_new_tagged_release(self): |
366 | + """Last revision is tagged - use as upstream version.""" |
367 | + self.revhistory = ["somerevid"] |
368 | + self.assertEquals(Version("1.3"), |
369 | + _upstream_branch_version(self.revhistory, {"somerevid": ["1.3"]}, "bla", "1.2", self.get_suffix)) |
370 | + |
371 | + def test_refresh_snapshot_pre(self): |
372 | + self.revhistory = ["oldrevid", "somerevid"] |
373 | + self.assertEquals(Version("1.3~bzr2"), |
374 | + _upstream_branch_version(self.revhistory, {}, "bla", "1.3~bzr1", self.get_suffix)) |
375 | + |
376 | + def test_refresh_snapshot_post(self): |
377 | + self.revhistory = ["oldrevid", "somerevid"] |
378 | + self.assertEquals(Version("1.3+bzr2"), |
379 | + _upstream_branch_version(self.revhistory, {}, "bla", "1.3+bzr1", self.get_suffix)) |
380 | + |
381 | + def test_new_tag_refresh_snapshot(self): |
382 | + self.revhistory = ["oldrevid", "somerevid", "newrevid"] |
383 | + self.assertEquals(Version("1.3+bzr3"), |
384 | + _upstream_branch_version(self.revhistory, |
385 | + {"somerevid": ["1.3"]}, "bla", "1.2+bzr1", self.get_suffix)) |
386 | + |
387 | + |
388 | +class TestUpstreamTagToVersion(TestCase): |
389 | + |
390 | + def test_prefix(self): |
391 | + self.assertEquals(Version("5.0"), upstream_tag_to_version("release-5.0")) |
392 | + |
393 | + def test_gibberish(self): |
394 | + self.assertIs(None, upstream_tag_to_version("blabla")) |
395 | + |
396 | + def test_vprefix(self): |
397 | + self.assertEquals(Version("2.0"), upstream_tag_to_version("v2.0")) |
398 | + |
399 | + def test_plain(self): |
400 | + self.assertEquals(Version("2.0"), upstream_tag_to_version("2.0")) |
401 | + |
402 | + def test_package_prefix(self): |
403 | + self.assertEquals(Version("42.0"), upstream_tag_to_version("bla-42.0", "bla")) |
404 | + |
405 | + |
406 | +class TestUpstreamVersionAddRevision(TestCaseWithTransport): |
407 | + """Test that updating the version string works.""" |
408 | + |
409 | + def setUp(self): |
410 | + super(TestUpstreamVersionAddRevision, self).setUp() |
411 | + self.revnos = {} |
412 | + self.svn_revnos = {"somesvnrev": 45} |
413 | + self.revnos = {"somerev": 42, "somesvnrev": 12} |
414 | + self.repository = self |
415 | + |
416 | + def revision_id_to_revno(self, revid): |
417 | + return self.revnos[revid] |
418 | + |
419 | + def get_revision(self, revid): |
420 | + rev = Revision(revid) |
421 | + if revid in self.svn_revnos: |
422 | + rev.foreign_revid = ("uuid", "bp", self.svn_revnos[revid]) |
423 | + return rev |
424 | + |
425 | + def test_update_plus_rev(self): |
426 | + self.assertEquals("1.3+bzr42", |
427 | + upstream_version_add_revision(self, "1.3+bzr23", "somerev")) |
428 | + |
429 | + def test_update_tilde_rev(self): |
430 | + self.assertEquals("1.3~bzr42", |
431 | + upstream_version_add_revision(self, "1.3~bzr23", "somerev")) |
432 | + |
433 | + def test_new_rev(self): |
434 | + self.assertEquals("1.3+bzr42", |
435 | + upstream_version_add_revision(self, "1.3", "somerev")) |
436 | + |
437 | + def test_svn_new_rev(self): |
438 | + self.assertEquals("1.3+svn45", |
439 | + upstream_version_add_revision(self, "1.3", "somesvnrev")) |
440 | + |
441 | + def test_svn_plus_rev(self): |
442 | + self.assertEquals("1.3+svn45", |
443 | + upstream_version_add_revision(self, "1.3+svn3", "somesvnrev")) |
444 | + |
445 | + def test_svn_tilde_rev(self): |
446 | + self.assertEquals("1.3~svn45", |
447 | + upstream_version_add_revision(self, "1.3~svn800", "somesvnrev")) |
448 | + |
449 | + |
450 | class _MissingUpstreamProvider(UpstreamProvider): |
451 | """For tests""" |
452 | |
453 | |
454 | === added directory 'upstream' |
455 | === renamed file 'upstream.py' => 'upstream/__init__.py' |
456 | --- upstream.py 2011-01-28 18:17:39 +0000 |
457 | +++ upstream/__init__.py 2011-01-28 23:09:00 +0000 |
458 | @@ -30,7 +30,6 @@ |
459 | # Prior to 0.1.15 the debian module was called debian_bundle |
460 | from debian_bundle.changelog import Version |
461 | |
462 | -from bzrlib.revisionspec import RevisionSpec |
463 | from bzrlib.trace import ( |
464 | note, |
465 | warning, |
466 | @@ -47,7 +46,6 @@ |
467 | from bzrlib.plugins.builddeb.repack_tarball import repack_tarball |
468 | from bzrlib.plugins.builddeb.util import ( |
469 | export, |
470 | - get_snapshot_revision, |
471 | tarball_name, |
472 | ) |
473 | |
474 | @@ -168,63 +166,6 @@ |
475 | return True |
476 | |
477 | |
478 | -class UpstreamBranchSource(UpstreamSource): |
479 | - """Upstream source that uses the upstream branch. |
480 | - |
481 | - :ivar upstream_branch: Branch with upstream sources |
482 | - :ivar upstream_version_map: Map from version strings to revids |
483 | - """ |
484 | - |
485 | - def __init__(self, upstream_branch, upstream_revision_map=None, |
486 | - config=None): |
487 | - self.upstream_branch = upstream_branch |
488 | - self.config = config |
489 | - if upstream_revision_map is None: |
490 | - self.upstream_revision_map = {} |
491 | - else: |
492 | - self.upstream_revision_map = upstream_revision_map |
493 | - |
494 | - def version_as_revision(self, package, version): |
495 | - if version in self.upstream_revision_map: |
496 | - return self.upstream_revision_map[version] |
497 | - revspec = get_snapshot_revision(version) |
498 | - if revspec is not None: |
499 | - return RevisionSpec.from_string( |
500 | - revspec).as_revision_id(self.upstream_branch) |
501 | - return None |
502 | - |
503 | - def get_latest_version(self, package, current_version): |
504 | - return self.get_version(package, current_version, |
505 | - self.upstream_branch.last_revision()) |
506 | - |
507 | - def get_version(self, package, current_version, revision): |
508 | - from bzrlib.plugins.builddeb.merge_upstream import ( |
509 | - upstream_branch_version) |
510 | - version = str(upstream_branch_version(self.upstream_branch, |
511 | - revision, package, current_version)) |
512 | - return version |
513 | - |
514 | - def fetch_tarball(self, package, version, target_dir): |
515 | - self.upstream_branch.lock_read() |
516 | - try: |
517 | - revid = self.version_as_revision(package, version) |
518 | - if revid is None: |
519 | - raise PackageVersionNotPresent(package, version, self) |
520 | - note("Exporting upstream branch revision %s to create the tarball", |
521 | - revid) |
522 | - target_filename = self._tarball_path(package, version, target_dir) |
523 | - tarball_base = "%s-%s" % (package, version) |
524 | - rev_tree = self.upstream_branch.repository.revision_tree(revid) |
525 | - export(rev_tree, target_filename, 'tgz', tarball_base) |
526 | - finally: |
527 | - self.upstream_branch.unlock() |
528 | - return target_filename |
529 | - |
530 | - def __repr__(self): |
531 | - return "<%s for %r>" % (self.__class__.__name__, |
532 | - self.upstream_branch.base) |
533 | - |
534 | - |
535 | class GetOrigSourceSource(UpstreamSource): |
536 | """Upstream source that uses the get-orig-source rule in debian/rules.""" |
537 | |
538 | |
539 | === added file 'upstream/branch.py' |
540 | --- upstream/branch.py 1970-01-01 00:00:00 +0000 |
541 | +++ upstream/branch.py 2011-01-28 23:09:00 +0000 |
542 | @@ -0,0 +1,219 @@ |
543 | +# upstream/branch.py -- Upstream branch source provider |
544 | +# Copyright (C) 2010-2011 Canonical Ltd. |
545 | +# Copyright (C) 2009 Jelmer Vernooij |
546 | +# |
547 | +# This file is part of bzr-builddeb. |
548 | +# |
549 | +# bzr-builddeb is free software; you can redistribute it and/or modify |
550 | +# it under the terms of the GNU General Public License as published by |
551 | +# the Free Software Foundation; either version 2 of the License, or |
552 | +# (at your option) any later version. |
553 | +# |
554 | +# bzr-builddeb is distributed in the hope that it will be useful, |
555 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
556 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
557 | +# GNU General Public License for more details. |
558 | +# |
559 | +# You should have received a copy of the GNU General Public License |
560 | +# along with bzr-builddeb; if not, write to the Free Software |
561 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
562 | + |
563 | +from bzrlib.errors import InvalidRevisionId |
564 | +from bzrlib.revisionspec import RevisionSpec |
565 | +from bzrlib.trace import note |
566 | + |
567 | +from bzrlib.plugins.builddeb.errors import PackageVersionNotPresent |
568 | +from bzrlib.plugins.builddeb.upstream import UpstreamSource |
569 | +from bzrlib.plugins.builddeb.util import ( |
570 | + export, |
571 | + get_snapshot_revision, |
572 | + ) |
573 | + |
574 | + |
575 | +def upstream_tag_to_version(tag_name, package=None): |
576 | + """Take a tag name and return the upstream version, or None.""" |
577 | + if (package is not None and ( |
578 | + tag_name.startswith("%s-" % package) or |
579 | + tag_name.startswith("%s_" % package))): |
580 | + return tag_name[len(package)+1:] |
581 | + if tag_name.startswith("release-"): |
582 | + return tag_name[len("release-"):] |
583 | + if tag_name[0] == "v" and tag_name[1].isdigit(): |
584 | + return tag_name[1:] |
585 | + if all([c.isdigit() or c in (".", "~") for c in tag_name]): |
586 | + return tag_name |
587 | + return None |
588 | + |
589 | + |
590 | +def _upstream_branch_version(revhistory, reverse_tag_dict, package, |
591 | + previous_version, add_rev): |
592 | + """Determine the version string of an upstream branch. |
593 | + |
594 | + The upstream version is determined from the most recent tag |
595 | + in the upstream branch. If that tag does not point at the last revision, |
596 | + the revision number is added to it (<version>+bzr<revno>). |
597 | + |
598 | + If there are no tags set on the upstream branch, the previous Debian |
599 | + version is used and combined with the bzr revision number |
600 | + (usually <version>+bzr<revno>). |
601 | + |
602 | + :param revhistory: Branch revision history. |
603 | + :param reverse_tag_dict: Reverse tag dictionary (revid -> list of tags) |
604 | + :param package: Name of package. |
605 | + :param previous_version: Previous upstream version in debian changelog. |
606 | + :param add_rev: Function that can add a revision suffix to a version string. |
607 | + :return: Name of the upstream revision. |
608 | + """ |
609 | + if revhistory == []: |
610 | + # No new version to merge |
611 | + return previous_version |
612 | + for r in reversed(revhistory): |
613 | + if r in reverse_tag_dict: |
614 | + # If there is a newer version tagged in branch, |
615 | + # convert to upstream version |
616 | + # return <upstream_version>+bzr<revno> |
617 | + for tag in reverse_tag_dict[r]: |
618 | + upstream_version = upstream_tag_to_version(tag, |
619 | + package=package) |
620 | + if upstream_version is not None: |
621 | + if r != revhistory[-1]: |
622 | + upstream_version = add_rev( |
623 | + upstream_version, revhistory[-1]) |
624 | + return upstream_version |
625 | + return add_rev(previous_version, revhistory[-1]) |
626 | + |
627 | + |
628 | +def extract_svn_revno(rev): |
629 | + """Extract the Subversion number of a revision from a revision. |
630 | + |
631 | + :param rev: Revision object |
632 | + :return: Revision number, None if this was not a Subversion revision or |
633 | + if the revision number could not be determined (bzr-svn not available). |
634 | + """ |
635 | + try: |
636 | + from bzrlib.plugins.svn import extract_svn_foreign_revid |
637 | + except ImportError: |
638 | + # No svn support |
639 | + return None |
640 | + else: |
641 | + try: |
642 | + (svn_uuid, branch_path, svn_revno) = extract_svn_foreign_revid(rev) |
643 | + except InvalidRevisionId: |
644 | + return None |
645 | + else: |
646 | + return svn_revno |
647 | + |
648 | + |
649 | +def upstream_version_add_revision(upstream_branch, version_string, revid): |
650 | + """Update the revision in a upstream version string. |
651 | + |
652 | + :param branch: Branch in which the revision can be found |
653 | + :param version_string: Original version string |
654 | + :param revid: Revision id of the revision |
655 | + """ |
656 | + revno = upstream_branch.revision_id_to_revno(revid) |
657 | + |
658 | + if "+bzr" in version_string: |
659 | + return "%s+bzr%d" % (version_string[:version_string.rfind("+bzr")], revno) |
660 | + |
661 | + if "~bzr" in version_string: |
662 | + return "%s~bzr%d" % (version_string[:version_string.rfind("~bzr")], revno) |
663 | + |
664 | + rev = upstream_branch.repository.get_revision(revid) |
665 | + svn_revno = extract_svn_revno(rev) |
666 | + |
667 | + # FIXME: Raise error if +svn/~svn is present and svn_revno is not set? |
668 | + if "+svn" in version_string and svn_revno: |
669 | + return "%s+svn%d" % (version_string[:version_string.rfind("+svn")], svn_revno) |
670 | + if "~svn" in version_string and svn_revno: |
671 | + return "%s~svn%d" % (version_string[:version_string.rfind("~svn")], svn_revno) |
672 | + |
673 | + if svn_revno: |
674 | + return "%s+svn%d" % (version_string, svn_revno) |
675 | + else: |
676 | + return "%s+bzr%d" % (version_string, revno) |
677 | + |
678 | + |
679 | +def upstream_branch_version(upstream_branch, upstream_revision, package, |
680 | + previous_version): |
681 | + """Determine the version string for a revision in an upstream branch. |
682 | + |
683 | + :param upstream_branch: The upstream branch object |
684 | + :param upstream_revision: The revision id of the upstream revision |
685 | + :param package: The name of the package |
686 | + :param previous_version: The previous upstream version string |
687 | + :return: Upstream version string for `upstream_revision`. |
688 | + """ |
689 | + dotted_revno = upstream_branch.revision_id_to_dotted_revno(upstream_revision) |
690 | + if len(dotted_revno) > 1: |
691 | + revno = -2 |
692 | + else: |
693 | + revno = dotted_revno[0] |
694 | + revhistory = upstream_branch.revision_history() |
695 | + previous_revision = get_snapshot_revision(previous_version) |
696 | + if previous_revision is not None: |
697 | + previous_revspec = RevisionSpec.from_string(previous_revision) |
698 | + previous_revno, _ = previous_revspec.in_history(upstream_branch) |
699 | + # Trim revision history - we don't care about any revisions |
700 | + # before the revision of the previous version |
701 | + else: |
702 | + previous_revno = 0 |
703 | + revhistory = revhistory[previous_revno:revno+1] |
704 | + return _upstream_branch_version(revhistory, |
705 | + upstream_branch.tags.get_reverse_tag_dict(), package, |
706 | + previous_version, |
707 | + lambda version, revision: upstream_version_add_revision(upstream_branch, version, revision)) |
708 | + |
709 | + |
710 | +class UpstreamBranchSource(UpstreamSource): |
711 | + """Upstream source that uses the upstream branch. |
712 | + |
713 | + :ivar upstream_branch: Branch with upstream sources |
714 | + :ivar upstream_version_map: Map from version strings to revids |
715 | + """ |
716 | + |
717 | + def __init__(self, upstream_branch, upstream_revision_map=None, |
718 | + config=None): |
719 | + self.upstream_branch = upstream_branch |
720 | + self.config = config |
721 | + if upstream_revision_map is None: |
722 | + self.upstream_revision_map = {} |
723 | + else: |
724 | + self.upstream_revision_map = upstream_revision_map |
725 | + |
726 | + def version_as_revision(self, package, version): |
727 | + if version in self.upstream_revision_map: |
728 | + return self.upstream_revision_map[version] |
729 | + revspec = get_snapshot_revision(version) |
730 | + if revspec is not None: |
731 | + return RevisionSpec.from_string( |
732 | + revspec).as_revision_id(self.upstream_branch) |
733 | + return None |
734 | + |
735 | + def get_latest_version(self, package, current_version): |
736 | + return self.get_version(package, current_version, |
737 | + self.upstream_branch.last_revision()) |
738 | + |
739 | + def get_version(self, package, current_version, revision): |
740 | + return upstream_branch_version(self.upstream_branch, |
741 | + revision, package, current_version) |
742 | + |
743 | + def fetch_tarball(self, package, version, target_dir): |
744 | + self.upstream_branch.lock_read() |
745 | + try: |
746 | + revid = self.version_as_revision(package, version) |
747 | + if revid is None: |
748 | + raise PackageVersionNotPresent(package, version, self) |
749 | + note("Exporting upstream branch revision %s to create the tarball", |
750 | + revid) |
751 | + target_filename = self._tarball_path(package, version, target_dir) |
752 | + tarball_base = "%s-%s" % (package, version) |
753 | + rev_tree = self.upstream_branch.repository.revision_tree(revid) |
754 | + export(rev_tree, target_filename, 'tgz', tarball_base) |
755 | + finally: |
756 | + self.upstream_branch.unlock() |
757 | + return target_filename |
758 | + |
759 | + def __repr__(self): |
760 | + return "<%s for %r>" % (self.__class__.__name__, |
761 | + self.upstream_branch.base) |