File: germinate.py

package info (click to toggle)
germinate 1.5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 244 kB
  • ctags: 181
  • sloc: python: 2,070; makefile: 35
file content (534 lines) | stat: -rwxr-xr-x 17,774 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""Update list files from the Wiki."""

# Copyright (c) 2004, 2005, 2006, 2007, 2008 Canonical Ltd.
#
# Germinate is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Germinate is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Germinate; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301, USA.

import gzip
import os
import shutil
import sys
import urllib2
import getopt
import logging
import codecs
import cStringIO

try:
    set # introduced in 2.4
except NameError:
    import sets
    set = sets.Set

import apt_pkg

from Germinate import Germinator
import Germinate.Archive
import Germinate.seeds
import Germinate.version


# Where do we get up-to-date seeds from?
SEEDS = ["http://people.ubuntu.com/~ubuntu-archive/seeds/"]
SEEDS_BZR = ["http://bazaar.launchpad.net/~ubuntu-core-dev/ubuntu-seeds/"]
RELEASE = "ubuntu.intrepid"

# If we need to download Packages.gz and/or Sources.gz, where do we get
# them from?
MIRRORS = []
SOURCE_MIRRORS = []
DEFAULT_MIRROR = "http://archive.ubuntu.com/ubuntu/"
DEFAULT_SOURCE_MIRROR = None
DIST = ["intrepid"]
COMPONENTS = ["main", "restricted"]
ARCH = "i386"

CHECK_IPV6 = False

# If we need to download a new IPv6 dump, where do we get it from?
IPV6DB = "http://debdev.fabbione.net/stat/"


def open_ipv6_tag_file(filename):
    """Download the daily IPv6 db dump if needed, and open it."""
    if os.path.exists(filename):
        return open(filename, "r")

    print "Downloading", filename, "file ..."
    url = IPV6DB + filename + ".gz"
    url_f = urllib2.urlopen(url)
    url_data = cStringIO.StringIO(url_f.read())
    url_f.close()
    print "Decompressing", filename, "file ..."
    gzip_f = gzip.GzipFile(fileobj=url_data)
    f = open(filename, "w")
    for line in gzip_f:
        print >>f, line,
    f.close()
    gzip_f.close()
    url_data.close()

    return open(filename, "r")

def write_list(whyname, filename, g, pkgset):
    pkglist = list(pkgset)
    pkglist.sort()

    pkg_len = len("Package")
    src_len = len("Source")
    why_len = len("Why")
    mnt_len = len("Maintainer")

    for pkg in pkglist:
        _pkg_len = len(pkg)
        if _pkg_len > pkg_len: pkg_len = _pkg_len

        _src_len = len(g.packages[pkg]["Source"])
        if _src_len > src_len: src_len = _src_len

        _why_len = len(g.why[whyname][pkg][0])
        if _why_len > why_len: why_len = _why_len

        _mnt_len = len(g.packages[pkg]["Maintainer"])
        if _mnt_len > mnt_len: mnt_len = _mnt_len

    size = 0
    installed_size = 0

    pkglist.sort()
    f = codecs.open(filename, "w", "utf8", "replace")
    print >>f, "%-*s | %-*s | %-*s | %-*s | %-15s | %-15s" % \
          (pkg_len, "Package",
           src_len, "Source",
           why_len, "Why",
           mnt_len, "Maintainer",
           "Deb Size (B)",
           "Inst Size (KB)")
    print >>f, ("-" * pkg_len) + "-+-" + ("-" * src_len) + "-+-" \
          + ("-" * why_len) + "-+-" + ("-" * mnt_len) + "-+-" \
          + ("-" * 15) + "-+-" + ("-" * 15) + "-"
    for pkg in pkglist:
        size += g.packages[pkg]["Size"]
        installed_size += g.packages[pkg]["Installed-Size"]
        print >>f, "%-*s | %-*s | %-*s | %-*s | %15d | %15d" % \
              (pkg_len, pkg,
               src_len, g.packages[pkg]["Source"],
               why_len, g.why[whyname][pkg][0],
               mnt_len, g.packages[pkg]["Maintainer"],
               g.packages[pkg]["Size"],
               g.packages[pkg]["Installed-Size"])
    print >>f, ("-" * (pkg_len + src_len + why_len + mnt_len + 9)) + "-+-" \
          + ("-" * 15) + "-+-" + ("-" * 15) + "-"
    print >>f, "%*s | %15d | %15d" % \
          ((pkg_len + src_len + why_len + mnt_len + 9), "",
           size, installed_size)

    f.close()

def write_source_list(filename, g, srcset):
    global CHECK_IPV6

    srclist = list(srcset)
    srclist.sort()

    src_len = len("Source")
    mnt_len = len("Maintainer")
    ipv6_len = len("IPv6 status")

    for src in srclist:
        _src_len = len(src)
        if _src_len > src_len: src_len = _src_len

        _mnt_len = len(g.sources[src]["Maintainer"])
        if _mnt_len > mnt_len: mnt_len = _mnt_len

        if CHECK_IPV6:
            _ipv6_len = len(g.sources[src]["IPv6"])
            if _ipv6_len > ipv6_len: ipv6_len = _ipv6_len

    srclist.sort()
    f = codecs.open(filename, "w", "utf8", "replace")

    format = "%-*s | %-*s"
    header_args = [src_len, "Source", mnt_len, "Maintainer"]
    separator = ("-" * src_len) + "-+-" + ("-" * mnt_len) + "-"
    if CHECK_IPV6:
        format += " | %-*s"
        header_args.extend((ipv6_len, "IPv6 status"))
        separator += "+-" + ("-" * ipv6_len) + "-"

    print >>f, format % tuple(header_args)
    print >>f, separator
    for src in srclist:
        args = [src_len, src, mnt_len, g.sources[src]["Maintainer"]]
        if CHECK_IPV6:
            args.extend((ipv6_len, g.sources[src]["IPv6"]))
        print >>f, format % tuple(args)

    f.close()

def write_rdepend_list(filename, g, pkg):
    f = open(filename, "w")
    print >>f, pkg
    _write_rdepend_list(f, g, pkg, "", done=set())
    f.close()

def _write_rdepend_list(f, g, pkg, prefix, stack=None, done=None):
    if stack is None:
        stack = []
    else:
        stack = list(stack)
        if pkg in stack:
            print >>f, prefix + "! loop"
            return
    stack.append(pkg)

    if done is None:
        done = set()
    elif pkg in done:
        print >>f, prefix + "! skipped"
        return
    done.add(pkg)

    for seed in g.seeds:
        if pkg in g.seed[seed]:
            print >>f, prefix + "*", seed.title(), "seed"

    if "Reverse-Depends" not in g.packages[pkg]:
        return

    for field in ("Pre-Depends", "Depends", "Recommends",
                  "Build-Depends", "Build-Depends-Indep"):
        if field not in g.packages[pkg]["Reverse-Depends"]:
            continue

        i = 0
        print >>f, prefix + "*", "Reverse", field + ":"
        for dep in g.packages[pkg]["Reverse-Depends"][field]:
            i += 1
            print >>f, prefix + " +- " + dep
            if field.startswith("Build-"):
                continue

            if i == len(g.packages[pkg]["Reverse-Depends"][field]):
                extra = "    "
            else:
                extra = " |  "
            _write_rdepend_list(f, g, dep, prefix + extra, stack, done)

def write_prov_list(filename, provdict):
    provides = provdict.keys()
    provides.sort()

    f = open(filename, "w")
    for prov in provides:
        print >>f, prov

        provlist = list(provdict[prov])
        provlist.sort()
        for pkg in provlist:
            print >>f, "\t%s" % (pkg,)
        print >>f
    f.close()

def write_structure(filename, structure):
    f = open(filename, "w")
    for line in structure:
        print >>f, line
    f.close()

def write_seedtext(filename, seedtext):
    f = open(filename, "w")
    for line in seedtext:
        print >>f, line.rstrip('\n')

def write_dot(filename, seednames, seedinherit):
    """Write a dot file to represent the structure of the seeds"""

    #Initialize dot document
    dotfile = codecs.open(filename, "w", "utf8", "replace")
    print >>dotfile, "digraph structure {"
    print >>dotfile, "    node [color=lightblue2, style=filled];"

    heritage = []

    for seed in seednames:
        for inherit in seedinherit[seed]:
            print >>dotfile, "    \"%s\" -> \"%s\";" % (inherit, seed)

    print >>dotfile, "}"
    dotfile.close()

def usage(f):
    print >>f, """Usage: germinate.py [options]

