~unit193/ubuntu-dev-tools/update-deb-mirror

105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
1
#!/usr/bin/python
135 by Siegfried-Angel Gevatter Pujals
Remove unneeded AUTHORS and README. Add the GPL header to all scripts.
2
#
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
3
# pull-lp-source -- pull a source package from Launchpad
136 by Jonathan Patrick Davies
* pull-lp-source: Now check if package exists in the release.
4
# Basic usage: pull-lp-source <source package> [<release>]
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
5
#
995 by Stefano Rivera
Catch DownloadErrors in ubuntutools.archive users. (LP: #708862)
6
# Copyright (C) 2008,      Iain Lane <iain@orangesquash.org.uk>,
7
#               2010-2011, Stefano Rivera <stefanor@ubuntu.com>
820 by Benjamin Drung
Remove all trailing spaces.
8
#
135 by Siegfried-Angel Gevatter Pujals
Remove unneeded AUTHORS and README. Add the GPL header to all scripts.
9
# ##################################################################
10
#
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
11
# This program is free software; you can redistribute it and/or
12
# modify it under the terms of the GNU General Public License
13
# as published by the Free Software Foundation; either version 3
14
# of the License, or (at your option) any later version.
820 by Benjamin Drung
Remove all trailing spaces.
15
#
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
820 by Benjamin Drung
Remove all trailing spaces.
20
#
135 by Siegfried-Angel Gevatter Pujals
Remove unneeded AUTHORS and README. Add the GPL header to all scripts.
21
# See file /usr/share/common-licenses/GPL for more details.
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
22
#
135 by Siegfried-Angel Gevatter Pujals
Remove unneeded AUTHORS and README. Add the GPL header to all scripts.
23
# ##################################################################
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
24
25
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
26
import json
134 by Jonathan Patrick Davies
* pull-lp-source:
27
import os
607.1.6 by Michael Bienia
pull-lp-source: Use (anonymously) the LP API to get the URL for the .dsc
28
import sys
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
29
import urllib2
134 by Jonathan Patrick Davies
* pull-lp-source:
30
from optparse import OptionParser
105.1.1 by Iain Lane
Add simple script pull-lp-source to download and extract a source given source package from LP
31
1309.1.10 by Stefano Rivera
pbuilder-dist, pull-debian-source, pull-lp-source, requestsync,
32
from distro_info import UbuntuDistroInfo, DistroDataOutdated
1087 by Benjamin Drung
Move suspicious-source and wrap-and-sort from ubuntu-dev-tools into devscripts.
33
995 by Stefano Rivera
Catch DownloadErrors in ubuntutools.archive users. (LP: #708862)
34
from ubuntutools.archive import UbuntuSourcePackage, DownloadError
902.1.9 by Stefano Rivera
Add mirror support to pull-lp-source
35
from ubuntutools.config import UDTConfig
607.1.6 by Michael Bienia
pull-lp-source: Use (anonymously) the LP API to get the URL for the .dsc
36
from ubuntutools.lp.lpapicache import Distribution, Launchpad
37
from ubuntutools.lp.udtexceptions import (SeriesNotFoundException,
940.1.3 by Stefano Rivera
Move common downloading code into new ubuntutools.mirrors. Use in backportpackage, pull-debian-debdiff, pull-lp-source
38
                                          PackageNotFoundException,
39
                                          PocketDoesNotExistError)
1383 by Benjamin Drung
Move devscripts.logger to ubuntutools.logger.
40
from ubuntutools.logger import Logger
940.1.3 by Stefano Rivera
Move common downloading code into new ubuntutools.mirrors. Use in backportpackage, pull-debian-debdiff, pull-lp-source
41
from ubuntutools.misc import split_release_pocket
185 by Jonathan Patrick Davies
* common.py: New functions: checkReleaseExists() and checkSourceExists().
42
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
43
44
def source_package_for(binary, release):
45
    """Query DDE to find the source package for a particular binary
46
    Should really do this with LP, but it's not possible LP: #597041
47
    """
48
    url = ('http://dde.debian.net/dde/q/udd/dist/d:ubuntu/r:%s/p:%s/?t=json'
49
           % (release, binary))
1348 by Stefano Rivera
pull-lp-source: Catch errors parsing JSON we got from DDE (LP: #1059848)
50
    data = None
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
51
    try:
52
        data = json.load(urllib2.urlopen(url))['r']
53
    except urllib2.URLError, e:
54
        Logger.error('Unable to retrieve package information from DDE: '
55
                     '%s (%s)', url, str(e))
1349 by Stefano Rivera
Use the same construction as pull-debian-source
56
    except ValueError, e:
57
        Logger.error('Unable to parse JSON response from DDE: '
58
                     '%s (%s)', url, str(e))
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
59
    if not data:
60
        return None
61
    return data[0]['source']
62
63
906 by Benjamin Drung
pull-lp-source: Make pylint a little bit happier.
64
def main():
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
65
    usage = "Usage: %prog <package> [release|version]"
906 by Benjamin Drung
pull-lp-source: Make pylint a little bit happier.
66
    opt_parser = OptionParser(usage)
67
    opt_parser.add_option('-d', '--download-only',
68
                          dest='download_only', default=False,
69
                          action='store_true',
70
                          help="Do not extract the source package")
71
    opt_parser.add_option('-m', '--mirror', metavar='UBUNTU_MIRROR',
72
                          dest='ubuntu_mirror',
73
                          help='Preferred Ubuntu mirror (default: Launchpad)')
74
    opt_parser.add_option('--no-conf',
75
                          dest='no_conf', default=False, action='store_true',
76
                          help="Don't read config files or environment "
77
                               "variables")
78
    (options, args) = opt_parser.parse_args()
607.1.6 by Michael Bienia
pull-lp-source: Use (anonymously) the LP API to get the URL for the .dsc
79
    if not args:
906 by Benjamin Drung
pull-lp-source: Make pylint a little bit happier.
80
        opt_parser.error("Must specify package name")
902.1.9 by Stefano Rivera
Add mirror support to pull-lp-source
81
82
    config = UDTConfig(options.no_conf)
83
    if options.ubuntu_mirror is None:
84
        options.ubuntu_mirror = config.get_value('UBUNTU_MIRROR')
607.1.6 by Michael Bienia
pull-lp-source: Use (anonymously) the LP API to get the URL for the .dsc
85
86
    # Login anonymously to LP
87
    Launchpad.login_anonymously()
88
185 by Jonathan Patrick Davies
* common.py: New functions: checkReleaseExists() and checkSourceExists().
89
    package = str(args[0]).lower()
134 by Jonathan Patrick Davies
* pull-lp-source:
90
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
91
    ubuntu_info = UbuntuDistroInfo()
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
92
    if len(args) > 1:  # Custom distribution specified.
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
93
        version = str(args[1])
607.1.6 by Michael Bienia
pull-lp-source: Use (anonymously) the LP API to get the URL for the .dsc
94
    else:
1309.1.10 by Stefano Rivera
pbuilder-dist, pull-debian-source, pull-lp-source, requestsync,
95
        try:
96
            version = os.getenv('DIST') or ubuntu_info.devel()
97
        except DistroDataOutdated, e:
98
            Logger.warn("%s\nOr specify a distribution.", e)
99
            sys.exit(1)
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
100
    component = None
101
102
    # Release, not package version number:
1039 by Stefano Rivera
Split release pockets a litle more sanely
103
    release = None
104
    pocket = None
105
    try:
1233 by Stefano Rivera
pull-lp-source, requestbackport: Take the latest version from any
106
        (release, pocket) = split_release_pocket(version, default=None)
1039 by Stefano Rivera
Split release pockets a litle more sanely
107
    except PocketDoesNotExistError, e:
108
        pass
1233 by Stefano Rivera
pull-lp-source, requestbackport: Take the latest version from any
109
    if release in ubuntu_info.all:
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
110
        archive = Distribution('ubuntu').getArchive()
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
111
        try:
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
112
            spph = archive.getSourcePackage(package, release, pocket)
113
        except SeriesNotFoundException, e:
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
114
            Logger.error(str(e))
115
            sys.exit(1)
1245 by Stefano Rivera
pull-debian-source, pull-lp-source: Resolve the source package (via DDE),
116
        except PackageNotFoundException, e:
117
            source_package = source_package_for(package, release)
118
            if source_package is not None and source_package != package:
119
                try:
120
                    spph = archive.getSourcePackage(source_package, release,
121
                                                    pocket)
122
                    package = source_package
123
                except PackageNotFoundException:
124
                    Logger.error(str(e))
125
                    sys.exit(1)
126
            else:
127
                Logger.error(str(e))
128
                sys.exit(1)
129
1036 by Stefano Rivera
pull-{lp,debian}-source: Download requested versions, as well as simply
130
        version = spph.getVersion()
131
        component = spph.getComponent()
132
133
    Logger.normal('Downloading %s version %s', package, version)
134
    srcpkg = UbuntuSourcePackage(package, version, component=component,
940.1.8 by Stefano Rivera
Beginnings of an OO ubuntutools.archive.SourcePackage
135
                                 mirrors=[options.ubuntu_mirror])
995 by Stefano Rivera
Catch DownloadErrors in ubuntutools.archive users. (LP: #708862)
136
    try:
137
        srcpkg.pull()
138
    except DownloadError, e:
139
        Logger.error('Failed to download: %s', str(e))
140
        sys.exit(1)
940.1.8 by Stefano Rivera
Beginnings of an OO ubuntutools.archive.SourcePackage
141
    if not options.download_only:
142
        srcpkg.unpack()
906 by Benjamin Drung
pull-lp-source: Make pylint a little bit happier.
143
144
if __name__ == '__main__':
1040 by Stefano Rivera
pull-{lp,debian}-source, pull-debian-debdiff: Catch KeyboardInterrupt.
145
    try:
146
        main()
147
    except KeyboardInterrupt:
1041 by Stefano Rivera
Make that 'User abort.'
148
        Logger.normal('User abort.')