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.
5 # Note that it will not overwrite existing BBFILES or BBFILE_* variables, so
6 # you'll need to remove those from your config in order to use this.
8 # Specifying an archive in COLLECTIONS is also supported. Any archives of a
9 # supported format will be unpacked into COLLECTIONS_UNPACKDIR and used from
12 COLLECTIONS = "${@' '.join(d.getVar('BBPATH', 1).split(':'))}"
13 COLLECTIONS_UNPACKDIR = "${TMPDIR}/collections"
15 def collection_unpack(collection, name, d):
16 """ Unpack a collection archive and return the path to it. """
22 ("tar"): "tar x --no-same-owner -f %s",
23 ("tar.gz", "tgz", "tar.Z"): "tar xz --no-same-owner -f %s",
24 ("tar.bz2", "tbz", "tbz2"): "tar xj --no-same-owner -f %s",
25 ("zip", "jar"): "unzip -q -o %s",
28 outpath = os.path.join(d.getVar("COLLECTIONS_UNPACKDIR", 1), name)
31 collectiondata = open(collection, "r").read()
33 bb.fatal("Unable to open %s to calculate md5 sum" % collection)
36 md5obj.update(collectiondata)
37 md5sum = md5obj.hexdigest()
39 md5file = os.path.join(outpath, "md5")
40 if os.path.exists(md5file):
42 oldmd5sum = open(md5file).read()
46 if oldmd5sum == md5sum:
47 bb.debug(1, "Using existing %s for collection %s" % (outpath, name))
50 bb.note("Removing old unpacked collection at %s" % outpath)
51 os.system("rm -rf %s" % outpath)
54 cmd = (cmd for (exts, cmd) in handlers.iteritems()
56 if collection.endswith(e)).next()
57 cmd = "cd %s && PATH=\"%s\" %s" % (outpath, d.getVar("PATH", 1), cmd)
59 bb.fatal("Unable to find unpack handler for %s" % collection)
61 if not os.path.isdir(outpath):
64 bb.note("Unpacking %s to %s/" % (collection, outpath))
65 ret = os.system(cmd % collection)
67 bb.fatal("Unable to unpack %s" % collection)
69 md5out = open(md5file, "w")
74 def collections_setup(d):
75 """ Populate collection and bbfiles metadata from the COLLECTIONS var. """
78 from itertools import izip, chain
82 if d.getVar(k, 0) is None:
85 collections = d.getVar("COLLECTIONS", 1)
88 globbed = (glob(path) for path in collections.split())
89 collections = list(chain(*globbed))
93 for collection in collections:
94 if collection.endswith(os.sep):
95 collection = collection[:-1]
96 basename = os.path.basename(collection).split(os.path.extsep)[0]
97 if namemap.get(basename):
98 basename = "%s-%s" % (basename, hash(collection))
99 namemap[basename] = collection
100 collectionmap[collection] = basename
102 for (collection, priority) in izip(collectionmap, xrange(len(collections), 0, -1)):
103 if not os.path.exists(collection):
104 bb.fatal("Collection %s does not exist" % collection)
106 name = collectionmap[collection]
108 bb.fatal("Unable to determine collection name for %s" % collection)
110 if not os.path.isdir(collection):
111 del collectionmap[collection]
112 unpacked = collection_unpack(collection, name, d)
114 collection = unpacked
115 collectionmap[collection] = name
117 bb.fatal("Unable to unpack collection %s" % collection)
119 setifunset("BBFILE_PATTERN_%s" % name, "^%s/" % collection)
120 setifunset("BBFILE_PRIORITY_%s" % name, str(priority))
122 setifunset("BBFILE_COLLECTIONS", " ".join(collectionmap.values()))
123 setifunset("BBFILES", " ".join(collectionmap.keys()))
125 addhandler collections_eh
126 python collections_eh () {
127 from bb.event import getName
129 if getName(e) == "ConfigParsed":
130 collections_setup(e.data)