Options:

  -h, --help            Print this help message and exit.
  --version             Output version information and exit.
  -v, --verbose         Be more verbose when processing seeds.
  -S, --seed-source=SOURCE
                        Fetch seeds from SOURCE
                        (default: %s).
  -s, --seed-dist=DIST  Fetch seeds for distribution DIST (default: %s).
  -m, --mirror=MIRROR   Get package lists from MIRROR
                        (default: %s).
  --source-mirror=MIRROR
                        Get source package lists from mirror
                        (default: value of --mirror).
  -d, --dist=DIST       Operate on distribution DIST (default: %s).
  -a, --arch=ARCH       Operate on architecture ARCH (default: %s).
  -c, --components=COMPS
                        Operate on components COMPS (default: %s).
  -i, --ipv6            Check IPv6 status of source packages.
  --bzr                 Fetch seeds using bzr. Requires bzr to be installed.
  --cleanup             Don't cache Packages or Sources files.
  --no-rdepends         Disable reverse-dependency calculations.
  --seed-packages=PARENT/PKG,PARENT/PKG,...
                        Treat each PKG as a seed by itself, inheriting from
                        PARENT.
""" % (",".join(SEEDS), RELEASE, DEFAULT_MIRROR, ",".join(DIST), ARCH,
       ",".join(COMPONENTS))


def main():
    global SEEDS, SEEDS_BZR, RELEASE
    global DEFAULT_MIRROR, DEFAULT_SOURCE_MIRROR, SOURCE_MIRRORS, MIRRORS
    global DIST, ARCH, COMPONENTS, CHECK_IPV6
    verbose = False
    bzr = False
    cleanup = False
    want_rdepends = True
    seed_packages = ()
    seeds_set = False

    g = Germinator()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hvS:s:m:d:c:a:i",
                                   ["help",
                                    "version",
                                    "verbose",
                                    "seed-source=",
                                    "seed-dist=",
                                    "mirror=",
                                    "source-mirror=",
                                    "dist=",
                                    "components=",
                                    "arch=",
                                    "ipv6",
                                    "bzr",
                                    "cleanup",
                                    "no-rdepends",
                                    "seed-packages="])
    except getopt.GetoptError:
        usage(sys.stderr)
        sys.exit(2)

    for option, value in opts:
        if option in ("-h", "--help"):
            usage(sys.stdout)
            sys.exit()
        elif option == "--version":
            print "%s %s" % (os.path.basename(sys.argv[0]),
                             Germinate.version.VERSION)
            sys.exit()
        elif option in ("-v", "--verbose"):
            verbose = True
        elif option in ("-S", "--seed-source"):
            SEEDS = value.split(",")
            seeds_set = True
        elif option in ("-s", "--seed-dist"):
            RELEASE = value
        elif option in ("-m", "--mirror"):
            if not value.endswith("/"):
                value += "/"
            MIRRORS.append(value)
        elif option == "--source-mirror":
            if not value.endswith("/"):
                value += "/"
            SOURCE_MIRRORS.append(value)
        elif option in ("-d", "--dist"):
            DIST = value.split(",")
        elif option in ("-c", "--components"):
            COMPONENTS = value.split(",")
        elif option in ("-a", "--arch"):
            ARCH = value
        elif option in ("-i", "--ipv6"):
            CHECK_IPV6 = True
        elif option == "--bzr":
            bzr = True
            if not seeds_set:
                SEEDS = SEEDS_BZR
        elif option == "--cleanup":
            cleanup = True
        elif option == "--no-rdepends":
            want_rdepends = False
        elif option == "--seed-packages":
            seed_packages = value.split(',')

    if not MIRRORS:
        MIRRORS.append(DEFAULT_MIRROR)

    logger = logging.getLogger()
    if verbose:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(Germinator.PROGRESS)
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(logging.Formatter('%(levelname)s%(message)s'))
    logger.addHandler(handler)

    apt_pkg.InitConfig()
    apt_pkg.Config.Set("APT::Architecture", ARCH)
    apt_pkg.InitSystem()

    Germinate.Archive.TagFile(MIRRORS, SOURCE_MIRRORS).feed(
        g, DIST, COMPONENTS, ARCH, cleanup)

    if CHECK_IPV6:
        g.parseIPv6(open_ipv6_tag_file("dailydump"))

    if os.path.isfile("hints"):
        g.parseHints(open("hints"))

    try:
        blacklist = Germinate.seeds.open_seed(SEEDS, RELEASE, "blacklist", bzr)
    except Germinate.seeds.SeedError:
        sys.exit(1)
    if blacklist is not None:
        g.parseBlacklist(blacklist)

    try:
        seednames, seedinherit, seedbranches = g.parseStructure(
            SEEDS, RELEASE, bzr)
    except Germinate.seeds.SeedError:
        sys.exit(1)

    write_dot("structure.dot", seednames, seedinherit)

    seednames, seedinherit, seedbranches = g.expandInheritance(
        seednames, seedinherit, seedbranches)

    seedtexts = {}
    for seedname in seednames:
        try:
            seed_fd = Germinate.seeds.open_seed(SEEDS, seedbranches, seedname,
                                                bzr)
        except Germinate.seeds.SeedError:
            sys.exit(1)
        seedtexts[seedname] = seed_fd.readlines()
        seed_fd.close()
        g.plantSeed(seedtexts[seedname],
                    ARCH, seedname, list(seedinherit[seedname]), RELEASE)
    for seed_package in seed_packages:
        (parent, pkg) = seed_package.split('/')
        g.plantSeed([" * " + pkg], ARCH, pkg,
                    seedinherit[parent] + [parent], RELEASE)
        seednames.append(pkg)
    g.prune()
    g.grow()
    g.addExtras(RELEASE)
    if want_rdepends:
        g.reverseDepends()

    seednames_extra = list(seednames)
    seednames_extra.append('extra')
    for seedname in seednames_extra:
        write_list(seedname, seedname,
                   g, set(g.seed[seedname]) | set(g.seedrecommends[seedname]) |
                      set(g.depends[seedname]))
        write_list(seedname, seedname + ".seed",
                   g, g.seed[seedname])
        write_list(seedname, seedname + ".seed-recommends",
                   g, g.seedrecommends[seedname])
        write_list(seedname, seedname + ".depends",
                   g, g.depends[seedname])
        write_list(seedname, seedname + ".build-depends",
                   g, g.build_depends[seedname])

        if seedname != "extra" and seedname in seedtexts:
            write_seedtext(seedname + ".seedtext", seedtexts[seedname])
            write_source_list(seedname + ".sources",
                              g, g.sourcepkgs[seedname])
        write_source_list(seedname + ".build-sources",
                          g, g.build_sourcepkgs[seedname])

    all = set()
    sup = set()
    all_srcs = set()
    sup_srcs = set()
    for seedname in seednames:
        all.update(g.seed[seedname])
        all.update(g.seedrecommends[seedname])
        all.update(g.depends[seedname])
        all.update(g.build_depends[seedname])
        all_srcs.update(g.sourcepkgs[seedname])
        all_srcs.update(g.build_sourcepkgs[seedname])

        if seedname == g.supported:
            sup.update(g.seed[seedname])
            sup.update(g.seedrecommends[seedname])
            sup.update(g.depends[seedname])
            sup_srcs.update(g.sourcepkgs[seedname])

        # Only include those build-dependencies that aren't already in the
        # dependency outputs for inner seeds of supported. This allows
        # supported+build-depends to be usable as an "everything else"
        # output.
        build_depends = dict.fromkeys(g.build_depends[seedname], True)
        build_sourcepkgs = dict.fromkeys(g.build_sourcepkgs[seedname], True)
        for seed in g.innerSeeds(g.supported):
            build_depends.update(dict.fromkeys(g.seed[seed], False))
            build_depends.update(dict.fromkeys(g.seedrecommends[seed], False))
            build_depends.update(dict.fromkeys(g.depends[seed], False))
            build_sourcepkgs.update(dict.fromkeys(g.sourcepkgs[seed], False))
        sup.update([k for (k, v) in build_depends.iteritems() if v])
        sup_srcs.update([k for (k, v) in build_sourcepkgs.iteritems() if v])

    write_list("all", "all", g, all)
    write_source_list("all.sources", g, all_srcs)

    write_list("all", "%s+build-depends" % g.supported, g, sup)
    write_source_list("%s+build-depends.sources" % g.supported, g, sup_srcs)

    write_list("all", "all+extra", g, g.all)
    write_source_list("all+extra.sources", g, g.all_srcs)

    write_prov_list("provides", g.pkgprovides)

    write_structure("structure", g.structure)

    if os.path.exists("rdepends"):
        shutil.rmtree("rdepends")
    if want_rdepends:
        os.mkdir("rdepends")
        os.mkdir(os.path.join("rdepends", "ALL"))
        for pkg in g.all:
            dirname = os.path.join("rdepends", g.packages[pkg]["Source"])
            if not os.path.exists(dirname):
                os.mkdir(dirname)

            write_rdepend_list(os.path.join(dirname, pkg), g, pkg)
            os.symlink(os.path.join("..", g.packages[pkg]["Source"], pkg),
                       os.path.join("rdepends", "ALL", pkg))

    g.writeBlacklisted("blacklisted")

if __name__ == "__main__":
    main()