Hi Daniel,

sorry for the delay. Real life keeps interfering.

On Thu, May 03, 2012 at 11:40:30AM +0200, Daniel Dehennin wrote:
> Guido Günther <a...@sigxcpu.org> writes:
> 
> > Hi Daniel,
> > thanks for your patch. Some more comments below:
> 
> I inline an updated version with some tests.
> 
> I still have one trouble, when no previous changelog was in the
> repository, dch.py raise "GbpError, "Version %s not found" %
> cp['Version']" from line 448.
> 
> It's triggered by test_create_from_dch_main and
> test_create_from_dch_main_with_auto.
> 
> The version info is irrelevant in that case:
> 
> - We may still need to figure out if the changelog was just created?
> 
> - We may look for last modification under the "debian/" directory?
> 
> - We may set "since" to the fist commit?
> 
> - We may set "since" to the last merged upstream version?
> 
> Regards.
> 

> From db8138f40e2e79ffd05c59bc2dc6c47360014515 Mon Sep 17 00:00:00 2001
> From: Daniel Dehennin <daniel.dehen...@baby-gnu.org>
> Date: Thu, 3 May 2012 08:32:20 +0200
> Subject: [PATCH] Create debian/changelog if it does not exist.
> 
> * tests/11_test_changelog_create.py: Test several senarios of
>   debian/changelog creation.
> 
> * gbp/deb/changelog.py: Import scripts from gbp instead of dch to avoid
>   include errors.
>   (create): New class method to create debian/changelog with version from
>   git-dch option "-N" or upstream tag.
> 
> * gbp/scripts/dch.py (guess_version_from_upstream): cp is None when
>   called by ChangeLog.create().
>   (main): Create a new changelog if it does not exist.
> ---
>  gbp/deb/changelog.py              |   36 ++++++++++++++++
>  gbp/scripts/dch.py                |    8 +++-
>  tests/11_test_changelog_create.py |   86 
> +++++++++++++++++++++++++++++++++++++
>  3 files changed, 129 insertions(+), 1 deletion(-)
>  create mode 100644 tests/11_test_changelog_create.py
> 
> diff --git a/gbp/deb/changelog.py b/gbp/deb/changelog.py
> index b8c10b8..a410e86 100644
> --- a/gbp/deb/changelog.py
> +++ b/gbp/deb/changelog.py
> @@ -19,6 +19,7 @@
>  import email
>  import os
>  import subprocess
> +from gbp import scripts

We don't want to import scripts itself (since these are the actual
scripts being run). If we need code from there we should move it out
(see below).

>  class NoChangeLogError(Exception):
>      """No changelog found"""
> @@ -148,3 +149,38 @@ class ChangeLog(object):
>          """
>          return self._cp['Date']
>  
> +    @classmethod
> +    def create(klass, repo=None, package=None, version=None, 
> upstream_tag_format=None):
> +        """
> +        Create an empty debian/changelog
> +
> +        @param repo: git repository object
> +        @type repo: L{GitRepository}
> +        @param package: name of the package, get it from debian/control if 
> None
> +        @type package: C{str}
> +        @param upstream_tag_format: format of the upstream tag
> +        @type upstream_tag_format: C{str}
> +        @return: changelog object
> +        @rtype: L{ChangeLog}
> +        """
> +
> +        if package == None and os.path.isfile("debian/control"):
> +            try:
> +                control = open("debian/control", "r")
> +                for line in control:
> +                    if line.startswith("Source: "):
> +                        package = line.split()[1]
> +                        break
> +            except Exception, e:
> +                raise NoChangeLogError, "Error parsing debian/control: %s" % 
> e

We should probably wrap this in either a Control(object) class or at
least defer this to another function in deb/__init__.py since the
ChangeLog class shouldn't access the control file directly.

> +
> +        if version == None:
> +            version = scripts.dch.guess_version_from_upstream(repo, 
> upstream_tag_format, None)
> +        if version == None:
> +            raise NoChangeLogError, "No suitable version found"
> +
> +        scripts.dch.spawn_dch(msg=["debian/changelog: generated by 
> git-dch."],
> +                              newversion=True, version={'version':version},
> +                              dch_options="--create --package %s" % package)

spawn_dch could be moved into gbp/dch.py. It'd be nicer to have this the
ChangeLog class later on but I don't want you to having to do all the
cleanup work.

> +
> +        return klass(filename="debian/changelog")
> diff --git a/gbp/scripts/dch.py b/gbp/scripts/dch.py
> index 14dff29..2a18f47 100644
> --- a/gbp/scripts/dch.py
> +++ b/gbp/scripts/dch.py
> @@ -112,6 +112,8 @@ def guess_version_from_upstream(repo, 
> upstream_tag_format, cp):
>          version = repo.tag_to_version(tag, upstream_tag_format)
>          if version:
>              gbp.log.debug("Found upstream version %s." % 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:
> @@ -423,7 +425,11 @@ def main(argv):
>              gbp.log.err("You are not on branch '%s' but on '%s'" % 
> (options.debian_branch, branch))
>              raise GbpError, "Use --ignore-branch to ignore or 
> --debian-branch to set the branch name."
>  
> -        cp = ChangeLog(filename=changelog)
> +        try:
> +            cp = ChangeLog(filename=changelog)
> +        except NoChangeLogError, e:
> +            gbp.log.debug("No debian/changelog: create a new one")
> +            cp = ChangeLog.create(repo, version=options.new_version, 
> upstream_tag_format=options.upstream_tag)

This look much better now!

>  
>          if options.since:
>              since = options.since
> diff --git a/tests/11_test_changelog_create.py 
> b/tests/11_test_changelog_create.py
> new file mode 100644
> index 0000000..51a8b04
> --- /dev/null
> +++ b/tests/11_test_changelog_create.py
> @@ -0,0 +1,86 @@
> +# vim: set fileencoding=utf-8 :
> +
> +"""Test L{Changelog}'s create"""
> +
> +import sys
> +sys.path.insert(0, "..")
> +sys.path.insert(0, ".")

