From 6d63d2ad0d98cd9bf6b4c27d02a799d64ed20228 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20O=C5=BCarowski?= <piotr@debian.org>
Date: Sun, 18 Jul 2010 11:30:53 +0200
Subject: [PATCH] dh_python2: follow Distribute and replace all
 non-alphanumeric characters with underscore in distribution name

---
 debian/changelog    |  2 ++
 debpython/pydist.py | 18 ++++++++++++++----
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 6c8eb40..77c2a96 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -3,6 +3,8 @@ python-defaults (2.6.5-9) UNRELEASED; urgency=low
   [ Piotr Ożarowski ]
   * pyclean, pycompile: use .communicate() instead of .wait() to avoid hanging
     `dpkg -L PACKAGE` in few situations
+  * dh_python2: follow Distribute and replace all non-alphanumeric
+    characters with underscore in distribution name
 
   [ Matthias Klose ]
   * Require python (>= 2.6.5-9~) in generated dependencies to use the fixed
diff --git a/debpython/pydist.py b/debpython/pydist.py
index 5b9e13b..6ba0617 100644
--- a/debpython/pydist.py
+++ b/debpython/pydist.py
@@ -89,7 +89,7 @@ def load(dname='/usr/share/python/dist/', fname='debian/pydist-overrides'):
                 if line.startswith('#') or not line:
                     continue
                 dist = PYDIST_RE.search(line).groupdict()
-                name = dist['name'].lower()
+                name = safe_name(dist['name'])
                 dist['versions'] = get_requested_versions(dist['vrange'])
                 dist['dependency'] = dist['dependency'].strip()
                 if dist['rules']:
@@ -106,6 +106,10 @@ def guess_dependency(req, version=None):
     if isinstance(version, basestring):
         version = getver(version)
 
+    # some upstreams have weird ideas for distribution name...
+    name, rest = re.compile('([^><= ]+)(.*)').match(req).groups()
+    req = safe_name(name) + rest
+
     data = load()
     req_dict = REQUIRES_RE.match(req)
     if not req:
@@ -143,8 +147,9 @@ def guess_dependency(req, version=None):
 
     log.debug("invoking dpkg -S %s", query)
     process = Popen("/usr/bin/dpkg -S %s" % query, \
-                    shell=True, stdout=PIPE, stderr=PIPE)
-    if process.wait() != 0:
+                    shell=True, stdout=PIPE)
+    stdout, stderr = process.communicate()
+    if process.returncode != 0:
         log.error('Cannot find package that provides %s. '
                   'Please add it to debian/pydist-overrides', name)
         log.info("hint: `apt-file search -x '(packages|pyshared)/" +\
@@ -152,7 +157,7 @@ def guess_dependency(req, version=None):
         exit(8)
 
     result = set()
-    for line in process.stdout:
+    for line in stdout:
         result.add(line.split(':')[0])
     if len(result) > 1:
         log.error('more than one package name found for %s dist', name)
@@ -179,3 +184,8 @@ def parse_pydep(fname):
                 if dependency:
                     result.append(dependency)
     return result
+
+
+def safe_name(name):
+    """Emulate distribute's safe_name."""
+    return re.compile('[^A-Za-z0-9.]+').sub('_', name).lower()
-- 
GitLab