1 # Take a list of directories in COLLECTIONS, in priority order (highest to
2 # lowest), and use those to populate BBFILES, BBFILE_COLLECTIONS,
3 # BBFILE_PATTERN_*, and BBFILE_PRIORITY_*. By default, COLLECTIONS is
4 # prepopulated with the locations the user specified in their BBPATH.
6 # Specifying an archive in COLLECTIONS is also supported. Any archives of a
7 # supported format will be unpacked into COLLECTIONS_UNPACKDIR and used from
10 COLLECTIONS = "${@' '.join(d.getVar('BBPATH', 1).split(':'))}"
11 COLLECTIONS_UNPACKDIR = "${TMPDIR}/collections"
13 def collection_unpack(collection, name, d):
14 """ Unpack a collection archive and return the path to it. """
20 ("tar"): "tar x --no-same-owner -f %s",
21 ("tar.gz", "tgz", "tar.Z"): "tar xz --no-same-owner -f %s",
22 ("tar.bz2", "tbz", "tbz2"): "tar xj --no-same-owner -f %s",
23 ("zip", "jar"): "unzip -q -o %s",
26 outpath = os.path.join(d.getVar("COLLECTIONS_UNPACKDIR", 1), name)
29 collectiondata = open(collection, "r").read()
31 bb.fatal("Unable to open %s to calculate md5 sum" % collection)
34 md5obj.update(collectiondata)
35 md5sum = md5obj.hexdigest()
37 md5file = os.path.join(outpath, "md5")
38 if os.path.exists(md5file):
40 oldmd5sum = open(md5file).read()
44 if oldmd5sum == md5sum:
45 bb.debug(1, "Using existing %s for collection %s" % (outpath, name))
48 bb.note("Removing old unpacked collection at %s" % outpath)
49 os.system("rm -rf %s" % outpath)
52 cmd = (cmd for (exts, cmd) in handlers.iteritems()
54 if collection.endswith(e)).next()
55 cmd = "cd %s && PATH=\"%s\" %s" % (outpath, d.getVar("PATH", 1), cmd)
57 bb.fatal("Unable to find unpack handler for %s" % collection)
59 if not os.path.isdir(outpath):
62 bb.note("Unpacking %s to %s/" % (collection, outpath))
63 ret = os.system(cmd % collection)
65 bb.fatal("Unable to unpack %s" % collection)
67 md5out = open(md5file, "w")
72 def collections_setup(d):
73 """ Populate collection and bbfiles metadata from the COLLECTIONS var. """
76 from itertools import izip, chain
80 if d.getVar(k, 0) is None:
83 collections = d.getVar("COLLECTIONS", 1)
86 globbed = (glob(path) for path in collections.split())
87 collections = list(chain(*globbed))
91 for collection in collections:
92 basename = os.path.basename(collection).split(os.path.extsep)[0]
93 if namemap.get(basename):
94 basename = "%s-%s" % (basename, hash(collection))
95 namemap[basename] = collection
96 collectionmap[collection] = basename
98 for (collection, priority) in izip(collections, xrange(len(collections), 0, -1)):
99 if not os.path.exists(collection):
100 bb.fatal("Collection %s does not exist" % collection)
102 name = collectionmap[collection]
104 bb.fatal("Unable to determine collection name for %s" % collection)
106 if not os.path.isdir(collection):
107 del collectionmap[collection]
108 unpacked = collection_unpack(collection, name, d)
110 collection = unpacked
111 collectionmap[collection] = name
113 bb.fatal("Unable to unpack collection %s" % collection)
115 setifunset("BBFILE_PATTERN_%s" % name, "^%s/" % collection)
116 setifunset("BBFILE_PRIORITY_%s" % name, str(priority))
118 setifunset("BBFILE_COLLECTIONS", " ".join(collectionmap.values()))
119 setifunset("BBFILES", " ".join(collectionmap.keys()))
121 addhandler collections_eh
122 python collections_eh () {
123 from bb.event import getName
125 if getName(e) == "ConfigParsed":
126 collections_setup(e.data)