~ubuntu-branches/ubuntu/trusty/ubuntu-dev-tools/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
#
# Copyright (C) 2011, Stefano Rivera <stefanor@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import collections
import gzip
import json
import optparse
import os
import time
import urllib

from ubuntutools.lp.lpapicache import (Distribution, Launchpad,
                                       PackageNotFoundException)
from ubuntutools.logger import Logger

DATA_URL = 'http://qa.ubuntuwire.org/ubuntu-seeded-packages/seeded.json.gz'


def load_index(url):
    '''Download a new copy of the image contents index, if necessary,
    and read it.
    '''
    cachedir = os.path.expanduser('~/.cache/ubuntu-dev-tools')
    fn = os.path.join(cachedir, 'seeded.json.gz')

    if (not os.path.isfile(fn)
            or time.time() - os.path.getmtime(fn) > 60 * 60 * 2):
        if not os.path.isdir(cachedir):
            os.makedirs(cachedir)
        urllib.urlretrieve(url, fn)

    try:
        with gzip.open(fn, 'r') as f:
            return json.load(f)
    except Exception, e:
        Logger.error("Unable to parse seed data: %s. "
                     "Deleting cached data, please try again.",
                     str(e))
        os.unlink(fn)


def resolve_binaries(sources):
    '''Return a dict of source:binaries for all binary packages built by
    sources
    '''
    archive = Distribution('ubuntu').getArchive()
    binaries = {}
    for source in sources:
        try:
            spph = archive.getSourcePackage(source)
        except PackageNotFoundException, e:
            Logger.error(str(e))
            continue
        binaries[source] = sorted(set(bpph.getPackageName()
                                      for bpph in spph.getBinaries()))

    return binaries


def present_on(appearences):
    '''Format a list of (flavor, type) tuples into a human-readable string'''
    present = collections.defaultdict(set)
    for flavor, type_ in appearences:
        present[flavor].add(type_)
    for flavor, types in present.iteritems():
        if len(types) > 1:
            types.discard('supported')
    output = ['  %s: %s' % (flavor, ', '.join(sorted(types)))
              for flavor, types in present.iteritems()]
    output.sort()
    return '\n'.join(output)


def output_binaries(index, binaries):
    '''Print binaries found in index'''
    for binary in binaries:
        if binary in index:
            print "%s is seeded in:" % binary
            print present_on(index[binary])
        else:
            print "%s is not seeded (and may not exist)." % binary


def output_by_source(index, by_source):
    '''Print binaries found in index. Grouped by source'''
    for source, binaries in by_source.iteritems():
        seen = False
        if not binaries:
            print ("Status unknown: No binary packages built by the latest "
                   "%s.\nTry again using -b and the expected binary packages."
                   % source)
            continue
        for binary in binaries:
            if binary in index:
                seen = True
                print "%s (from %s) is seeded in:" % (binary, source)
                print present_on(index[binary])
        if not seen:
            print "%s's binaries are not seeded." % source


def main():
    '''Query which images the specified packages are on'''
    parser = optparse.OptionParser('%prog [options] package...')
    parser.add_option('-b', '--binary',
                      default=False, action='store_true',
                      help="Binary packages are being specified, "
                           "not source packages (fast)")
    parser.add_option('-u', '--data-url', metavar='URL',
                      default=DATA_URL,
                      help='URL for the seeded packages index. '
                           'Default: UbuntuWire')
    options, args = parser.parse_args()

    if len(args) < 1:
        parser.error("At least one package must be specified")

    # Login anonymously to LP
    Launchpad.login_anonymously()

    index = load_index(options.data_url)
    if options.binary:
        output_binaries(index, args)
    else:
        binaries = resolve_binaries(args)
        output_by_source(index, binaries)


if __name__ == '__main__':
    main()