Refactor deb helpers: introduce PkgPolicy class
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 12 Jan 2012 13:43:32 +0000 (15:43 +0200)
committerGuido Günther <agx@sigxcpu.org>
Tue, 1 May 2012 20:27:15 +0000 (22:27 +0200)
Create a new 'pkg' basemodule, intended to be re-used by the upcoming
rpm package helpers. Move some deb functionality to a new pkg.PkgPolicy
class, to be used as a base for different package types. Introduces
Deb-specific deb.DebianPkgPolicy.

gbp/deb/__init__.py
gbp/deb/pristinetar.py
gbp/pkg/__init__.py [new file with mode: 0644]
gbp/scripts/buildpackage.py
gbp/scripts/import_orig.py
tests/05_test_detection.py

index 50ce3f3..0b5168e 100644 (file)
@@ -28,42 +28,38 @@ import gbp.command_wrappers as gbpc
 from gbp.errors import GbpError
 from gbp.git import GitRepositoryError
 from gbp.deb.changelog import ChangeLog, NoChangeLogError
+from gbp.pkg import (PkgPolicy, compressor_opts, compressor_aliases)
 
 # When trying to parse a version-number from a dsc or changes file, these are
 # the valid characters.
 debian_version_chars = 'a-zA-Z\d.~+-'
 
-# Valid package names according to Debian Policy Manual 5.6.1:
-# "Package names (both source and binary, see Package, Section 5.6.7)
-# must consist only of lower case letters (a-z), digits (0-9), plus (+)
-# and minus (-) signs, and periods (.). They must be at least two
-# characters long and must start with an alphanumeric character."
-packagename_re = re.compile("^[a-zA-Z0-9][a-zA-Z0-9\.\+\-~]+$")
-packagename_msg = """Package names must be at least two characters long, start with an
-alphanumeric and can only containg letters (a-z,A-Z), digits
-(0-9), plus signs (+), minus signs (-), periods (.) and hyphens (~)"""
-
-# Valid upstream versions according to Debian Policy Manual 5.6.12:
-# "The upstream_version may contain only alphanumerics[32] and the
-# characters . + - : ~ (full stop, plus, hyphen, colon, tilde) and
-# should start with a digit. If there is no debian_revision then hyphens
-# are not allowed; if there is no epoch then colons are not allowed."
-# Since we don't know about any epochs and debian revisions yet, the
-# last two conditions are not checked.
-upstreamversion_re = re.compile("^[0-9][a-z0-9\.\+\-\:\~]*$")
-upstreamversion_msg = """Upstream version numbers must start with a digit and can only containg lower case
-letters (a-z), digits (0-9), full stops (.), plus signs (+), minus signs
-(-), colons (:) and tildes (~)"""
-
-# compression types, extra options and extensions
-compressor_opts = { 'gzip'  : [ '-n', 'gz' ],
-                    'bzip2' : [ '', 'bz2' ],
-                    'lzma'  : [ '', 'lzma' ],
-                    'xz'    : [ '', 'xz' ] }
-
-# Map frequently used names of compression types to the internal ones:
-compressor_aliases = { 'bz2' : 'bzip2',
-                       'gz'  : 'gzip', }
+
+class DebianPkgPolicy(PkgPolicy):
+    """Packaging policy for Debian"""
+
+    # Valid package names according to Debian Policy Manual 5.6.1:
+    # "Package names (both source and binary, see Package, Section 5.6.7)
+    # must consist only of lower case letters (a-z), digits (0-9), plus (+)
+    # and minus (-) signs, and periods (.). They must be at least two
+    # characters long and must start with an alphanumeric character."
+    packagename_re = re.compile("^[a-zA-Z0-9][a-zA-Z0-9\.\+\-~]+$")
+    packagename_msg = """Package names must be at least two characters long, start with an
+    alphanumeric and can only containg letters (a-z,A-Z), digits
+    (0-9), plus signs (+), minus signs (-), periods (.) and hyphens (~)"""
+
+    # Valid upstream versions according to Debian Policy Manual 5.6.12:
+    # "The upstream_version may contain only alphanumerics[32] and the
+    # characters . + - : ~ (full stop, plus, hyphen, colon, tilde) and
+    # should start with a digit. If there is no debian_revision then hyphens
+    # are not allowed; if there is no epoch then colons are not allowed."
+    # Since we don't know about any epochs and debian revisions yet, the
+    # last two conditions are not checked.
+    upstreamversion_re = re.compile("^[0-9][a-z0-9\.\+\-\:\~]*$")
+    upstreamversion_msg = """Upstream version numbers must start with a digit and can only containg lower case
+    letters (a-z), digits (0-9), full stops (.), plus signs (+), minus signs
+    (-), colons (:) and tildes (~)"""
+
 
 class DpkgCompareVersions(gbpc.Command):
     cmd='/usr/bin/dpkg'
