Add gbp.deb.ChangeLogSection
authorGuido Günther <agx@sigxcpu.org>
Tue, 15 May 2012 19:00:57 +0000 (21:00 +0200)
committerGuido Günther <agx@sigxcpu.org>
Tue, 15 May 2012 19:01:33 +0000 (21:01 +0200)
to parse package and version out of a changelog section

gbp/deb/changelog.py
tests/test_Changelog.py

index b8c10b8..7dc51d9 100644 (file)
@@ -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)
index 9048e20..48b370e 100644 (file)
@@ -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'
+    """