]> git.donarmstrong.com Git - wannabuild.git/blob - bin/add-sources.py
Use add-sources.py in PATH
[wannabuild.git] / bin / add-sources.py
1 #!/usr/bin/python
2
3 # Given as input a Packages and a Sources file, produces as output a new
4 # Packages containing fake packages which are installable if and only if the
5 # corresponding source package has satisfiable build dependencies.
6
7 # Copyright (C) 2008 Stefano Zacchiroli <zack@debian.org>
8 # This program is free software: you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free Software
10 # Foundation, either version 3 of the License, or (at your option) any later
11 # version.
12
13 # $Id: add-sources.py 5957 2008-08-16 18:32:17Z zack $
14
15 import string
16 import sys
17
18 from optparse import OptionParser
19 from debian_bundle import deb822
20
21 usage = 'Usage: cat Packages | add-sources [OPTION...] Sources ARCH > Packages.new'
22 cli = OptionParser(usage=usage)
23 cli.add_option('-p', '--prefix', dest='prefix', default='source---',
24         help='set the prefix for fake source packages to PREFIX (default: source---)',
25         metavar='PREFIX')
26 (options, args) = cli.parse_args()
27 if len(args) != 2:
28     cli.print_help()
29     sys.exit(2)
30 sources_file = args[0]
31 architecture = args[1]
32
33 def pkg_of_src(src):
34     global architecture, options
35     pkg = deb822.Packages()
36     pkg['Package'] = options.prefix + src['Package']
37
38     def dep_for_me(dep):
39         for_me = None
40         if dep['arch'] is None:
41             for_me = True
42         elif dep['arch']:
43             (polarity, _) = dep['arch'][0]
44             if polarity:    # list is inclusive
45                 for_me = (True, architecture) in dep['arch']
46             else:   # list is exclusive
47                 for_me = not ((False, architecture) in dep['arch'])
48         else:
49             for_me = False
50         return for_me
51
52     def mk_bin_rels(fields, relations):
53         def strip_arch(dep):
54             dep['arch'] = None
55             return dep
56
57         def get_rels(fields, relations):
58             rels = []
59             for name in fields:
60                 if relations.has_key(name):
61                     rels.extend(relations[name])
62             return rels
63
64         src_rels = get_rels(fields, relations)
65         bin_rels = []
66         for or_deps in src_rels:
67             my_or_deps = map(strip_arch, filter(dep_for_me, or_deps))
68             if my_or_deps:
69                 bin_rels.append(my_or_deps)
70
71         return bin_rels
72
73     def str_of_relations(rels):
74         # XXX this is cut and paste from python-debian's deb822.py, more
75         # precisely it matches the str() method of the PkgRelation class
76         # TODO to be removed as soon as python-debian 0.1.12 hits unstable
77         def pp_arch(arch_spec):
78             (excl, arch) = arch_spec
79             if excl:
80                 return arch
81             else:
82                 return '!' + arch
83         def pp_atomic_dep(dep):
84             s = dep['name']
85             if dep.has_key('version') and dep['version'] is not None:
86                 s += ' (%s %s)' % dep['version']
87             if dep.has_key('arch') and dep['arch'] is not None:
88                 s += ' [%s]' % string.join(map(pp_arch, dep['arch']))
89             return s
90         pp_or_dep = lambda deps: string.join(map(pp_atomic_dep, deps), ' | ')
91         return string.join(map(pp_or_dep, rels), ', ')
92
93     for field in ['Version', 'Priority', 'Section', 'Maintainer']:
94         if src.has_key(field):
95             pkg[field] = src[field]
96     bin_depends = mk_bin_rels(['build-depends', 'build-depends-indep'],
97             src.relations)
98     if bin_depends:
99         #pkg['Depends'] = deb822.PkgRelation.str(bin_depends)
100         pkg['Depends'] = str_of_relations(bin_depends)
101     bin_conflicts = mk_bin_rels(['build-conflicts', 'build-conflicts-indep'],
102             src.relations)
103     if bin_conflicts:
104         #pkg['Conflicts'] = deb822.PkgRelation.str(bin_conflicts)
105         pkg['Conflicts'] = str_of_relations(bin_conflicts)
106     pkg['Description'] = 'dummy counterpart of "%s" source package' % \
107             src['Package']
108     pkg['Description'] += "\n I don't exist, go away."
109     pkg['Architecture'] = 'all'
110
111     return pkg
112
113 #for pkg in deb822.Packages.iter_paragraphs(sys.stdin):
114 for line in sys.stdin:
115     print line,
116 print
117 for src in deb822.Sources.iter_paragraphs(file(sources_file)):
118     if src['Architecture'] in ['any', 'all'] \
119             or architecture in src['Architecture'].split():
120         pkg = pkg_of_src(src)
121         print pkg
122