Merge branch 'opendreambox' of git://git.ritzmo.de/openembedded into opendreambox
[vuplus_openembedded] / conf / collections.inc
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_*.
4 #
5 # Specifying an archive in COLLECTIONS is also supported.  Any archives of a
6 # supported format will be unpacked into COLLECTIONS_UNPACKDIR and used from
7 # there.
8
9 COLLECTIONS = "${@' '.join(d.getVar('BBPATH', 1).split(':'))}"
10 COLLECTIONS_UNPACKDIR = "${TMPDIR}/collections"
11
12 def collection_unpack(collection, name, d):
13     """ Unpack a collection archive and return the path to it. """
14     import bb
15     import os
16     from md5 import md5
17
18     handlers = {
19         ("tar"): "tar x --no-same-owner -f %s",
20         ("tar.gz", "tgz", "tar.Z"): "tar xz --no-same-owner -f %s",
21         ("tar.bz2", "tbz", "tbz2"): "tar xj --no-same-owner -f %s",
22         ("zip", "jar"): "unzip -q -o %s",
23     }
24
25     outpath = os.path.join(d.getVar("COLLECTIONS_UNPACKDIR", 1), name)
26
27     try:
28         collectiondata = open(collection, "r").read()
29     except IOError:
30         bb.fatal("Unable to open %s to calculate md5 sum" % collection)
31
32     md5obj = md5()
33     md5obj.update(collectiondata)
34     md5sum = md5obj.hexdigest()
35
36     md5file = os.path.join(outpath, "md5")
37     if os.path.exists(md5file):
38         try:
39             oldmd5sum = open(md5file).read()
40         except IOError:
41             pass
42         else:
43             if oldmd5sum == md5sum:
44                 bb.note("Using existing %s for collection '%s'" % (outpath, name))
45                 return outpath, False
46
47         bb.note("Removing old unpacked collection at %s" % outpath)
48         os.system("rm -rf %s" % outpath)
49
50     try:
51         cmd = (cmd for (exts, cmd) in handlers.iteritems()
52                    for e in exts
53                    if collection.endswith(e)).next()
54         cmd = "cd %s && PATH=\"%s\" %s" % (outpath, d.getVar("PATH", 1), cmd)
55     except StopIteration:
56         bb.fatal("Unable to find unpack handler for %s" % collection)
57
58     if not os.path.isdir(outpath):
59         os.makedirs(outpath)
60
61     bb.note("Unpacking %s to %s/" % (collection, outpath))
62     ret = os.system(cmd % collection)
63     if ret != 0:
64         bb.fatal("Unable to unpack %s" % collection)
65
66     md5out = open(md5file, "w")
67     md5out.write(md5sum)
68     md5out.close()
69     return outpath, True
70
71 def collections_setup(d):
72     """ Populate collection and bbfiles metadata from the COLLECTIONS var. """
73     import bb
74     import os
75     from itertools import izip, chain
76     from glob import glob
77
78     def setifunset(k, v):
79         if d.getVar(k, 0) is None:
80             d.setVar(k, v)
81
82     collections = d.getVar("COLLECTIONS", 1)
83     if not collections:
84         return
85
86     bb.debug(1, "Processing COLLECTIONS (%s)" % collections)
87
88     globbed = []
89     for path in collections.split():
90         paths = glob(os.path.normpath(path))
91         if not paths:
92             bb.msg.warn(None, "No matches in filesystem for %s in COLLECTIONS" % path)
93         globbed += paths
94     collections = globbed
95
96     collectionmap = {}
97     namemap = {}
98     for collection in collections:
99         basename = os.path.basename(collection).split(os.path.extsep)[0]
100         if namemap.get(basename):
101             basename = "%s-%s" % (basename, hash(collection))
102         namemap[basename] = collection
103         collectionmap[collection] = basename
104
105     unpackedthisexec = False
106     oldbbpath = d.getVar("BBPATH", 1)
107     bbpath = (oldbbpath or "").split(":")
108     for (collection, priority) in izip(collections, xrange(len(collections), 0, -1)):
109         if not os.path.exists(collection):
110             bb.fatal("Collection %s does not exist" % collection)
111
112         name = collectionmap[collection]
113         if not name:
114             bb.fatal("Unable to determine collection name for %s" % collection)
115
116         if not os.path.isdir(collection):
117             del collectionmap[collection]
118             unpacked, unpackedthisexec = collection_unpack(collection, name, d)
119             if unpacked:
120                 collection = unpacked
121                 collectionmap[collection] = name
122                 for dir in glob("%s/*/" % collection):
123                     if not dir in bbpath:
124                         bbpath.append(dir)
125             else:
126                 bb.fatal("Unable to unpack collection %s" % collection)
127         else:
128             if not collection in bbpath:
129                 bbpath.append(collection)
130
131         setifunset("BBFILE_PATTERN_%s" % name, "^%s/" % collection)
132         setifunset("BBFILE_PRIORITY_%s" % name, str(priority))
133
134     setifunset("BBFILE_COLLECTIONS", " ".join(collectionmap.values()))
135     setifunset("BBFILES", " ".join(collectionmap.keys()))
136
137     # Strip out the fallback bitbake.conf from BB_RUN_LOCATION
138     bbpath = [os.path.realpath(dir) for dir in bbpath if os.path.exists(dir)]
139     try:
140         bbpath.remove(os.path.realpath(bb.data.expand("${BB_RUN_LOCATION}/../share/bitbake", d)))
141     except (OSError, ValueError):
142         pass
143
144     from sets import Set
145     d.setVar("BBPATH", ":".join(bbpath))
146     if unpackedthisexec or Set(bbpath).symmetric_difference(Set(oldbbpath.split(":"))):
147         bb.debug(1, "Re-executing bitbake with BBPATH of %s" % d.getVar("BBPATH", 0))
148         import sys
149         os.environ["BBPATH"] = d.getVar("BBPATH", 0)
150         os.execvpe("bitbake", sys.argv, os.environ)
151
152 addhandler collections_eh
153 python collections_eh () {
154     from bb.event import getName
155
156     if getName(e) == "ConfigParsed":
157         collections_setup(e.data)
158
159     return NotHandled
160 }