aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2012-05-15 21:00:57 +0200
committerGuido Günther <agx@sigxcpu.org>2012-05-15 21:01:33 +0200
commit1eeb298934012223a50ced516c1a38592d30303e (patch)
treeaa137ff36b5dd3a2dabe1215780bfd6422acffb3
parentc57d4af675910ec151cf982532db0f877aef413f (diff)
Add gbp.deb.ChangeLogSection
to parse package and version out of a changelog section
-rw-r--r--gbp/deb/changelog.py56
-rw-r--r--tests/test_Changelog.py24
2 files changed, 80 insertions, 0 deletions
diff --git a/gbp/deb/changelog.py b/gbp/deb/changelog.py
index b8c10b86..7dc51d9f 100644
--- a/gbp/deb/changelog.py
+++ b/gbp/deb/changelog.py
@@ -28,6 +28,37 @@ class ParseChangeLogError(Exception):
"""Problem parsing changelog"""
pass
+
+class ChangeLogSection(object):
+ """A section in the changelog describing one particular version"""
+ def __init__(self, package, version):
+ self._package = package
+ self._version = version
+
+ @property
+ def package(self):
+ return self._package
+
+ @property
+ def version(self):
+ return self._version
+
+ @classmethod
+ def parse(klass, section):
+ """
+ Parse one changelog section
+
+ @param section: a changelog section
+ @type section: C{str}
+ @returns: the parse changelog section
+ @rtype: L{ChangeLogSection}
+ """
+ header = section.split('\n')[0]
+ package = header.split()[0]
+ version = header.split()[1][1:-1]
+ return klass(package, version)
+
+
class ChangeLog(object):
"""A Debian changelog"""
@@ -75,6 +106,11 @@ class ChangeLog(object):
raise ParseChangeLogError, output.split('\n')[0]
self._cp = cp
+ if contents:
+ self._contents = contents[:]
+ else:
+ with file(filename) as f:
+ self._contents = f.read()
def __getitem__(self, item):
return self._cp[item]
@@ -148,3 +184,23 @@ class ChangeLog(object):
"""
return self._cp['Date']
+ @property
+ def sections_iter(self):
+ """
+ Iterate over sections in the changelog
+ """
+ section = ''
+ for line in self._contents.split('\n'):
+ if line and line[0] not in [ ' ', '\t' ]:
+ section += line
+ else:
+ if section:
+ yield ChangeLogSection.parse(section)
+ section = ''
+
+ @property
+ def sections(self):
+ """
+ Get sections in the changelog
+ """
+ return list(self.sections_iter)
diff --git a/tests/test_Changelog.py b/tests/test_Changelog.py
index 9048e205..48b370e7 100644
--- a/tests/test_Changelog.py
+++ b/tests/test_Changelog.py
@@ -192,3 +192,27 @@ def test_parse_last_mod():
>>> cl.date
'Mon, 17 Oct 2011 10:15:22 +0200'
"""
+
+def test_parse_sections():
+ """
+ Test if we can parse sections out of the changelog
+
+ Methods tested:
+ - L{gbp.deb.changelog.ChangeLog.__init__}
+ - L{gbp.deb.changelog.ChangeLogSection.__init__}
+ - L{gbp.deb.changelog.ChangeLogSection.parse}
+
+ Properties tested:
+ - L{gbp.deb.changelog.ChangeLog.sections}
+
+ >>> import gbp.deb.changelog
+ >>> cl = gbp.deb.changelog.ChangeLog(cl_debian)
+ >>> cl.sections[0].package
+ 'git-buildpackage'
+ >>> cl.sections[0].version
+ '0.5.32'
+ >>> cl.sections[1].package
+ 'git-buildpackage'
+ >>> cl.sections[1].version
+ '0.5.31'
+ """