File: update-metapackage.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 (439 lines) | stat: -rwxr-xr-x 16,357 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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2004, 2005, 2006, 2007, 2008 Canonical Ltd.
# Copyright (c) 2006 Gustavo Franco
#
# This file is part of Germinate.
#
# 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.

# TODO:
# - Exclude essential packages from dependencies

import sys
import re
import os
import getopt
import logging
import ConfigParser
import subprocess

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

def usage(f):
    print >>f, """Usage: update-metapackage.py [options] [dist]

Update metapackage lists for distribution 'dist' as defined in
update.cfg.

Options:

  -h, --help            Print this help message and exit.
  -o, --output-directory=DIR
                        Output in specific directory.
  --version             Output version information and exit.
  --nodch               Don't modify debian/changelog.
  --bzr                 Fetch seeds using bzr. Requires bzr to be installed.
"""

bzr = False
nodch = False
outdir = None

try:
    opts, args = getopt.getopt(sys.argv[1:], "ho:",
                               ["help", "version", "bzr", "nodch", "output-directory="])
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 == "--bzr":
        bzr = True
    elif option == "--nodch":
        nodch = True
    elif option in ("-o", "--output-directory"):
        outdir = value

if not outdir:
    outdir = "."

if not os.path.exists('debian/control'):
    raise RuntimeError('must be run from the top level of a source package')
this_source = None
control = open('debian/control')
for line in control:
    if line.startswith('Source:'):
        this_source = line[7:].strip()
        break
    elif line == '':
        break
if this_source is None:
    raise RuntimeError('cannot find Source: in debian/control')
if not this_source.endswith('-meta'):
    raise RuntimeError('source package name must be *-meta')
metapackage = this_source[:-5]

print "[info] Initialising %s-* package lists update..." % metapackage
    
config = ConfigParser.SafeConfigParser()
config_file = open('update.cfg')
config.readfp(config_file)
config_file.close()

if len(args) > 0:
    dist = args[0]
else:
    dist = config.get('DEFAULT', 'dist')
        
seeds = config.get(dist, 'seeds').split()
try:
    output_seeds = config.get(dist, 'output_seeds').split()
except ConfigParser.NoOptionError:
    output_seeds = list(seeds)
architectures = config.get(dist, 'architectures').split()
try:
    archive_base_default = config.get(dist, 'archive_base/default')
    archive_base_default = re.split(r'[, ]+', archive_base_default)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
    archive_base_default = None

archive_base = {}
for arch in architectures:
    try:
        archive_base[arch] = config.get(dist, 'archive_base/%s' % arch)
        archive_base[arch] = re.split(r'[, ]+', archive_base[arch])
    except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
        if archive_base_default is not None:
            archive_base[arch] = archive_base_default
        else:
            raise RuntimeError('no archive_base configured for %s' % arch)

if bzr and config.has_option("%s/bzr" % dist, 'seed_base'):
    seed_base = config.get("%s/bzr" % dist, 'seed_base')
else:
    seed_base = config.get(dist, 'seed_base')
seed_base = re.split(r'[, ]+', seed_base)
if bzr and config.has_option("%s/bzr" % dist, 'seed_dist'):
    seed_dist = config.get("%s/bzr" % dist, 'seed_dist')
elif config.has_option(dist, 'seed_dist'):
    seed_dist = config.get(dist, 'seed_dist')
else:
    seed_dist = dist
seed_entry = re.compile(' *\* *(?P<package>\S+) *(\[(?P<arches>[^]]*)\])? *(#.*)?')
components = config.get(dist, 'components').split()

def seed_packages(germinator_list, seed, seed_text):
    if config.has_option(dist, "seed_map/%s" % seed):
        mapped_seeds = config.get(dist, "seed_map/%s" % seed).split()
    else:
        mapped_seeds = []
        task_seeds_re = re.compile('^Task-Seeds:\s*(.*)', re.I)
        for line in seed_text:
            task_seeds_match = task_seeds_re.match(line)
            if task_seeds_match is not None:
                mapped_seeds = task_seeds_match.group(1).split()
                break
        if seed not in mapped_seeds:
            mapped_seeds.append(seed)
    packages = []
    for mapped_seed in mapped_seeds:
        packages.extend(germinator_list[mapped_seed])
    return packages

def metapackage_name(seed, seed_text):
    if config.has_option(dist, "metapackage_map/%s" % seed):
        return config.get(dist, "metapackage_map/%s" % seed)
    else:
        task_meta_re = re.compile('^Task-Metapackage:\s*(.*)', re.I)
        for line in seed_text:
            task_meta_match = task_meta_re.match(line)
            if task_meta_match is not None:
                return task_meta_match.group(1)
        return "%s-%s" % (metapackage, seed)

