Guido Günther <a...@sigxcpu.org> writes: > On Sat, May 19, 2012 at 06:27:26PM +0200, Daniel Dehennin wrote: >> Hello,
Hello, > I think it's almost ready to go. Why not add it? We could extend it to > keep track of the changelog contents after newly added sections, etc. > Cheers, Here[1] is a new version of the patch. The path is splited in 3 parts, nosetests are OK at each steps: 1. Add guess_version_from_upstream() to gbp.deb.git.DebianGitRepository: this is required for next patch 2. Add new methods to gbp.deb.changelog.ChangeLog: they are adapted version of gbp.scripts.dch ones 3. Update gbp.scripts.dch to use new methods. Regards. Pull request: ============= The following changes since commit a116edd739e9ff529058a2d9df6fe67bdb17670d: Refactor deb helpers: move PristineTar class (2012-05-25 10:45:13 +0200) are available in the git repository at: git://git.baby-gnu.net/git-buildpackage.git tags/dad/move-spawn_dch-to-ChangeLog/rebased/on-a116edd for you to fetch changes up to 5f6c498e0454c1f0f91824ddcf6879a35be44c08: Convert gbp.scripts.dch to gbp.deb.changelog.ChangeLog method calls. (2012-05-30 21:53:22 +0200) ---------------------------------------------------------------- New version on latest experimental. The path is splited in 3 parts, nosetests are OK at each steps: 1. Add guess_version_from_upstream() to gbp.deb.git.DebianGitRepository: this is required for next patch 2. Add new methods to gbp.deb.changelog.ChangeLog: they are adapted version of gbp.scripts.dch ones 3. Update gbp.scripts.dch to use new methods. ---------------------------------------------------------------- Daniel Dehennin (3): Add guess_version_from_upstream() to gbp.deb.git.DebianGitRepository. Add spawn_dch(), add_changelog_entry() and add_changelog_section() to gbp.deb.changelog.ChangeLog. Convert gbp.scripts.dch to gbp.deb.changelog.ChangeLog method calls. gbp/deb/changelog.py | 113 ++++++++++++++++ gbp/deb/git.py | 17 +++ gbp/scripts/dch.py | 138 +++----------------- ...anGitRepository_guess_version_from_upstream.py} | 11 +- 4 files changed, 153 insertions(+), 126 deletions(-) rename tests/{03_test_dch_guess_version.py => 03_test_DebianGitRepository_guess_version_from_upstream.py} (76%) Patches: ======== From 0d177f45fc429e464cc5f6128b39e44318da9710 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin <daniel.dehen...@baby-gnu.org> Date: Wed, 30 May 2012 21:12:41 +0200 Subject: [PATCH 1/3] Add guess_version_from_upstream() to gbp.deb.git.DebianGitRepository. * gbp/deb/git.py: Update imports. (guess_version_from_upstream): New method adapted from gbp.scripts.dch. Remove the logging. Caller is responsible of handling possible GitRepositoryError exception. * tests/03_test_DebianGitRepository_guess_version_from_upstream.py: Remove useless import. (MockGitRepository): Inherit from DebianGitRepository. (TestGuessVersionFromUpstream.test_guess_no_epoch): Convert calls of dch.guess_version_from_upstream() to DebianGitRepository version. (TestGuessVersionFromUpstream.test_guess_epoch): Ditoo. --- gbp/deb/git.py | 17 +++++++ ...ianGitRepository_guess_version_from_upstream.py | 49 ++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/03_test_DebianGitRepository_guess_version_from_upstream.py diff --git a/gbp/deb/git.py b/gbp/deb/git.py index 7e13b5e..2d2dc18 100644 --- a/gbp/deb/git.py +++ b/gbp/deb/git.py @@ -19,6 +19,7 @@ import re from gbp.git import GitRepository, GitRepositoryError from gbp.deb.pristinetar import DebianPristineTar +from gbp.deb import compare_versions class DebianGitRepository(GitRepository): """A git repository that holds the source of a Debian package""" @@ -117,6 +118,22 @@ class DebianGitRepository(GitRepository): return version return None + def guess_version_from_upstream(self, upstream_tag_format, cp): + """ + Guess the version based on the latest version on the upstream branch + """ + pattern = upstream_tag_format % dict(version='*') + tag = self.find_tag('HEAD', pattern=pattern) + version = self.tag_to_version(tag, upstream_tag_format) + if version: + if cp == None: + return "%s-1" % version + if cp.has_epoch(): + version = "%s:%s" % (cp.epoch, version) + if compare_versions(version, cp.version) > 0: + return "%s-1" % version + return None + @property def pristine_tar_branch(self): """ diff --git a/tests/03_test_DebianGitRepository_guess_version_from_upstream.py b/tests/03_test_DebianGitRepository_guess_version_from_upstream.py new file mode 100644 index 0000000..ba80a3b --- /dev/null +++ b/tests/03_test_DebianGitRepository_guess_version_from_upstream.py @@ -0,0 +1,49 @@ +# vim: set fileencoding=utf-8 : + +"""Test L{Changelog}'s guess_version_from_upstream""" + +import unittest + +from gbp.errors import GbpError +from gbp.deb.changelog import ChangeLog +from gbp.deb.git import DebianGitRepository + +class MockGitRepository(DebianGitRepository): + def __init__(self, upstream_tag): + self.upstream_tag = upstream_tag + + def find_tag(self, branch, pattern): + return self.upstream_tag + + def tag_to_version(self, tag, format): + return DebianGitRepository.tag_to_version(tag, format) + + +class MockedChangeLog(ChangeLog): + contents = """foo (%s) experimental; urgency=low + + * a important change + + -- Debian Maintainer <ma...@debian.org> Sat, 01 Jan 2012 00:00:00 +0100""" + + def __init__(self, version): + ChangeLog.__init__(self, contents=self.contents % version) + + +class TestGuessVersionFromUpstream(unittest.TestCase): + """Test guess_version_from_upstream""" + def test_guess_no_epoch(self): + """Guess the new version from the upstream tag""" + repo = MockGitRepository(upstream_tag='upstream/1.1') + cp = MockedChangeLog('1.0-1') + guessed = repo.guess_version_from_upstream('upstream/%(version)s', cp) + self.assertEqual('1.1-1', guessed) + + def test_guess_epoch(self): + """Check if we picked up the epoch correctly (#652366)""" + repo = MockGitRepository(upstream_tag='upstream/1.1') + cp = MockedChangeLog('1:1.0-1') + guessed = repo.guess_version_from_upstream('upstream/%(version)s', cp) + self.assertEqual('1:1.1-1', guessed) + + -- 1.7.10 From a58c9589c8d0e90046737ada0ee281301bc84e6d Mon Sep 17 00:00:00 2001 From: Daniel Dehennin <daniel.dehen...@baby-gnu.org> Date: Wed, 30 May 2012 21:30:45 +0200 Subject: [PATCH 2/3] Add spawn_dch(), add_changelog_entry() and add_changelog_section() to gbp.deb.changelog.ChangeLog. spawn_dch use gbp.command.wrappers.Command. * gbp/deb/changelog.py (ChangeLog.spawn_dch): static method adapted from gbp.scripts.dch and converted to gbp.command_wrappers.Command. (add_entry): New method adapted from gbp.scripts.dch.add_changelog_entry. (add_section): New method adapted from gbp.scripts.dch.add_changelog_entry. Replace options argument by direct upstream_tag_format. Use gbp.deb.git.DebianGitRepository version of guess_version_from_upstream() through repo argument. --- gbp/deb/changelog.py | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/gbp/deb/changelog.py b/gbp/deb/changelog.py index 0f9bace..37012cf 100644 --- a/gbp/deb/changelog.py +++ b/gbp/deb/changelog.py @@ -19,6 +19,7 @@ import email import os import subprocess +from gbp.command_wrappers import Command class NoChangeLogError(Exception): """No changelog found""" @@ -214,3 +215,115 @@ class ChangeLog(object): Get sections in the changelog """ return list(self.sections_iter) + + @staticmethod + def spawn_dch(msg=[], author=None, email=None, newversion=False, version=None, + release=False, distribution=None, dch_options=[]): + """ + Spawn dch + + @param author: committers name + @type author: C{str} + @param email: committers email + @type email: C{str} + @param newversion: start a new version + @type newversion: C{bool} + @param version: the verion to use + @type version: C{str} + @param release: finalize changelog for releaze + @type release: C{bool} + @param distribution: distribution to use + @type distribution: C{str} + @param dch_options: options passed verbatim to dch + @type dch_options: C{list} + @return: return code of dch subprocess + @rtype: C{int} + """ + distopt = "" + versionopt = "" + env = {} + args = ['--no-auto-nmu'] + if newversion: + if version: + try: + args.append(version['increment']) + except KeyError: + args.append('--newversion=%s' % version['version']) + else: + args.append('-i') + elif release: + args.extend(["--release", "--no-force-save-on-release"]) + msg = None + + if author and email: + env = {'DEBFULLNAME': author, 'DEBEMAIL': email} + + if distribution: + args.append("--distribution=%s" % distribution) + + args.extend(dch_options) + args.append('--') + if msg: + args.append('[[[insert-git-dch-commit-message-here]]]') + else: + args.append('') + dch = Command('dch', args, extra_env=env) + dch.call([]) + if msg: + old_cl = open("debian/changelog", "r") + new_cl = open("debian/changelog.bak", "w") + for line in old_cl: + if line == " * [[[insert-git-dch-commit-message-here]]]\n": + print >> new_cl, " * " + msg[0] + for line in msg[1:]: + print >> new_cl, " " + line + else: + print >> new_cl, line, + os.rename("debian/changelog.bak", "debian/changelog") + + def add_entry(self, msg, author=None, email=None, dch_options=[]): + """Add a single changelog entry + + @param msg: log message to add + @type msg: C{str} + @param author: name of the author of the log message + @type author: C{str} + @param email: email of the author of the log message + @type email: C{str} + @param dch_options: options passed verbatim to dch + @type dch_options: C{list} + @return: return code of dch subprocess + @rtype: C{int} + """ + self.spawn_dch(msg=msg, author=author, email=email, dch_options=dch_options) + + def add_section(self, msg, distribution, repo, upstream_tag_format='', + author=None, email=None, version={}, dch_options=[]): + """Add a new section to the changelog + + @param msg: log message to add + @type msg: C{str} + @param distribution: distribution to set for the new changelog entry + @type distribution: C{str} + @param repo: git repository we are operating on + @type repo: C{gbp.deb.git.DebianRepository} + @param upstream_tag_format: format use to find upstream tag + @type upstream_tag_format: C{str} + @param author: name of the author of the log message + @type author: C{str} + @param email: email of the author of the log message + @type email: C{str} + @param version: version to set for the new changelog entry + @param version: C{dict} + @param dch_options: options passed verbatim to dch + @type dch_options: C{list} + @return: return code of dch subprocess + @rtype: C{int} + """ + if not version and not self.is_native(): + v = repo.guess_version_from_upstream(upstream_tag_format, self) + if v: + version['version'] = v + self.spawn_dch(msg=msg, newversion=True, version=version, author=author, + email=email, distribution=distribution, dch_options=dch_options) + -- 1.7.10 From 5f6c498e0454c1f0f91824ddcf6879a35be44c08 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin <daniel.dehen...@baby-gnu.org> Date: Wed, 30 May 2012 21:39:51 +0200 Subject: [PATCH 3/3] Convert gbp.scripts.dch to gbp.deb.changelog.ChangeLog method calls. * gbp/scripts/dch.py: compare_version became useless. Remove useless functions: system(), spawn_dch(), add_changelog_section(), add_changelog_entry() and guess_version_from_upstream(). Update calls accordingly. (fixup_trailer): Use spawn_dch() method of ChangeLog class. (process_options): dch_options became a list. (main): Use add_section() and add_entry() methods of ChangeLog object. Update exception handling, ChangeLog.spawn_dch() can raise "CommandExecFailed" exception. * tests/03_test_dch_guess_version.py: No more gbp.scripts.dch.guess_version_from_upstream() to test. --- gbp/scripts/dch.py | 138 ++++++------------------------------ tests/03_test_dch_guess_version.py | 54 -------------- 2 files changed, 20 insertions(+), 172 deletions(-) delete mode 100644 tests/03_test_dch_guess_version.py diff --git a/gbp/scripts/dch.py b/gbp/scripts/dch.py index ba96631..34e0a00 100644 --- a/gbp/scripts/dch.py +++ b/gbp/scripts/dch.py @@ -28,7 +28,6 @@ import gbp.dch as dch import gbp.log from gbp.config import GbpOptionParserDebian, GbpOptionGroup from gbp.errors import GbpError -from gbp.deb import compare_versions from gbp.deb.git import GitRepositoryError, DebianGitRepository from gbp.deb.changelog import ChangeLog, NoChangeLogError @@ -36,102 +35,6 @@ user_customizations = {} snapshot_re = re.compile("\s*\*\* SNAPSHOT build @(?P<commit>[a-z0-9]+)\s+\*\*") -def system(cmd): - try: - gbpc.Command(cmd, shell=True)() - except gbpc.CommandExecFailed: - raise GbpError - - -def spawn_dch(msg=[], author=None, email=None, newversion=False, version=None, - release=False, distribution=None, dch_options=''): - """ - Spawn dch - - @param author: committers name - @param email: committers email - @param newversion: start a new version - @param version: the verion to use - @param release: finalize changelog for releaze - @param distribution: distribution to use - @param dch_options: options passed verbatim to dch - """ - distopt = "" - versionopt = "" - env = "" - - if newversion: - if version: - try: - versionopt = version['increment'] - except KeyError: - versionopt = '--newversion=%s' % version['version'] - else: - versionopt = '-i' - elif release: - versionopt = "--release --no-force-save-on-release" - msg = None - - if author and email: - env = """DEBFULLNAME="%s" DEBEMAIL="%s" """ % (author, email) - - if distribution: - distopt = "--distribution=%s" % distribution - - cmd = '%(env)s dch --no-auto-nmu %(distopt)s %(versionopt)s %(dch_options)s ' % locals() - if msg: - cmd += '-- "[[[insert-git-dch-commit-message-here]]]"' - else: - cmd += '-- ""' - system(cmd) - if msg: - old_cl = open("debian/changelog", "r") - new_cl = open("debian/changelog.bak", "w") - for line in old_cl: - if line == " * [[[insert-git-dch-commit-message-here]]]\n": - print >> new_cl, " * " + msg[0] - for line in msg[1:]: - print >> new_cl, " " + line - else: - print >> new_cl, line, - os.rename("debian/changelog.bak", "debian/changelog") - - -def add_changelog_entry(msg, author, email, dch_options): - """Add a single changelog entry""" - spawn_dch(msg=msg, author=author, email=email, dch_options=dch_options) - - -def guess_version_from_upstream(repo, upstream_tag_format, cp): - """ - Guess the version based on the latest version on the upstream branch - """ - pattern = upstream_tag_format % dict(version='*') - try: - tag = repo.find_tag('HEAD', pattern=pattern) - version = repo.tag_to_version(tag, upstream_tag_format) - if version: - gbp.log.debug("Found upstream version %s." % version) - if cp.has_epoch(): - version = "%s:%s" % (cp.epoch, version) - if compare_versions(version, cp.version) > 0: - return "%s-1" % version - except GitRepositoryError: - gbp.log.debug("No tag found matching pattern %s." % pattern) - return None - - -def add_changelog_section(msg, distribution, repo, options, cp, - author=None, email=None, version={}, dch_options=''): - """Add a new section to the changelog""" - if not version and not cp.is_native(): - v = guess_version_from_upstream(repo, options.upstream_tag, cp) - if v: - version['version'] = v - spawn_dch(msg=msg, newversion=True, version=version, author=author, - email=email, distribution=distribution, dch_options=dch_options) - - def get_author_email(repo, use_git_config): """Get author and email from git configuration""" author = email = None @@ -153,7 +56,7 @@ def fixup_trailer(repo, git_author, dch_options): creating the changelog """ author, email = get_author_email(repo, git_author) - spawn_dch(msg='', author=author, email=email, dch_options=dch_options) + ChangeLog.spawn_dch(msg='', author=author, email=email, dch_options=dch_options) def snapshot_version(version): @@ -294,15 +197,16 @@ def process_options(options, parser): if options.since and options.auto: parser.error("'--since' and '--auto' are incompatible options") + dch_options = [] if options.multimaint_merge: - dch_options = "--multimaint-merge" + dch_options.append("--multimaint-merge") else: - dch_options = "--nomultimaint-merge" + dch_options.append("--nomultimaint-merge") if options.multimaint: - dch_options += " --multimaint" + dch_options.append(" --multimaint") else: - dch_options += " --nomultimaint" + dch_options.append(" --nomultimaint") get_customizations(options.customization_file) return dch_options @@ -485,18 +389,17 @@ def main(argv): if add_section: # Add a section containing just this message (we can't # add an empty section with dch) - add_changelog_section(distribution="UNRELEASED", msg=commit_msg, - version=version_change, - author=commit_author, - email=commit_email, - dch_options=dch_options, - repo=repo, - options=options, - cp=cp) + cp.add_section(distribution="UNRELEASED", msg=commit_msg, + version=version_change, + author=commit_author, + email=commit_email, + dch_options=dch_options, + repo=repo, + upstream_tag_format=options.upstream_tag) # Adding a section only needs to happen once. add_section = False else: - add_changelog_entry(commit_msg, commit_author, commit_email, dch_options) + cp.add_entry(commit_msg, commit_author, commit_email, dch_options) # Show a message if there were no commits (not even ignored @@ -507,12 +410,11 @@ def main(argv): if add_section: # If we end up here, then there were no commits to include, # so we put a dummy message in the new section. - add_changelog_section(distribution="UNRELEASED", msg=["UNRELEASED"], - version=version_change, - dch_options=dch_options, - repo=repo, - options=options, - cp=cp) + cp.add_section(distribution="UNRELEASED", msg=["UNRELEASED"], + version=version_change, + dch_options=dch_options, + repo=repo, + upstream_tag_format=options.upstream_tag) fixup_trailer(repo, git_author=options.git_author, dch_options=dch_options) @@ -537,7 +439,7 @@ def main(argv): repo.commit_files([changelog], msg) gbp.log.info("Changelog has been committed for version %s" % version) - except (GbpError, GitRepositoryError, NoChangeLogError), err: + except (CommandExecFailed, GbpError, GitRepositoryError, NoChangeLogError), err: if len(err.__str__()): gbp.log.err(err) ret = 1 diff --git a/tests/03_test_dch_guess_version.py b/tests/03_test_dch_guess_version.py deleted file mode 100644 index d954459..0000000 --- a/tests/03_test_dch_guess_version.py +++ /dev/null @@ -1,54 +0,0 @@ -# vim: set fileencoding=utf-8 : - -"""Test L{Changelog}'s guess_version_from_upstream""" - -import unittest - -from gbp.scripts import dch -from gbp.errors import GbpError -from gbp.deb.changelog import ChangeLog -from gbp.deb.git import DebianGitRepository - -class MockGitRepository(object): - def __init__(self, upstream_tag): - self.upstream_tag = upstream_tag - - def find_tag(self, branch, pattern): - return self.upstream_tag - - def tag_to_version(self, tag, format): - return DebianGitRepository.tag_to_version(tag, format) - - -class MockedChangeLog(ChangeLog): - contents = """foo (%s) experimental; urgency=low - - * a important change - - -- Debian Maintainer <ma...@debian.org> Sat, 01 Jan 2012 00:00:00 +0100""" - - def __init__(self, version): - ChangeLog.__init__(self, contents=self.contents % version) - - -class TestGuessVersionFromUpstream(unittest.TestCase): - """Test guess_version_from_upstream""" - def test_guess_no_epoch(self): - """Guess the new version from the upstream tag""" - repo = MockGitRepository(upstream_tag='upstream/1.1') - cp = MockedChangeLog('1.0-1') - guessed = dch.guess_version_from_upstream(repo, - 'upstream/%(version)s', - cp) - self.assertEqual('1.1-1', guessed) - - def test_guess_epoch(self): - """Check if we picked up the epoch correctly (#652366)""" - repo = MockGitRepository(upstream_tag='upstream/1.1') - cp = MockedChangeLog('1:1.0-1') - guessed = dch.guess_version_from_upstream(repo, - 'upstream/%(version)s', - cp) - self.assertEqual('1:1.1-1', guessed) - - -- 1.7.10 Footnotes: [1] http://git.baby-gnu.net/gitweb/?p=git-buildpackage.git;a=tag;h=985f5696da99be0ecfb9242aafcdc27f1f99c3af -- Daniel Dehennin Récupérer ma clef GPG: gpg --keyserver pgp.mit.edu --recv-keys 0x7A6FE2DF
pgp5iJGnUs40e.pgp
Description: PGP signature