summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Packages.py
blob: ca198702ccac18812e09d403ba7f9d0020154ef8 (plain)
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
import copy, gzip, lxml.etree, re, urllib
import os
import Bcfg2.Server.Plugin

# build sources.list?
# caching
# pinning
# multi apt-source from xml

def apt_source_from_xml(xsource):
    ret = dict()
    for key, tag in [('groups', 'Group'), ('components', 'Component'),
                     ('arches', 'Arch')]:
        ret[key] = [item.text for item in xsource.findall(tag)]
    ret['version'] = xsource.find('Version').text
    ret['url'] = xsource.find('URL').text
    return ret

class APTSource(object):
    def __init__(self, basepath, url, version, arches, components, groups):
        self.basepath = basepath
        self.version = version
        self.components = components
        self.url = url
        self.urls = ["%s/dists/%s/%s/binary-%s/Packages.gz" % \
                     (url, version, part, arch) for part in components \
                     for arch in arches]
        self.files = [self.mk_fname(url) for url in self.urls]
        self.groups = groups
        self.deps = dict()
        self.provides = {}

    def get_aptsrc(self):
        return ["deb %s %s %s" % (self.url, self.version,
                                  " ".join(self.components)),
                "deb-src %s %s %s" % (self.url, self.version,
                                      " ".join(self.components))]

    def mk_fname(self, url):
        return "%s/%s" % (self.basepath, url.replace('/', '_'))

    def read_files(self):
        bdeps = dict()
        bprov = dict()
        for fname in self.files:
            bin = [x for x in fname.split('_') if x.startswith('binary-')][0][7:]
            if bin not in bdeps:
                bdeps[bin] = dict()
                bprov[bin] = dict()
            try:
                reader = gzip.GzipFile(fname)
            except:
                print "failed to read file %s" % fname
                continue
            for line in reader.readlines():
                words = line.strip().split(':', 1)
                if words[0] == 'Package':
                    pkgname = words[1].strip().rstrip()
                elif words[0] == 'Depends':
                    bdeps[bin][pkgname] = []
                    for dep in words[1].split(','):
                        raw_dep = re.sub('\(.*\)', '', dep)
                        if '|' in raw_dep:
                            # FIXME hack alert
                            raw_dep = raw_dep.split('|')[0]
                        raw_dep = raw_dep.rstrip().strip()
                        bdeps[bin][pkgname].append(raw_dep)
                elif words[0] == 'Provides':
                    for pkg in words[1].split(','):
                        dname = pkg.rstrip().strip()
                        if dname not in bprov[bin]:
                            bprov[bin][dname] = set()
                        bprov[bin][dname].add(pkgname)

        pkgnames = set()
        self.deps['global'] = dict()
        self.provides['global'] = dict()
        for bin in bdeps:
            [pkgnames.add(pname) for pname in bdeps[bin]]
            self.deps[bin] = dict()
            self.provides[bin] = dict()
        for pkgname in pkgnames:
            pset = set()
            for bin in bdeps:
                if pkgname not in bdeps[bin]:
                    bdeps[bin][pkgname] = []
                pset.add(tuple(bdeps[bin][pkgname]))
            if len(pset) == 1:
                self.deps['global'][pkgname] = pset.pop()
            else:
                for bin in bdeps:
                    self.deps[bin][pkgname] = bdeps[bin][pkgname]
        provided = set()
        for bin in bprov:
            for prov in bprov[bin]:
                provided.add(prov)
        for prov in provided:
            prset = set()
            for bin in bprov:
                if prov not in bprov[bin]:
                    continue
                prset.add(tuple(bprov[bin].get(prov, ())))
            if len(prset) == 1:
                self.provides['global'][prov] = prset.pop()
            else:
                for bin in bprov:
                    self.provides[bin][prov] = bprov[bin].get(prov, ())


    def update(self):
        for url in self.urls:
            print "updating", url
            fname = self.mk_fname(url)
            data = urllib.urlopen(url).read()
            output = file(fname, 'w').write(data)

    def applies(self, metadata):
        return len([g for g in metadata.groups if g in self.groups]) \
               == len(self.groups)

    def find_provides(self, metadata, pkgname):
        arches = [ar for ar in self.provides if ar in metadata.groups]
        for arch in ['global'] + arches:
            if pkgname in self.provides[arch]:
                # FIXME next round of provides HACK alert
                return self.provides[arch][pkgname][0]
        return False

    def get_deps(self, metadata, pkgname):
        data = False
        if pkgname in self.deps['global']:
            data = self.deps['global'][pkgname]
        for arch in [ar for ar in self.deps if ar in metadata.groups]:
            if pkgname in self.deps[arch]:
                data = self.deps[arch][pkgname]
        if data:
            ret = list()
            for item in data:
                prov = self.find_provides(metadata, item)
                if prov:
                    ret.append(prov)
                else:
                    ret.append(item)
            return tuple(ret)
        return False

class Packages(Bcfg2.Server.Plugin.Plugin,
               Bcfg2.Server.Plugin.StructureValidator,
               Bcfg2.Server.Plugin.Generator):
    name = 'Packages'
    experimental = True
    
    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.StructureValidator.__init__(self)
        Bcfg2.Server.Plugin.Generator.__init__(self)
        cachepath = self.data + '/cache'
        if not os.path.exists(cachepath):
            # create cache directory if needed
            os.mkdir(cachepath)
        xdata = lxml.etree.parse(self.data + '/config.xml').getroot()
        self.sources = []
        for s in xdata.findall('APTSource'):
            self.sources.append(APTSource(cachepath, **apt_source_from_xml(s)))
        for source in self.sources:
            try:
                source.read_files()
            except:
                self.logger.info("File read failed; updating sources", exc_info=1)
                source.update()
                source.read_files()
        self.pkgmap = dict()

    def find_deps(self, metadata, pkgname):
        for source in self.sources:
            if source.applies(metadata):
                deps = source.get_deps(metadata, pkgname)
                if deps:
                    return deps
        return ()

    def HandlesEntry(self, entry, metadata):
        if [x for x in metadata.groups if x in ['debian', 'ubuntu']] and \
           entry.tag == 'Package':
            return True
        return False

    def HandleEntry(self, entry, metadata):
        entry.set('version', 'auto')
        entry.set('type', 'deb')

    def validate_structures(self, meta, structures):
        if [g for g in meta.groups if g in ['debian', 'ubuntu']]:
            ptype = 'deb'
        else:
            return
        pkgnames = set()
        for struct in structures:
            for pkg in struct.findall('Package'):
                pkgnames.add(pkg.get('name'))
        all = copy.copy(pkgnames)
        work = copy.copy(pkgnames)
        new = set()
        while work:
            next = work.pop()
            for dep in self.find_deps(meta, next):
                if dep in all:
                    continue
                else:
                    new.add(dep)
                    all.add(dep)
                    work.add(dep)
        news = lxml.etree.Element('Independent')
        for pkg in new:
            lxml.etree.SubElement(news, 'BoundPackage', name=pkg,
                                  type=ptype, version='auto')
        structures.append(news)

if __name__ == '__main__':
    aa = Packages(None, '/home/desai/tmp/bcfg2')