If there is no debian/changelog to find the source package name, and/or
the upstream version was not given on the commandline, ask the user for
them. The values guessed from the original tarball name are offered as
defaults to the user. Previously, this guessed version was used without
confirmation.

The source package name is now always taken from debian/changelog or
prompted for, even when it might not be used (when importing a directory
instead of a tarball, for example). This makes the code more
straightforward and less errorprone in the future.

Closes: #479172
Thanks: Matthijs Kooijman
---
 gbp/deb.py      |   34 ++++++++++--------
 git-import-orig |  100 +++++++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 105 insertions(+), 29 deletions(-)

diff --git a/gbp/deb.py b/gbp/deb.py
index ba9dd0b..cd9f79d 100644
--- a/gbp/deb.py
+++ b/gbp/deb.py
@@ -234,43 +234,47 @@ def get_arch():
     return arch
 
 
-def guess_upstream_version(archive, version_regex=r''):
+def guess_upstream_version(archive, extra_regex=r''):
     """
-    guess the version from the filename of an upstgream archive
+    guess the package name and version from the filename of an upstgream
+    archive. Returns a tuple with package name and version, or None.
     @archive: filename to guess to version for
-    @version_regex: additional version regex to apply, needs a 'version' group
+    @extra_regex: additional regex to apply, needs a 'package' and a
+    'version' group
 
     >>> guess_upstream_version('foo-bar_0.2.orig.tar.gz')
-    '0.2'
+    ('foo-bar', '0.2')
     >>> guess_upstream_version('foo-Bar_0.2.orig.tar.gz')
     >>> guess_upstream_version('git-bar-0.2.tar.gz')
-    '0.2'
+    ('git-bar', '0.2')
     >>> guess_upstream_version('git-bar-0.2-rc1.tar.gz')
-    '0.2-rc1'
+    ('git-bar', '0.2-rc1')
     >>> guess_upstream_version('git-bar-0.2:~-rc1.tar.gz')
-    '0.2:~-rc1'
+    ('git-bar', '0.2:~-rc1')
     >>> guess_upstream_version('git-Bar-0A2d:rc1.tar.bz2')
-    '0A2d:rc1'
+    ('git-Bar', '0A2d:rc1')
     >>> guess_upstream_version('git-1.tar.bz2')
-    '1'
+    ('git', '1')
     >>> guess_upstream_version('kvm_87+dfsg.orig.tar.gz')
-    '87+dfsg'
+    ('kvm', '87+dfsg')
     >>> 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',
+                         
r'^(?P<package>[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
+                         
r'^(?P<package>[a-zA-Z\d\.\+\-]+)-(?P<version>[0-9]%s*)%s'))
+    if extra_regex:
+        version_filters = extra_regex + version_filters
+
     for filter in version_filters:
         m = re.match(filter, os.path.basename(archive))
         if m:
-            return m.group('version')
+            return (m.group('package'), m.group('version'))
 
 # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·:
diff --git a/git-import-orig b/git-import-orig
index 6f7f896..b4f229d 100755
--- a/git-import-orig
+++ b/git-import-orig
@@ -35,6 +35,29 @@ from gbp.git import (FastImport, GitRepositoryError, 
GitRepository, build_tag)
 from gbp.config import GbpOptionParser, GbpOptionGroup
 from gbp.errors import (GbpError, GbpNothingImported)
 
+# Try to import readline, since that will cause raw_input to get fancy
+# line editing and history capabilities. However, if readline is not
+# available, raw_input will still work.
+try:
+    import readline
+except ImportError:
+    pass
+
+# 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-z0-9][a-z0-9\.\+\-]+$")
+# 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\.\+\-\:\~]+$")
+
 def cleanup_tmp_tree(tree):
     """remove a tree of temporary files"""
     try:
@@ -141,6 +164,52 @@ def turn_off_fastimport(options, msg):
         options.fast_import = False
 
 
+def ask_package_name(default):
+    """
+    Ask the user for the source package name.
+    @default The default package name to suggest to the user.
+    """
+    while True:
+        sourcepackage = raw_input("What will be the source package name? [%s] 
" % default)
+        if not sourcepackage: # No input, use the default.
+            sourcepackage = default
+        # Valid package name, return it.
+        if packagename_re.match(sourcepackage):
+            return sourcepackage
+
+        # Not a valid package name. Print an extra
+        # newline before the error to make the output a
+        # bit clearer.
+        print """
+Not a valid package name: '%s'.
+Package names must be at least two characters long, start with an
+alphanumeric and can only containg lower case letters (a-z), digits
+(0-9), plus signs (+), minus signs (-), and periods (.)""" % sourcepackage
+
+
+def ask_package_version(default):
+    """
+    Ask the user for the upstream package version.
+    @default The default package version to suggest to the user.
+    """
+    while True:
+        version = raw_input("What is the upstream version? [%s] " % default)
+        if not version: # No input, use the default.
+            version = default
+        # Valid version, return it.
+        if upstreamversion_re.match(version):
+            return version
+
+        # Not a valid upstream version. Print an extra
+        # newline before the error to make the output a
+        # bit clearer.
+        print """
+Not a valid upstream version: '%s'.
+Package names 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 (~)""" % version
+
+
 def main(argv):
     ret = 0
     tmpdir = ''
@@ -241,17 +310,22 @@ on howto create it otherwise use --upstream-branch to 
specify it.
 """  % options.upstream_branch
             raise GbpError
 
+        # Guess defaults for the package name and version from the
+        # original tarball.
+        (guessed_package, guessed_version) = guess_upstream_version(archive) 
or ('', '')
+
+        # Try to find the source package name
+        try:
+            cp = parse_changelog('debian/changelog')
+            sourcepackage = cp['Source']
+        except NoChangelogError:
+            sourcepackage = ask_package_name(guessed_package)
+
+        # Try to find the version.
         if options.version:
             version = options.version
         else:
-            version = guess_upstream_version(archive)
-
-        if version:
-            print "Upstream version is %s" % version
-        else:
-            print >>sys.stderr, "Cannot determine upstream version from %s - 
use -u <version>" % archive
-            parser.print_help()
-            raise GbpError
+            version = ask_package_version(guessed_version)
 
         (clean, out) = repo.is_clean()
         if not clean and not is_empty:
@@ -277,12 +351,7 @@ on howto create it otherwise use --upstream-branch to 
specify it.
                         os.path.basename(archive).replace(".tar", ".gbp.tar")
                         )
                     repack_orig(archive, tmpdir, os.path.basename(orig_dir))
-            try:
-                cp = parse_changelog('debian/changelog')
-                pristine_orig = symlink_orig(archive, cp['Source'], version)
-            except NoChangelogError:
-                print "Warning: Can't symlink orig.tar.gz due to missing 
debian/changelog"
-                pristine_orig = archive
+            pristine_orig = symlink_orig(archive, sourcepackage, version)
 
         try:
             filter_msg = ["", " (filtering out %s)" % 
options.filters][len(options.filters) > 0]
@@ -292,6 +361,9 @@ on howto create it otherwise use --upstream-branch to 
specify it.
                 print "Importing '%s' to branch '%s'%s..." % (archive, 
options.upstream_branch, filter_msg)
                 if not options.fast_import:
                     repo.set_branch(options.upstream_branch)
+            print "Source package is %s" % sourcepackage
+            print "Upstream version is %s" % version
+
 
             if options.fast_import:
                 fast_import_upstream_tree(repo, pristine_orig, version, 
options)
-- 
1.6.6




-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to