|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# |
| 5 | +# Copyright (C) 2008, TUBITAK/UEKAE |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify it under |
| 8 | +# the terms of the GNU General Public License as published by the Free |
| 9 | +# Software Foundation; either version 2 of the License, or (at your option) |
| 10 | +# any later version. |
| 11 | +# |
| 12 | +# Please read the COPYING file. |
| 13 | +# |
| 14 | + |
| 15 | +import sys |
| 16 | +import multiprocessing |
| 17 | + |
| 18 | +import urllib2 |
| 19 | +import bz2 |
| 20 | +import lzma |
| 21 | + |
| 22 | +import piksemel |
| 23 | + |
| 24 | +import pisi |
| 25 | +import pisi.dependency as dependency |
| 26 | +from pisi.graph import CycleException |
| 27 | + |
| 28 | + |
| 29 | +class SourceDB: |
| 30 | + def __init__(self, index): |
| 31 | + |
| 32 | + self.__source_nodes = {} |
| 33 | + self.__pkgstosrc = {} |
| 34 | + |
| 35 | + doc = piksemel.parseString(index) |
| 36 | + self.__source_nodes, self.__pkgstosrc = self.__generate_sources(doc) |
| 37 | + |
| 38 | + def __generate_sources(self, doc): |
| 39 | + sources = {} |
| 40 | + pkgstosrc = {} |
| 41 | + |
| 42 | + for spec in doc.tags("SpecFile"): |
| 43 | + src_name = spec.getTag("Source").getTagData("Name") |
| 44 | + sources[src_name] = spec.toString() |
| 45 | + for package in spec.tags("Package"): |
| 46 | + pkgstosrc[package.getTagData("Name")] = src_name |
| 47 | + |
| 48 | + return sources, pkgstosrc |
| 49 | + |
| 50 | + def has_spec(self, name): |
| 51 | + return self.__source_nodes.has_key(name) |
| 52 | + |
| 53 | + def get_spec(self, name): |
| 54 | + src = self.__source_nodes[name] |
| 55 | + spec = pisi.specfile.SpecFile() |
| 56 | + spec.parse(src) |
| 57 | + return spec |
| 58 | + |
| 59 | + def list_specs(self): |
| 60 | + return self.__source_nodes.keys() |
| 61 | + |
| 62 | + def pkgtosrc(self, name): |
| 63 | + return self.__pkgstosrc[name] |
| 64 | + |
| 65 | +def find_circle(sourcedb, A): |
| 66 | + |
| 67 | + G_f = pisi.graph.Digraph() |
| 68 | + |
| 69 | + def get_spec(name): |
| 70 | + if sourcedb.has_spec(name): |
| 71 | + return sourcedb.get_spec(name) |
| 72 | + else: |
| 73 | + raise Exception('Cannot find source package: %s' % name) |
| 74 | + |
| 75 | + def get_src(name): |
| 76 | + return get_spec(name).source |
| 77 | + |
| 78 | + def add_src(src): |
| 79 | + if not str(src.name) in G_f.vertices(): |
| 80 | + G_f.add_vertex(str(src.name), (src.version, src.release)) |
| 81 | + |
| 82 | + def pkgtosrc(pkg): |
| 83 | + try: |
| 84 | + tmp = sourcedb.pkgtosrc(pkg) |
| 85 | + except KeyError, e: |
| 86 | + # this is a bad hack but after we hit a problem we need to continue |
| 87 | + tmp = "e3" |
| 88 | + print "---> borks in ", e |
| 89 | + |
| 90 | + return tmp |
| 91 | + |
| 92 | + B = A |
| 93 | + |
| 94 | + install_list = set() |
| 95 | + |
| 96 | + while len(B) > 0: |
| 97 | + Bp = set() |
| 98 | + for x in B: |
| 99 | + sf = get_spec(x) |
| 100 | + src = sf.source |
| 101 | + add_src(src) |
| 102 | + |
| 103 | + # add dependencies |
| 104 | + |
| 105 | + def process_dep(dep): |
| 106 | + srcdep = pkgtosrc(dep.package) |
| 107 | + if not srcdep in G_f.vertices(): |
| 108 | + Bp.add(srcdep) |
| 109 | + add_src(get_src(srcdep)) |
| 110 | + if not src.name == srcdep: # firefox - firefox-devel thing |
| 111 | + G_f.add_edge(src.name, srcdep) |
| 112 | + |
| 113 | + for builddep in src.buildDependencies: |
| 114 | + process_dep(builddep) |
| 115 | + |
| 116 | + for pkg in sf.packages: |
| 117 | + for rtdep in pkg.packageDependencies: |
| 118 | + process_dep(rtdep) |
| 119 | + B = Bp |
| 120 | + |
| 121 | + try: |
| 122 | + order_build = G_f.topological_sort() |
| 123 | + order_build.reverse() |
| 124 | + except CycleException, cycle: |
| 125 | + return str(cycle) |
| 126 | + |
| 127 | + return "" |
| 128 | + |
| 129 | +def getIndex(uri): |
| 130 | + try: |
| 131 | + if "://" in uri: |
| 132 | + rawdata = urllib2.urlopen(uri).read() |
| 133 | + else: |
| 134 | + rawdata = open(uri, "r").read() |
| 135 | + except IOError: |
| 136 | + print "could not fetch %s" % uri |
| 137 | + return None |
| 138 | + |
| 139 | + if uri.endswith("bz2"): |
| 140 | + data = bz2.decompress(rawdata) |
| 141 | + elif uri.endswith("xz") or uri.endswith("lzma"): |
| 142 | + data = lzma.decompress(rawdata) |
| 143 | + else: |
| 144 | + data = rawdata |
| 145 | + |
| 146 | + return data |
| 147 | + |
| 148 | +def processPackage(pkg, sourcesLength, counter): |
| 149 | + global sourcedb |
| 150 | + |
| 151 | + sys.stdout.write("\r(%04d/%d) Calculating build dep of %s " % (counter, sourcesLength, pkg)) |
| 152 | + sys.stdout.flush() |
| 153 | + |
| 154 | + return find_circle(sourcedb, [pkg]) |
| 155 | + |
| 156 | +def updateStatus(circleResult): |
| 157 | + global cycles |
| 158 | + cycles.add(circleResult) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + |
| 163 | + if len(sys.argv) < 2: |
| 164 | + print "Usage: circlefinder.py <source repo pisi-index.xml file>" |
| 165 | + sys.exit(1) |
| 166 | + |
| 167 | + rawIndex = getIndex(sys.argv[1]) |
| 168 | + sourcedb = SourceDB(rawIndex) |
| 169 | + sources = sourcedb.list_specs() |
| 170 | + |
| 171 | + sourcesLength = len(sources) |
| 172 | + counter = 0 |
| 173 | + |
| 174 | + global cycles |
| 175 | + cycles = set() |
| 176 | + |
| 177 | + pool = multiprocessing.Pool() |
| 178 | + |
| 179 | + for pkg in sources: |
| 180 | + counter += 1 |
| 181 | + pool.apply_async(processPackage, (pkg, sourcesLength, counter), callback=updateStatus) |
| 182 | + |
| 183 | + pool.close() |
| 184 | + pool.join() |
| 185 | + |
| 186 | + if len(cycles): |
| 187 | + print |
| 188 | + for cycle in cycles: |
| 189 | + print cycle |
| 190 | + else: |
| 191 | + print "No circular dep found" |
| 192 | + |
| 193 | + |
0 commit comments