Package: ubuntu-dev-tools
Version: 0.158
Severity: normal
Tags: patch
User: ubuntu-de...@lists.ubuntu.com
Usertags: origin-ubuntu zesty ubuntu-patch

Dear Maintainer,

Calling pull-lp-source with the name of a binary package fails to get the
corresponding source package.

In Ubuntu, this is tracked with bug 1453330:
https://bugs.launchpad.net/ubuntu/+source/ubuntu-dev-tools/+bug/1453330

This patch corrects that so pull-lp-source will correctly lookup the source
package name for any binary package name provided.

Thanks for considering the patch.
diff -Nru ubuntu-dev-tools-0.157/pull-lp-source ubuntu-dev-tools-0.158/pull-lp-source
--- ubuntu-dev-tools-0.157/pull-lp-source	2016-05-09 00:56:05.000000000 -0400
+++ ubuntu-dev-tools-0.158/pull-lp-source	2017-02-02 16:52:52.000000000 -0500
@@ -40,25 +40,36 @@
 from ubuntutools.logger import Logger
 from ubuntutools.misc import split_release_pocket
 
+from launchpadlib.launchpad import Launchpad as LP
 
-def source_package_for(binary, release):
-    """Query DDE to find the source package for a particular binary
-    Should really do this with LP, but it's not possible LP: #597041
-    """
-    url = ('http://dde.debian.net/dde/q/udd/dist/d:ubuntu/r:%s/p:%s/?t=json'
-           % (release, binary))
-    data = None
-    try:
-        data = json.load(urllib2.urlopen(url))['r']
-    except urllib2.URLError, e:
-        Logger.error('Unable to retrieve package information from DDE: '
-                     '%s (%s)', url, str(e))
-    except ValueError, e:
-        Logger.error('Unable to parse JSON response from DDE: '
-                     '%s (%s)', url, str(e))
-    if not data:
+
+def getSPPH(lp, archive, package, version, series, pocket, try_binary=True):
+    params = { 'exact_match': True, 'order_by_date': True, }
+    if pocket:
+        params['pocket'] = pocket
+    if series:
+        params['distro_series'] = series()
+    else:
+        params['version'] = version
+    spphs = archive.getPublishedSources(source_name=package, **params)
+    if spphs:
+        return spphs[0]
+    if not try_binary:
         return None
-    return data[0]['source']
+
+    # Didn't find any, maybe the package is a binary package name
+    if series:
+        del params['distro_series']
+        archs = lp.load(series().architectures_collection_link).entries
+        params['distro_arch_series'] = archs[0]['self_link']
+    bpphs = archive.getPublishedBinaries(binary_name=package, **params)
+    if bpphs:
+        bpph_build = lp.load(bpphs[0].build_link)
+        source_package = bpph_build.source_package_name
+        return getSPPH(lp, archive, source_package, version, series, pocket,
+                       try_binary=False)
+
+    return None
 
 
 def main():
@@ -85,6 +96,7 @@
 
     # Login anonymously to LP
     Launchpad.login_anonymously()
+    lp = LP.login_anonymously("pull-lp-source", "production")
 
     package = str(args[0]).lower()
 
@@ -106,29 +118,25 @@
         (release, pocket) = split_release_pocket(version, default=None)
     except PocketDoesNotExistError, e:
         pass
+
+    distro = Distribution('ubuntu')
+    archive = distro.getArchive()
+
+    series = None
     if release in ubuntu_info.all:
-        archive = Distribution('ubuntu').getArchive()
-        try:
-            spph = archive.getSourcePackage(package, release, pocket)
-        except SeriesNotFoundException, e:
-            Logger.error(str(e))
-            sys.exit(1)
-        except PackageNotFoundException, e:
-            source_package = source_package_for(package, release)
-            if source_package is not None and source_package != package:
-                try:
-                    spph = archive.getSourcePackage(source_package, release,
-                                                    pocket)
-                    package = source_package
-                except PackageNotFoundException:
-                    Logger.error(str(e))
-                    sys.exit(1)
-            else:
-                Logger.error(str(e))
-                sys.exit(1)
+        series = distro.getSeries(release)
 
-        version = spph.getVersion()
-        component = spph.getComponent()
+    spph = getSPPH(lp, archive, package, version, series, pocket)
+    if not spph:
+        Logger.error("Package %s not found in %s", package, release)
+        sys.exit(1)
+
+    if package != spph.source_package_name:
+        Logger.normal("Using source package %s for %s",
+                      spph.source_package_name, package);
+    package = spph.source_package_name
+    version = spph.source_package_version
+    component = spph.component_name
 
     Logger.normal('Downloading %s version %s', package, version)
     srcpkg = UbuntuSourcePackage(package, version, component=component,

Reply via email to