Provide minimalist debian/control object
authorDaniel Dehennin <daniel.dehennin@baby-gnu.org>
Sun, 20 May 2012 15:36:59 +0000 (17:36 +0200)
committerGuido Günther <agx@sigxcpu.org>
Sun, 20 May 2012 17:42:16 +0000 (19:42 +0200)
Closes: #673473
Signed-off-by: Guido Günther <agx@sigxcpu.org>
gbp/deb/control.py [new file with mode: 0644]
tests/test_Control.py [new file with mode: 0644]

diff --git a/gbp/deb/control.py b/gbp/deb/control.py
new file mode 100644 (file)
index 0000000..da01ebe
--- /dev/null
@@ -0,0 +1,78 @@
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2012 Daniel Dehennin <daniel.dehennin@baby-gnu.org>
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+"""A Debian Control"""
+
+import email
+import os
+
+class NoControlError(Exception):
+    """No control found"""
+    pass
+
+class ParseControlError(Exception):
+    """Problem parsing control"""
+    pass
+
+class Control(object):
+    """A Debian control"""
+
+    def __init__(self, contents=None, filename="debian/control"):
+        """
+        Parse an existing control file.
+
+        @param contents: content of a control file
+        @type contents: C{str}
+        @param filename: name of the control file
+        @type filename: C{str}
+        @return: Control object
+        @rtype: C{gbp.deb.conrol.Control} object
+        """
+        if contents:
+            control = email.message_from_string(contents)
+        else:
+            if not os.access(filename, os.F_OK):
+                raise NoControlError("Control file %s does not exist" % filename)
+
+            with file(filename) as f:
+                control = email.message_from_file(f)
+
+        if not control.items():
+            raise ParseControlError("Empty or invalid control file or contents")
+
+        self._control = control
+        self.filename = filename
+
+    def __getitem__(self, item):
+        return self._control[item]
+
+    def __setitem__(self, item, value):
+        self._control[item] = value
+
+    @property
+    def name(self):
+        """The packges name"""
+        return self._control['Source']
+
+    @property
+    def section(self):
+        """The packges section"""
+        return self._control['Section']
+
+    @property
+    def priority(self):
+        """The packges priority"""
+        return self._control['Priority']
diff --git a/tests/test_Control.py b/tests/test_Control.py
new file mode 100644 (file)
index 0000000..0fdeb91
--- /dev/null
@@ -0,0 +1,89 @@
+# vim: set fileencoding=utf-8 :
+
+"""
+Test L{gbp.deb.control.Control}
+"""
+
+cl_debian = """Source: git-buildpackage
+Section: vcs
+Priority: optional
+Maintainer: Guido Günther <agx@sigxcpu.org>
+Build-Depends: debhelper (>= 7.0.50~), python (>> 2.6.6-3~),
+ pychecker, gtk-doc-tools, sgml2x, docbook-utils, jade, python-dateutil, python-nose,
+ bash-completion, perl, python-epydoc, python-coverage, python-setuptools,
+ # For the testsuite
+ git (>= 1:1.7.9.1-1~), bzip2, unzip, pristine-tar
+Standards-Version: 3.9.3
+Vcs-Git: git://honk.sigxcpu.org/git/git-buildpackage.git
+Vcs-Browser: http://git.debian.org/?p=users/agx/git-buildpackage.git
+Homepage: https://honk.sigxcpu.org/piki/projects/git-buildpackage/
+X-Python-Version: >= 2.6
+
+Package: git-buildpackage
+Architecture: all
+Depends: ${python:Depends}, ${shlibs:Depends}, ${misc:Depends}, devscripts (>= 2.10.66~),
+ git (>= 1:1.7.9.1-1~), python-dateutil
+Recommends: pristine-tar (>= 0.5), cowbuilder
+Suggests: python-notify, unzip
+Description: Suite to help with Debian packages in Git repositories
+ This package contains the following tools:
+  * git-import-{dsc,dscs}: import existing Debian source packages into a git
+    repository
+  * git-import-orig: import a new upstream version into the git repository
+  * git-buildpackage: build a package out of a git repository, check for local
+    modifications and tag appropriately
+  * git-dch: generate Debian changelog entries from Git commit messages
+  * gbp-{pull,clone}: clone and pull from remote repos
+  * gbp-pq: manage debian/patches easily
+  * gbp-create-remote-repo: create remote repositories
+"""
+
+def test_parse_control():
+    """
+    Parse a the control of debian package
+
+    Methods tested:
+         - L{gbp.deb.control.Control.__init__}
+
+    Properties tested:
+         - L{gbp.deb.control.Control.name}
+         - L{gbp.deb.control.Control.section}
+         - L{gbp.deb.control.Control.priority}
+
+    >>> import gbp.deb.control
+    >>> cl = gbp.deb.control.Control(cl_debian)
+    >>> cl.name
+    'git-buildpackage'
+    >>> cl.name == cl['Source']
+    True
+    >>> cl.section
+    'vcs'
+    >>> cl.section == cl['Section']
+    True
+    >>> cl.priority
+    'optional'
+    >>> cl.priority == cl['Priority']
+    True
+    >>> cl['Standards-Version']
+    '3.9.3'
+    >>> cl['Package']
+
+    """
+
+def test_no_control_error():
+    """
+    Raise an error if no control file exist or is empty
+
+    Methods tested:
+         - L{gbp.deb.control.Control.__init__}
+
+    >>> import gbp.deb.control
+    >>> cl = gbp.deb.control.Control(filename="doesnotexist")
+    Traceback (most recent call last):
+    ...
+    NoControlError: Control file doesnotexist does not exist
+    >>> cl = gbp.deb.control.Control("notparsable")
+    Traceback (most recent call last):
+    ...
+    ParseControlError: Empty or invalid control file or contents
+    """