1 # Copyright (c) 2009 MontaVista Software, Inc. All rights reserved.
3 # Released under the MIT license (see COPYING.MIT for the terms)
5 # Take a list of directories in COLLECTIONS, in priority order (highest to
6 # lowest), and use those to populate BBFILES, BBFILE_COLLECTIONS,
7 # BBFILE_PATTERN_*, and BBFILE_PRIORITY_*.
9 # Specifying an archive in COLLECTIONS is also supported. Any archives of a
10 # supported format will be unpacked into COLLECTIONS_UNPACKDIR and used from
13 COLLECTIONS ?= "${@' '.join(d.getVar('BBPATH', 1).split(':'))}"
14 COLLECTIONS_UNPACKDIR = "${TMPDIR}/collections"
16 COLLECTIONINFO = "${@get_collection(d.getVar('FILE', 1), d)}"
18 def has_collection(name, d):
19 for (uniquename, info) in d.getVar("COLLECTIONSINFO", 1).iteritems():
20 if info["name"] == name:
24 def get_collection(file, d):
25 if not os.path.isabs(file):
26 file = bb.which(d.getVar("BBPATH", 1), file)
27 filedir = os.path.realpath(os.path.dirname(file))
28 for (uniquename, info) in d.getVar("COLLECTIONSINFO", 1).iteritems():
29 path = os.path.realpath(info["path"])
30 if filedir.startswith(path + os.path.sep):
33 def collection_unpack(collection, d):
34 """ Unpack a collection archive and return the path to it. """
40 ("tar"): "tar x --no-same-owner -f %s",
41 ("tar.gz", "tgz", "tar.Z"): "tar xz --no-same-owner -f %s",
42 ("tar.bz2", "tbz", "tbz2"): "tar xj --no-same-owner -f %s",
43 ("zip", "jar"): "unzip -q -o %s",
46 basename = os.path.basename(collection)
48 cmd, name = ((cmd, basename[:-len(e)-1]) for (exts, cmd) in handlers.iteritems()
50 if basename.endswith(e)).next()
52 bb.fatal("No method available to unpack %s (unsupported file type?)" % collection)
54 outpath = os.path.join(d.getVar("COLLECTIONS_UNPACKDIR", 1), name)
55 cmd = "cd %s && PATH=\"%s\" %s" % (outpath, d.getVar("PATH", 1), cmd)
58 collectiondata = open(collection, "r").read()
60 bb.fatal("Unable to open %s to calculate md5 sum" % collection)
63 md5obj.update(collectiondata)
64 md5sum = md5obj.hexdigest()
66 md5file = os.path.join(outpath, "md5")
67 if os.path.exists(md5file):
69 oldmd5sum = open(md5file).read()
73 if oldmd5sum == md5sum:
74 bb.debug(1, "Using existing %s for collection '%s'" % (outpath, name))
75 return outpath, False, name
77 bb.note("Removing old unpacked collection at %s" % outpath)
78 os.system("rm -rf %s" % outpath)
80 if not os.path.isdir(outpath):
83 bb.note("Unpacking %s to %s/" % (collection, outpath))
84 ret = os.system(cmd % collection)
86 bb.fatal("Unable to unpack %s" % collection)
88 md5out = open(md5file, "w")
91 return outpath, True, name
93 def collections_setup(d):
94 """ Populate collection and bbfiles metadata from the COLLECTIONS var. """
97 from itertools import izip, chain
100 def setifunset(k, v):
101 if d.getVar(k, 0) is None:
104 collections = d.getVar("COLLECTIONS", 1)
108 bb.debug(1, "Processing COLLECTIONS (%s)" % collections)
111 for path in collections.split():
112 paths = glob(os.path.normpath(path))
114 bb.msg.warn(None, "No matches in filesystem for %s in COLLECTIONS" % path)
116 collections = globbed
121 unpackedthisexec = False
122 oldbbpath = d.getVar("BBPATH", 1)
123 bbpath = (oldbbpath or "").split(":")
124 for (collection, priority) in izip(collections, xrange(len(collections), 0, -1)):
125 if not os.path.exists(collection):
126 bb.fatal("Collection %s does not exist" % collection)
128 origpath = collection
129 if not os.path.isdir(collection):
130 unpacked, unpackedthisexec, name = collection_unpack(collection, d)
132 collection = unpacked
133 for dir in glob("%s/*/" % collection):
134 if not dir in bbpath:
137 bb.fatal("Unable to unpack collection %s" % collection)
139 name = os.path.basename(collection)
140 if not collection in bbpath:
141 bbpath.append(collection)
143 if namemap.get(name):
144 name = "%s-%s" % (name, hash(collection))
145 namemap[name] = collection
146 collectionmap[collection] = name
148 collectioninfo[name] = {
150 "originalpath": origpath,
152 "priority": priority,
155 setifunset("BBFILE_PATTERN_%s" % name, "^%s/" % collection)
156 setifunset("BBFILE_PRIORITY_%s" % name, str(priority))
158 d.setVar("COLLECTIONSINFO", collectioninfo)
160 setifunset("BBFILE_COLLECTIONS", " ".join(collectionmap.values()))
161 setifunset("BBFILES", " ".join(collectionmap.keys()))
163 bbpath = [os.path.realpath(dir) for dir in bbpath if os.path.exists(dir)]
164 d.setVar("BBPATH", ":".join(bbpath))
165 if unpackedthisexec or (set(bbpath) != set(oldbbpath.split(":"))):
167 bb.debug(1, "Re-executing bitbake with BBPATH of %s" % d.getVar("BBPATH", 0))
168 os.environ["BBPATH"] = d.getVar("BBPATH", 0)
169 # FIXME: This exports a bogus PYTHONPATH to OE recipes resulting in massive breakages:
170 # http://thread.gmane.org/gmane.comp.handhelds.openembedded/30163
171 # os.environ["PYTHONPATH"] = ":".join(sys.path)
172 sys.argv.insert(0, sys.executable)
173 os.execvpe(sys.executable, sys.argv, os.environ)
175 addhandler collections_eh
176 python collections_eh () {
177 from bb.event import getName
179 if getName(e) == "ConfigParsed":
180 collections_setup(e.data)