@@ -447,68 +443,6 @@ def orig_file(cp, compression):
                                              compression)
 
 
-def is_valid_packagename(name):
-    "Is this a valid Debian package name?"
-    return packagename_re.match(name)
-
-def is_valid_upstreamversion(version):
-    "Is this a valid upstream version number?"
-    return upstreamversion_re.match(version)
-
-def get_compression(orig_file):
-    """
-    Given an orig file return the compression used
-
-    >>> get_compression("abc.tar.gz")
-    'gzip'
-    >>> get_compression("abc.tar.bz2")
-    'bzip2'
-    >>> get_compression("abc.tar.foo")
-    >>> get_compression("abc")
-    """
-    try:
-        ext = orig_file.rsplit('.',1)[1]
-    except IndexError:
-        return None
-    for (c, o) in compressor_opts.iteritems():
-        if o[1] == ext:
-            return c
-    return None
-
-
-def has_orig(orig_file, dir):
-    "Check if orig tarball exists in dir"
-    try:
-        os.stat( os.path.join(dir, orig_file) )
-    except OSError:
-        return False
-    return True
-
-def symlink_orig(orig_file, orig_dir, output_dir, force=False):
-    """
-    symlink orig tarball from orig_dir to output_dir
-    @return: True if link was created or src == dst
-             False in case of error or src doesn't exist
-    """
-    orig_dir = os.path.abspath(orig_dir)
-    output_dir = os.path.abspath(output_dir)
-
-    if orig_dir == output_dir:
-        return True
-
-    src = os.path.join(orig_dir, orig_file)
-    dst = os.path.join(output_dir, orig_file)
-    if not os.access(src, os.F_OK):
-        return False
-    try:
-        if os.access(dst, os.F_OK) and force:
-            os.unlink(dst)
-        os.symlink(src, dst)
-    except OSError:
-        return False
-    return True
-
-
 def parse_uscan(out):
     """
     Parse the uscan output return (True, tarball) if a new version was
index abbe136..2ac82ef 100644 (file)
@@ -19,7 +19,8 @@
 import os, re
 import gbp.log
 from gbp.command_wrappers import Command
-from gbp.deb import UpstreamSource, compressor_opts
+from gbp.pkg import compressor_opts
+from gbp.deb import UpstreamSource
 
 class PristineTar(Command):
     """The pristine-tar branch in a git repository"""
diff --git a/gbp/pkg/__init__.py b/gbp/pkg/__init__.py
new file mode 100644 (file)
index 0000000..a3f02bc
--- /dev/null
@@ -0,0 +1,104 @@
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2006,2007 Guido Guenther <agx@sigxcpu.org>
+# (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
+#    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
+"""Common functionality of the Debian/RPM package helpers"""
+
+import os
+import re
+import glob
+
+import gbp.log
+import gbp.command_wrappers as gbpc
+from gbp.errors import GbpError
+from gbp.command_wrappers import Command
+
+# compression types, extra options and extensions
+compressor_opts = { 'gzip'  : [ '-n', 'gz' ],
+                    'bzip2' : [ '', 'bz2' ],
+                    'lzma'  : [ '', 'lzma' ],
+                    'xz'    : [ '', 'xz' ] }
+
+# Map frequently used names of compression types to the internal ones:
+compressor_aliases = { 'bz2' : 'bzip2',
+                       'gz'  : 'gzip', }
+
+class PkgPolicy(object):
+    @classmethod
+    def is_valid_packagename(cls, name):
+        "Is this a valid package name?"
+        return cls.packagename_re.match(name)
+
+    @classmethod
+    def is_valid_upstreamversion(cls, version):
+        "Is this a valid upstream version number?"
+        return cls.upstreamversion_re.match(version)
+
+    @staticmethod
+    def get_compression(orig_file):
+        """
+        Given an orig file return the compression used
+
+        >>> PkgPolicy.get_compression("abc.tar.gz")
+        'gzip'
+        >>> PkgPolicy.get_compression("abc.tar.bz2")
+        'bzip2'
+        >>> PkgPolicy.get_compression("abc.tar.foo")
+        >>> PkgPolicy.get_compression("abc")
+        """
+        try:
+            ext = orig_file.rsplit('.',1)[1]
+        except IndexError:
+            return None
+        for (c, o) in compressor_opts.iteritems():
+            if o[1] == ext:
+                return c
+        return None
+
+    @staticmethod
+    def has_orig(orig_file, dir):
+        "Check if orig tarball exists in dir"
+        try:
+            os.stat( os.path.join(dir, orig_file) )
+        except OSError:
+            return False
+        return True
+
+    @staticmethod
+    def symlink_orig(orig_file, orig_dir, output_dir, force=False):
+        """
+        symlink orig tarball from orig_dir to output_dir
+        @return: True if link was created or src == dst
+                 False in case of error or src doesn't exist
+        """
+        orig_dir = os.path.abspath(orig_dir)
+        output_dir = os.path.abspath(output_dir)
+
+        if orig_dir == output_dir:
+            return True
+
+        src = os.path.join(orig_dir, orig_file)
+        dst = os.path.join(output_dir, orig_file)
+        if not os.access(src, os.F_OK):
+            return False
+        try:
+            if os.access(dst, os.F_OK) and force:
+                os.unlink(dst)
+            os.symlink(src, dst)
+        except OSError:
+            return False
+        return True
+
index 7e3ea1e..e1e1097 100755 (executable)
@@ -38,11 +38,12 @@ from gbp.scripts.common.buildpackage import (index_name, wc_name,
                                              git_archive_submodules,
                                              git_archive_single, dump_tree,
                                              write_wc, drop_index)
+from gbp.pkg import (compressor_opts, compressor_aliases)
 
 def git_archive(repo, cp, output_dir, treeish, comp_type, comp_level, with_submodules):
     "create a compressed orig tarball in output_dir using git_archive"
     try:
-        comp_opts = du.compressor_opts[comp_type][0]
+        comp_opts = compressor_opts[comp_type][0]
     except KeyError:
         raise GbpError, "Unsupported compression type '%s'" % comp_type
 
@@ -86,12 +87,12 @@ def prepare_upstream_tarball(repo, cp, options, tarball_dir, output_dir):
     # look in tarball_dir first, if found force a symlink to it
     if options.tarball_dir:
         gbp.log.debug("Looking for orig tarball '%s' at '%s'" % (orig_file, tarball_dir))
-        if not du.symlink_orig(orig_file, tarball_dir, output_dir, force=True):
+        if not du.DebianPkgPolicy.symlink_orig(orig_file, tarball_dir, output_dir, force=True):
             gbp.log.info("Orig tarball '%s' not found at '%s'" % (orig_file, tarball_dir))
         else:
             gbp.log.info("Orig tarball '%s' found at '%s'" % (orig_file, tarball_dir))
     # build an orig unless the user forbids it, always build (and overwrite pre-existing) if user forces it
-    if options.force_create or (not options.no_create_orig and not du.has_orig(orig_file, output_dir)):
+    if options.force_create or (not options.no_create_orig and not du.DebianPkgPolicy.has_orig(orig_file, output_dir)):
         if not pristine_tar_build_orig(repo, cp, output_dir, options):
             upstream_tree = git_archive_build_orig(repo, cp, output_dir, options)
             if options.pristine_tar_commit:
@@ -290,9 +291,9 @@ def guess_comp_type(repo, comp_type, cp, tarball_dir):
     upstream_version = cp['Upstream-Version']
 
     if comp_type != 'auto':
-        comp_type = du.compressor_aliases.get(comp_type, comp_type)
+        comp_type = compressor_aliases.get(comp_type, comp_type)
         try:
-            dummy = du.compressor_opts[comp_type]
+            dummy = compressor_opts[comp_type]
         except KeyError:
             gbp.log.warn("Unknown compression type - guessing.")
             comp_type = 'auto'
@@ -302,8 +303,8 @@ def guess_comp_type(repo, comp_type, cp, tarball_dir):
             if not tarball_dir:
                 tarball_dir = '..'
             detected = None
-            for comp in du.compressor_opts.keys():
-                if du.has_orig(du.orig_file(cp, comp), tarball_dir):
+            for comp in compressor_opts.keys():
+                if du.DebianPkgPolicy.has_orig(du.orig_file(cp, comp), tarball_dir):
                     if detected is not None:
                         raise GbpError, "Multiple orig tarballs found."
                     detected = comp
@@ -320,7 +321,7 @@ def guess_comp_type(repo, comp_type, cp, tarball_dir):
             else:
                 commit = repo.pristine_tar_branch
             tarball = repo.get_subject(commit)
-            comp_type = du.get_compression(tarball)
+            comp_type = du.DebianPkgPolicy.get_compression(tarball)
             gbp.log.debug("Determined compression type '%s'" % comp_type)
             if not comp_type:
                 comp_type = 'gzip'
index 8559a25..4ba9bf8 100644 (file)
@@ -23,9 +23,7 @@ import sys
 import re
 import tempfile
 import gbp.command_wrappers as gbpc
-from gbp.deb import (do_uscan, parse_changelog_repo,
-                     is_valid_packagename, packagename_msg,
-                     is_valid_upstreamversion, upstreamversion_msg)
+from gbp.deb import (DebianPkgPolicy, do_uscan, parse_changelog_repo)
 from gbp.deb.changelog import ChangeLog, NoChangeLogError
 from gbp.deb.git import (GitRepositoryError, DebianGitRepository)
 from gbp.config import GbpOptionParserDebian, GbpOptionGroup, no_upstream_branch_msg
@@ -101,8 +99,8 @@ def detect_name_and_version(repo, source, options):
         except NoChangeLogError:
             if options.interactive:
                 sourcepackage = ask_package_name(guessed_package,
-                                                 is_valid_packagename,
-                                                 packagename_msg)
+                                                 DebianPkgPolicy.is_valid_packagename,
+                                                 DebianPkgPolicy.packagename_msg)
             else:
                 if guessed_package:
                     sourcepackage = guessed_package
@@ -115,8 +113,8 @@ def detect_name_and_version(repo, source, options):
     else:
         if options.interactive:
             version = ask_package_version(guessed_version,
-                                          is_valid_upstreamversion,
-                                          upstreamversion_msg)
+                                          DebianPkgPolicy.is_valid_upstreamversion,
+                                          DebianPkgPolicy.upstreamversion_msg)
         else:
             if guessed_version:
                 version = guessed_version
index 913a7b9..9abb6fe 100644 (file)
@@ -8,7 +8,7 @@ import tempfile
 import unittest
 
 from gbp.scripts import buildpackage
-from gbp.deb import (has_orig, orig_file)
+from gbp.deb import (DebianPkgPolicy, orig_file)
 from gbp.errors import GbpError
 
 class MockGitRepository:
@@ -69,11 +69,11 @@ class TestDetection(unittest.TestCase):
         self.assertEqual("bzip2", guessed)
 
     def test_has_orig_false(self):
-        self.assertFalse(has_orig(orig_file(self.cp, 'gzip'), self.tmpdir))
+        self.assertFalse(DebianPkgPolicy.has_orig(orig_file(self.cp, 'gzip'), self.tmpdir))
 
     def test_has_orig_true(self):
         open(os.path.join(self.tmpdir, 'source_1.2.orig.tar.gz'), "w").close()
-        self.assertTrue(has_orig(orig_file(self.cp, 'gzip'), self.tmpdir))
+        self.assertTrue(DebianPkgPolicy.has_orig(orig_file(self.cp, 'gzip'), self.tmpdir))
 
     def test_guess_comp_type_bzip2(self):
         repo = MockGitRepository(with_branch=False)