~manjo/ubuntu-archive-tools/create-ppa

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
#! /usr/bin/python

from __future__ import print_function

import atexit
import bz2
from contextlib import closing
from optparse import OptionParser
import shutil
import sys
import tempfile
try:
    from urllib.request import urlretrieve
except ImportError:
    from urllib import urlretrieve

import apt_pkg
from launchpadlib.launchpad import Launchpad

import lputils


tempdir = None


def ensure_tempdir():
    global tempdir
    if not tempdir:
        tempdir = tempfile.mkdtemp(prefix="orphaned-sources")
        atexit.register(shutil.rmtree, tempdir)


def decompress_open(tagfile):
    if tagfile.startswith("http:") or tagfile.startswith("ftp:"):
        url = tagfile
        tagfile = urlretrieve(url)[0]

    if tagfile.endswith(".bz2"):
        ensure_tempdir()
        decompressed = tempfile.mktemp(dir=tempdir)
        with closing(bz2.BZ2File(tagfile)) as fin:
            with open(decompressed, "wb") as fout:
                fout.write(fin.read())
        return open(decompressed, "r")
    else:
        return open(tagfile, "r")


def archive_base(archtag):
    if archtag in ("amd64", "i386", "src"):
        return "http://archive.ubuntu.com/ubuntu"
    else:
        return "http://ports.ubuntu.com/ubuntu-ports"


def source_names(options):
    sources = set()
    for component in "main", "restricted", "universe", "multiverse":
        url = "%s/dists/%s/%s/source/Sources.bz2" % (
            archive_base("src"), options.suite, component)
        print("Reading %s ..." % url, file=sys.stderr)
        for section in apt_pkg.TagFile(decompress_open(url)):
            sources.add(section["Package"])
    return sources


def referenced_sources(options):
    sources = set()
    for component in "main", "restricted", "universe", "multiverse":
        for arch in options.architectures:
            archtag = arch.architecture_tag
            for suffix in "", "/debian-installer":
                url = "%s/dists/%s/%s%s/binary-%s/Packages.bz2" % (
                    archive_base(archtag), options.suite, component, suffix,
                    archtag)
                print("Reading %s ..." % url, file=sys.stderr)
                for section in apt_pkg.TagFile(decompress_open(url)):
                    if "Source" in section:
                        sources.add(section["Source"].split(" ", 1)[0])
                    else:
                        sources.add(section["Package"])
    return sources


def main():
    parser = OptionParser(
        description="Check for sources without any remaining binaries.")
    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option("-s", "--suite", help="check this suite")
    options, _ = parser.parse_args()

    options.distribution = "ubuntu"
    options.launchpad = Launchpad.login_anonymously(
        "orphaned-sources", options.launchpad_instance)
    lputils.setup_location(options)

    if options.pocket != "Release":
        parser.error("cannot run on non-release pocket")

    orphaned_sources = source_names(options) - referenced_sources(options)
    for source in sorted(orphaned_sources):
        print(source)


if __name__ == '__main__':
    main()