diff options
-rw-r--r-- | gbp/pkg/__init__.py | 77 | ||||
-rw-r--r-- | tests/06_test_upstream_source.py | 4 |
2 files changed, 67 insertions, 14 deletions
diff --git a/gbp/pkg/__init__.py b/gbp/pkg/__init__.py index 7769fbe0..83389297 100644 --- a/gbp/pkg/__init__.py +++ b/gbp/pkg/__init__.py @@ -139,6 +139,67 @@ class PkgPolicy(object): return True return False + @classmethod + def guess_upstream_src_version(cls, filename, extra_regex=r''): + """ + Guess the package name and version from the filename of an upstream + archive. + + @param filename: filename (archive or directory) from which to guess + @type filename: C{string} + @param extra_regex: additional regex to apply, needs a 'package' and a + 'version' group + @return: (package name, version) or ('', '') + @rtype: tuple + + >>> PkgPolicy.guess_upstream_src_version('foo-bar_0.2.orig.tar.gz') + ('foo-bar', '0.2') + >>> PkgPolicy.guess_upstream_src_version('foo-Bar_0.2.orig.tar.gz') + ('foo-Bar', '0.2.orig') + >>> PkgPolicy.guess_upstream_src_version('git-bar-0.2.tar.gz') + ('git-bar', '0.2') + >>> PkgPolicy.guess_upstream_src_version('git-bar-0.2-rc1.tar.gz') + ('git-bar', '0.2-rc1') + >>> PkgPolicy.guess_upstream_src_version('git-bar-0.2:~-rc1.tar.gz') + ('git-bar', '0.2:~-rc1') + >>> PkgPolicy.guess_upstream_src_version('git-Bar-0A2d:rc1.tar.bz2') + ('git-Bar', '0A2d:rc1') + >>> PkgPolicy.guess_upstream_src_version('git-1.tar.bz2') + ('git', '1') + >>> PkgPolicy.guess_upstream_src_version('kvm_87+dfsg.orig.tar.gz') + ('kvm', '87+dfsg') + >>> PkgPolicy.guess_upstream_src_version('foo-Bar-a.b.tar.gz') + ('', '') + >>> PkgPolicy.guess_upstream_src_version('foo-bar_0.2.orig.tar.xz') + ('foo-bar', '0.2') + >>> PkgPolicy.guess_upstream_src_version('foo-bar_0.2.tar.gz') + ('foo-bar', '0.2') + >>> PkgPolicy.guess_upstream_src_version('foo-bar_0.2.orig.tar.lzma') + ('foo-bar', '0.2') + >>> PkgPolicy.guess_upstream_src_version('foo-bar-0.2.zip') + ('foo-bar', '0.2') + >>> PkgPolicy.guess_upstream_src_version('foo-bar-0.2.tlz') + ('foo-bar', '0.2') + """ + version_chars = r'[a-zA-Z\d\.\~\-\:\+]' + basename = parse_archive_filename(os.path.basename(filename))[0] + + version_filters = map ( lambda x: x % version_chars, + ( # Debian upstream tarball: package_'<version>.orig.tar.gz' + r'^(?P<package>[a-z\d\.\+\-]+)_(?P<version>%s+)\.orig', + # Upstream 'package-<version>.tar.gz' + # or Debian native 'package_<version>.tar.gz' + # or directory 'package-<version>': + r'^(?P<package>[a-zA-Z\d\.\+\-]+)(-|_)(?P<version>[0-9]%s*)')) + if extra_regex: + version_filters = extra_regex + version_filters + + for filter in version_filters: + m = re.match(filter, basename) + if m: + return (m.group('package'), m.group('version')) + return ('', '') + @staticmethod def guess_upstream_src_version(filename, extra_regex=r''): """ @@ -309,24 +370,12 @@ class UpstreamSource(object): @property def archive_fmt(self): - """ - >>> UpstreamSource('foo/bar.tar.gz').archive_fmt - 'tar' - >>> UpstreamSource('foo.bar.zip').archive_fmt - 'zip' - >>> UpstreamSource('foo.bar.baz').archive_fmt - """ + """Archive format of the sources, e.g. 'tar'""" return self._archive_fmt @property def compression(self): - """ - >>> UpstreamSource('foo/bar.tar.gz').compression - 'gzip' - >>> UpstreamSource('foo.bar.zip').compression - >>> UpstreamSource('foo.bz2').compression - 'bzip2' - """ + """Compression format of the sources, e.g. 'gzip'""" return self._compression def unpack(self, dir, filters=[]): diff --git a/tests/06_test_upstream_source.py b/tests/06_test_upstream_source.py index 2e9246c6..b5989725 100644 --- a/tests/06_test_upstream_source.py +++ b/tests/06_test_upstream_source.py @@ -63,6 +63,8 @@ class TestTar(unittest.TestCase): self.assertEqual(repacked.is_tarball(), True) self.assertEqual(repacked.is_dir(), False) self.assertEqual(repacked.guess_version(), ('gbp', '0.1')) + self.assertEqual(repacked.archive_fmt, 'tar') + self.assertEqual(repacked.compression, 'bzip2') self._check_tar(repacked, ["gbp/errors.py", "gbp/__init__.py"]) def test_pack_filtered(self): @@ -105,6 +107,8 @@ class TestZip(unittest.TestCase): self.assertEqual(source.is_dir(), False) self.assertEqual(source.unpacked, None) self.assertEqual(source.guess_version(), ('gbp', '0.1')) + self.assertEqual(source.archive_fmt, 'zip') + self.assertEqual(source.compression, None) source.unpack(str(self.tmpdir)) self.assertNotEqual(source.unpacked, None) |