debootstrap_version_file = 'debootstrap-version'

def get_debootstrap_version():
    version = os.popen("dpkg-query -W --showformat '${Version}' debootstrap").read()
    if not version:
        raise RuntimeError('debootstrap does not appear to be installed')

    return version

def debootstrap_packages(arch):
    env = dict(os.environ)
    if 'PATH' in env:
        env['PATH'] = '/usr/sbin:/sbin:%s' % env['PATH']
    else:
        env['PATH'] = '/usr/sbin:/sbin:/usr/bin:/bin'
    debootstrap = subprocess.Popen(
        ['debootstrap', '--arch', arch, '--print-debs',
         dist, 'debootstrap-dir', archive_base[arch][0]],
        stdout=subprocess.PIPE, env=env, stderr=subprocess.PIPE)
    (debootstrap_stdout, _) = debootstrap.communicate()
    if debootstrap.returncode != 0:
        raise RuntimeError('Unable to retrieve package list from debootstrap: %s' % debootstrap_stdout)    

    packages = filter(None, debootstrap_stdout.split())
    # sometimes debootstrap gives empty packages / multiple separators   
    packages.sort()

    return packages

def check_debootstrap_version():
    if os.path.exists(debootstrap_version_file):
        old_debootstrap_version = open(debootstrap_version_file).read().strip()
        debootstrap_version = get_debootstrap_version()
        failed = os.system("dpkg --compare-versions '%s' ge '%s'" % (debootstrap_version,
                                                                     old_debootstrap_version))
        if failed:
            raise RuntimeError('Installed debootstrap is older than in the previous version! (%s < %s)' % (
                debootstrap_version,
                old_debootstrap_version
                ))

def update_debootstrap_version():
    open(debootstrap_version_file, 'w').write(get_debootstrap_version() + '\n')

def format_changes(items):
    by_arch = {}
    for (pkg, arch) in items:
        by_arch.setdefault(pkg, [])
        by_arch[pkg].append(arch)
    all_pkgs = by_arch.keys()
    all_pkgs.sort()
    chunks = []
    for pkg in all_pkgs:
        arches = by_arch[pkg]
        if set(architectures) - set(arches):
            # only some architectures
            arches.sort()
            chunks.append('%s [%s]' % (pkg, ' '.join(arches)))
        else:
            # all architectures
            chunks.append(pkg)
    return ', '.join(chunks)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(levelname)s%(message)s'))
logger.addHandler(handler)

check_debootstrap_version()