What is this needed for?

> +
> +import unittest
> +
> +from testutils import DebianGitTestRepo
> +
> +from gbp.errors import GbpError
> +from gbp.scripts import dch
> +from gbp.deb.changelog import ChangeLog
> +from gbp.deb.git import DebianGitRepository
> +
> +import os
> +
> +class TestScriptDch(DebianGitTestRepo):
> +    """Test git-dch"""
> +
> +    def setUp(self):
> +        DebianGitTestRepo.setUp(self)
> +        self.add_file("foo", "bar")
> +        self.repo.create_tag("upstream/1.0", msg="upstream version 1.0")
> +        self.repo.create_branch("debian")
> +        self.repo.set_branch("debian")
> +        self.upstream_tag = "upstream/%(version)s"
> +        self.top = os.path.abspath(os.path.curdir)
> +        os.mkdir(os.path.join(self.repo.path, "debian"))
> +        os.chdir(self.repo.path)
> +        self.add_file("debian/control", """Source: test-package\nSection: 
> test\n""")
> +
> +    def tearDown(self):
> +        os.chdir(self.top)
> +        DebianGitTestRepo.tearDown(self)
> +
> +    def test_create(self):
> +        """Test direct call to ChangeLog.create(): guess package name from 
> debian/control"""
> +        ChangeLog.create(self.repo, upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines

Since you derived from unittest (via DebianGitTestRepo) you can use
self.assertEqual and friends here. Which gives better failure messages.

> +
> +    def test_create_without_control(self):
> +        """Test direct call to ChangeLog.create()"""
> +        ChangeLog.create(self.repo, package="test-package", 
> upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines

See above.

> +
> +    def test_create_without_upstream_log(self):
> +        """Test direct call to ChangeLog.create(): must not include upstream 
> log in debian/changelog"""
> +        ChangeLog.create(self.repo, upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * added foo.\n""" not in lines

See above.

> +
> +    def test_create_after_delete(self):
> +        """Test direct call to ChangeLog.create(): previous debian/changelog 
> deleted"""
> +        self.add_file("debian/changelog", "fake")
> +        self.repo.remove_files("debian/changelog")
> +        self.repo.commit_files(self.repo.path, "removed debian/changelog")
> +        ChangeLog.create(self.repo, upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * removed debian/changelog.\n""" not in lines

See above.

> +
> +    def test_create_from_dch_main(self):
> +        """Test dch.py like git-dch script does: must include upstream log 
> in debian/changelog"""
> +        dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian"])
> +        lines = file("debian/changelog").readlines()
> +        assert "test-package (1.0-1) UNRELEASED; urgency=low\n" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * added foo.\n""" in lines

See above.

> +
> +    def test_create_from_dch_main_with_auto(self):
> +        """Test dch.py like git-dch script does: must include upstream log 
> in debian/changelog"""
> +        dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-a"])
> +        lines = file("debian/changelog").readlines()
> +        assert "test-package (1.0-1) UNRELEASED; urgency=low\n" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * added foo.\n""" in lines

See above.

Again sorry for the delay.
Cheers,
 -- Guido
> +
> -- 
> 1.7.10
> 

> 
> -- 
> Daniel Dehennin
> Récupérer ma clef GPG:
> gpg --keyserver pgp.mit.edu --recv-keys 0x7A6FE2DF






--
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to