ubuntu-dev-tools/seeded-in-ubuntu

146 lines
4.9 KiB
Plaintext
Raw Normal View History

2019-09-04 16:43:12 -03:00
#!/usr/bin/python3
2011-12-03 17:02:47 +02:00
#
# 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.
2011-12-06 00:24:11 +02:00
import collections
2011-12-03 17:02:47 +02:00
import gzip
import json
import optparse
import os
import time
2019-09-04 16:43:12 -03:00
import urllib.request
2011-12-03 17:02:47 +02:00
from ubuntutools.lp.lpapicache import (Distribution, Launchpad,
PackageNotFoundException)
from ubuntutools import getLogger
Logger = getLogger(__name__)
2011-12-03 17:02:47 +02:00
2011-12-08 00:01:56 +02:00
DATA_URL = 'http://qa.ubuntuwire.org/ubuntu-seeded-packages/seeded.json.gz'
2011-12-03 17:02:47 +02:00
2011-12-06 00:24:11 +02:00
def load_index(url):
2011-12-04 00:09:51 +02:00
'''Download a new copy of the image contents index, if necessary,
and read it.
'''
2011-12-03 17:02:47 +02:00
cachedir = os.path.expanduser('~/.cache/ubuntu-dev-tools')
2011-12-06 00:24:11 +02:00
fn = os.path.join(cachedir, 'seeded.json.gz')
2011-12-03 17:02:47 +02:00
if (not os.path.isfile(fn)
2011-12-06 00:24:11 +02:00
or time.time() - os.path.getmtime(fn) > 60 * 60 * 2):
2011-12-03 17:02:47 +02:00
if not os.path.isdir(cachedir):
os.makedirs(cachedir)
2019-09-04 16:43:12 -03:00
urllib.request.urlretrieve(url, fn)
2011-12-03 17:02:47 +02:00
try:
with gzip.open(fn, 'r') as f:
return json.load(f)
2019-09-04 16:43:12 -03:00
except Exception as e:
Logger.error("Unable to parse seed data: %s. "
"Deleting cached data, please try again.",
str(e))
os.unlink(fn)
2011-12-03 17:02:47 +02:00
2011-12-06 00:24:11 +02:00
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)
2019-09-04 16:43:12 -03:00
except PackageNotFoundException as e:
2011-12-06 00:24:11 +02:00
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_)
2019-09-04 16:43:12 -03:00
for flavor, types in present.items():
2011-12-06 00:24:11 +02:00
if len(types) > 1:
types.discard('supported')
output = [' %s: %s' % (flavor, ', '.join(sorted(types)))
2019-09-04 16:43:12 -03:00
for flavor, types in present.items()]
output.sort()
return '\n'.join(output)
2011-12-06 00:24:11 +02:00
def output_binaries(index, binaries):
'''Print binaries found in index'''
for binary in binaries:
if binary in index:
Logger.info("%s is seeded in:" % binary)
Logger.info(present_on(index[binary]))
2011-12-06 00:24:11 +02:00
else:
Logger.info("%s is not seeded (and may not exist)." % binary)
2011-12-06 00:24:11 +02:00
def output_by_source(index, by_source):
'''Logger.Info(binaries found in index. Grouped by source'''
2019-09-04 16:43:12 -03:00
for source, binaries in by_source.items():
2011-12-06 00:24:11 +02:00
seen = False
if not binaries:
Logger.info("Status unknown: No binary packages built by the latest "
"%s.\nTry again using -b and the expected binary packages."
% source)
continue
2011-12-06 00:24:11 +02:00
for binary in binaries:
if binary in index:
seen = True
Logger.info("%s (from %s) is seeded in:" % (binary, source))
Logger.info(present_on(index[binary]))
2011-12-06 00:24:11 +02:00
if not seen:
Logger.info("%s's binaries are not seeded." % source)
2011-12-06 00:24:11 +02:00
2011-12-03 17:02:47 +02:00
def main():
2011-12-04 00:09:51 +02:00
'''Query which images the specified packages are on'''
2011-12-03 17:02:47 +02:00
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)")
2011-12-06 00:29:32 +02:00
parser.add_option('-u', '--data-url', metavar='URL',
2011-12-06 00:24:11 +02:00
default=DATA_URL,
2011-12-06 00:29:32 +02:00
help='URL for the seeded packages index. '
2011-12-06 00:24:11 +02:00
'Default: UbuntuWire')
2011-12-03 17:02:47 +02:00
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)
2011-12-03 17:02:47 +02:00
if options.binary:
2011-12-06 00:24:11 +02:00
output_binaries(index, args)
2011-12-03 17:02:47 +02:00
else:
2011-12-06 00:24:11 +02:00
binaries = resolve_binaries(args)
output_by_source(index, binaries)
2011-12-03 17:02:47 +02:00
if __name__ == '__main__':
main()