additions = {}
removals = {}
moves = {}
apt_pkg.InitConfig()
apt_pkg.InitSystem()
for architecture in architectures:
    print "[%s] Downloading available package lists..." % architecture
    apt_pkg.Config.Set("APT::Architecture", architecture)
    germinator = Germinator()
    Germinate.Archive.TagFile(archive_base[architecture], archive_base_default).feed(
        germinator, [dist], components, architecture, cleanup=True)
    debootstrap_base = set(debootstrap_packages(architecture))

    print "[%s] Loading seed lists..." % architecture
    try:
        seed_names, seed_inherit, seed_branches = germinator.parseStructure(
            seed_base, seed_dist, bzr)
    except Germinate.seeds.SeedError:
        sys.exit(1)
    seed_names, seed_inherit, seed_branches = germinator.expandInheritance(
        seed_names, seed_inherit, seed_branches)
    needed_seeds = []
    for seed_name in seeds:
        for inherit in seed_inherit[seed_name]:
            if inherit not in needed_seeds:
                needed_seeds.append(inherit)
        if seed_name not in needed_seeds:
            needed_seeds.append(seed_name)
    seed_texts = {}
    for seed_name in needed_seeds:
        try:
            seed_fd = Germinate.seeds.open_seed(seed_base, seed_branches,
                                                seed_name, bzr)
        except Germinate.seeds.SeedError:
            sys.exit(1)
        seed_texts[seed_name] = seed_fd.readlines()
        seed_fd.close()
        germinator.plantSeed(seed_texts[seed_name], architecture, seed_name,
                             list(seed_inherit[seed_name]))

    print "[%s] Merging seeds with available package lists..." % architecture
    for seed_name in output_seeds:
        meta_name = metapackage_name(seed_name, seed_texts[seed_name])

        output_filename = os.path.join(outdir, '%s-%s' % (seed_name,architecture))
        old_list = None
        if os.path.exists(output_filename):
            old_list = set(map(str.strip,open(output_filename).readlines()))
            os.rename(output_filename, output_filename + '.old')

        # work on the depends
        new_list = []
        for package in seed_packages(germinator.seed, seed_name,
                                     seed_texts[seed_name]):
            if package == meta_name:
                print "%s/%s: Skipping package %s (metapackage)" % (seed_name,architecture,package)
            elif seed_name == 'minimal' and package not in debootstrap_base:
                print "%s/%s: Skipping package %s (package not in debootstrap)" % (seed_name,architecture,package)
            elif germinator.packages[package]["Essential"]:
                print "%s/%s: Skipping package %s (essential)" % (seed_name,architecture,package)
            else:
                new_list.append(package)

        new_list.sort()
        output = open(output_filename, 'w')
        for package in new_list:
            output.write(package)
            output.write('\n')
        output.close()

        # work on the recommends
        old_recommends_list = None
        new_recommends_list = []
        for package in seed_packages(germinator.seedrecommends, seed_name,
                                     seed_texts[seed_name]):
            if package == meta_name:
                print "%s/%s: Skipping package %s (metapackage)" % (seed_name,architecture,package)
                continue
            if seed_name == 'minimal' and package not in debootstrap_base:
                print "%s/%s: Skipping package %s (package not in debootstrap)" % (seed_name,architecture,package)
            else:
                new_recommends_list.append(package)

        new_recommends_list.sort()
        seed_name_recommends = '%s-recommends' % seed_name
        output_recommends_filename = os.path.join(outdir, '%s-%s' % (seed_name_recommends,architecture))
        if os.path.exists(output_recommends_filename):
            old_recommends_list = set(map(str.strip,open(output_recommends_filename).readlines()))
            os.rename(output_recommends_filename, output_recommends_filename + '.old')

        output = open(output_recommends_filename, 'w')
        for package in new_recommends_list:
            output.write(package)
            output.write('\n')
        output.close()


        # Calculate deltas
        merged = {}
        recommends_merged = {}
        if old_list is not None:
            for package in new_list:
                merged.setdefault(package, 0)
                merged[package] += 1
            for package in old_list:
                merged.setdefault(package, 0)
                merged[package] -= 1
        if old_recommends_list is not None:
            for package in new_recommends_list:
                recommends_merged.setdefault(package, 0)
                recommends_merged[package] += 1
            for package in old_recommends_list:
                recommends_merged.setdefault(package, 0)
                recommends_merged[package] -= 1

        mergeditems = merged.items()
        mergeditems.sort()
        for package, value in mergeditems:
            #print package, value
            if value == 1:
                if recommends_merged.get(package, 0) == -1:
                    moves.setdefault(package,[])
                    moves[package].append([seed_name, architecture])
                    recommends_merged[package] += 1
                else:
                    additions.setdefault(package,[])
                    additions[package].append([seed_name, architecture])
            elif value == -1:
                if recommends_merged.get(package, 0) == 1:
                    moves.setdefault(package,[])
                    moves[package].append([seed_name_recommends, architecture])
                    recommends_merged[package] -= 1
                else:
                    removals.setdefault(package,[])
                    removals[package].append([seed_name, architecture])

        mergedrecitems = recommends_merged.items()
        mergedrecitems.sort()
        for package, value in mergedrecitems:
            #print package, value
            if value == 1:
                additions.setdefault(package,[])
                additions[package].append([seed_name_recommends, architecture])
            elif value == -1:
                removals.setdefault(package,[])
                removals[package].append([seed_name_recommends, architecture])

if not nodch and (additions or removals or moves):
    dch_help = os.popen('dch --help')
    have_U = '-U' in dch_help.read()
    dch_help.close()
    if have_U:
        os.system("dch -iU 'Refreshed dependencies'")
    else:
        os.system("dch -i 'Refreshed dependencies'")
    changes = []
    addition_keys = additions.keys()
    addition_keys.sort()
    for package in addition_keys:
        changes.append('Added %s to %s' %
                       (package, format_changes(additions[package])))
    removal_keys = removals.keys()
    removal_keys.sort()
    for package in removal_keys:
        changes.append('Removed %s from %s' %
                       (package, format_changes(removals[package])))
    move_keys = moves.keys()
    move_keys.sort()
    for package in move_keys:
        # TODO: We should really list where it moved from as well, but that
        # gets wordy very quickly, and at the moment this is only
        # implemented for depends->recommends or vice versa. In future,
        # using this for moves between seeds might also be useful.
        changes.append('Moved %s to %s' %
                       (package, format_changes(moves[package])))
    for change in changes:
        print change
        os.system("dch -a '%s'" % change)
    update_debootstrap_version()
else:
    if not nodch:
        print "No changes found"