allow for uppercase characters in the version pattern
authorGuido Günther <agx@sigxcpu.org>
Tue, 9 Jun 2009 15:35:34 +0000 (17:35 +0200)
committerGuido Günther <agx@sigxcpu.org>
Thu, 11 Jun 2009 16:22:04 +0000 (18:22 +0200)
and in the package name if it's not a debian source package's name.
Also allow for ':' and '~' which are allowed accoring to Debian Policy.
Based on a patch by Felipe Sateler.

Closes: #531819

gbp/deb_utils.py
git-import-orig

index 2879459..f20033a 100644 (file)
@@ -26,7 +26,6 @@ class ParseChangeLogError(Exception):
     """problem parsing changelog"""
     pass
 
-
 class DscFile(object):
     """Keeps all needed data read from a dscfile"""
     pkg_re = re.compile('Source:\s+(?P<pkg>.+)\s*')
@@ -216,6 +215,42 @@ def get_arch():
     return arch
 
 
+def guess_upstream_version(archive, version_regex=r''):
+    """
+    guess the version from the filename of an upstgream archive
+    @archive: filename to guess to version for
+    @version_regex: additional version regex to apply, needs a 'version' group
+
+    >>> guess_upstream_version('foo-bar_0.2.orig.tar.gz')
+    '0.2'
+    >>> guess_upstream_version('foo-Bar_0.2.orig.tar.gz')
+    >>> guess_upstream_version('git-bar-0.2.tar.gz')
+    '0.2'
+    >>> guess_upstream_version('git-bar-0.2-rc1.tar.gz')
+    '0.2-rc1'
+    >>> guess_upstream_version('git-bar-0.2:~-rc1.tar.gz')
+    '0.2:~-rc1'
+    >>> guess_upstream_version('git-Bar-0A2d:rc1.tar.bz2')
+    '0A2d:rc1'
+    >>> guess_upstream_version('foo-Bar_0.2.orig.tar.gz')
+    >>> guess_upstream_version('foo-Bar-a.b.tar.gz')
+    """
+    version_chars = r'[a-zA-Z\d\.\~\-\:]'
+    extensions = r'\.tar\.(gz|bz2)'
+
+    version_filters = map ( lambda x: x % (version_chars, extensions),
+                       ( # Debian package_<version>.orig.tar.gz:
+                         r'^[a-z\d\.\+\-]+_(?P<version>%s+)\.orig%s',
+                         # Upstream package-<version>.tar.gz:
+                         r'^[a-zA-Z\d\.\+\-]+-(?P<version>[0-9]%s+)%s'))
+    if version_regex:
+        version_filters = version_regex + version_filters
+    for filter in version_filters:
+        m = re.match(filter, os.path.basename(archive))
+        if m:
+            return m.group('version')
+
+
 def _test():
     import doctest
     doctest.testmod()
index e2529f6..acb012c 100755 (executable)
@@ -27,7 +27,7 @@ import subprocess
 import tarfile
 import time
 import gbp.command_wrappers as gbpc
-from gbp.deb_utils import parse_changelog, unpack_orig, NoChangelogError, has_epoch, tar_toplevel
+from gbp.deb_utils import parse_changelog, unpack_orig, NoChangelogError, has_epoch, tar_toplevel, guess_upstream_version
 from gbp.git_utils import (GitRepositoryError, GitRepository, build_tag)
 from gbp.config import GbpOptionParser
 from gbp.errors import (GbpError, GbpNothingImported)
@@ -196,24 +196,6 @@ def fast_import_upstream_tree(repo, tarball, version, options):
         print "FastImport done."
 
 
-def guess_version(archive, version_regex=r''):
-    """
-    guess the version from the filename of an upstgream archive
-    @archive: filename to guess to version for
-    @version_regex: additional version regex to apply, needs a 'version' group
-    """
-    version_filters = [ # Debian package_<version>.orig.tar.gz:
-                        r'^[a-z\d\.\+\-]+_(?P<version>[a-z\d\.\~\-]+)\.orig\.tar\.(gz|bz2)',
-                        # Upstream package-<version>.tar.gz:
-                        r'^[a-z\d\.\+\-]+-(?P<version>[a-z\d\.]+)\.tar\.(gz|bz2)' ]
-    if version_regex:
-        version_filters = version_regex + version_filters
-    for filter in version_filters:
-        m = re.match(filter, os.path.basename(archive))
-        if m:
-            return m.group('version')
-
-
 def turn_off_fastimport(options, msg):
     if options.fast_import:
         print >>sys.stderr, msg
@@ -285,7 +267,7 @@ on howto create it otherwise use --upstream-branch to specify it.
         if options.version:
             version = options.version
         else:
-            version = guess_version(archive)
+            version = guess_upstream_version(archive)
 
         if version:
             print "Upstream version is %s" % version