Merge branch 'org.openembedded.dev' of git@git.openembedded.net:openembedded into...
[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_*.  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.
7 #
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
10 # there.
11
12 COLLECTIONS = "${@' '.join(d.getVar('BBPATH', 1).split(':'))}"
13 COLLECTIONS_UNPACKDIR = "${TMPDIR}/collections"
14
15 def collection_unpack(collection, name, d):
16     """ Unpack a collection archive and return the path to it. """
17     import bb
18     import os
19     from md5 import md5
20
21     handlers = {
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",
26     }
27
28     outpath = os.path.join(d.getVar("COLLECTIONS_UNPACKDIR", 1), name)
29
30     try:
31         collectiondata = open(collection, "r").read()
32     except IOError:
33         bb.fatal("Unable to open %s to calculate md5 sum" % collection)
34
35     md5obj = md5()
36     md5obj.update(collectiondata)
37     md5sum = md5obj.hexdigest()
38
39     md5file = os.path.join(outpath, "md5")
40     if os.path.exists(md5file):
41         try:
42             oldmd5sum = open(md5file).read()
43         except IOError:
44             pass
45         else:
46             if oldmd5sum == md5sum:
47                 bb.debug(1, "Using existing %s for collection %s" % (outpath, name))
48                 return outpath
49
50         bb.note("Removing old unpacked collection at %s" % outpath)
51         os.system("rm -rf %s" % outpath)
52
53     try:
54         cmd = (cmd for (exts, cmd) in handlers.iteritems()
55                    for e in exts
56                    if collection.endswith(e)).next()
57         cmd = "cd %s && PATH=\"%s\" %s" % (outpath, d.getVar("PATH", 1), cmd)
58     except StopIteration:
59         bb.fatal("Unable to find unpack handler for %s" % collection)
60
61     if not os.path.isdir(outpath):
62         os.makedirs(outpath)
63
64     bb.note("Unpacking %s to %s/" % (collection, outpath))
65     ret = os.system(cmd % collection)
66     if ret != 0:
67         bb.fatal("Unable to unpack %s" % collection)
68
69     md5out = open(md5file, "w")
70     md5out.write(md5sum)
71     md5out.close()
72     return outpath
73
74 def collections_setup(d):
75     """ Populate collection and bbfiles metadata from the COLLECTIONS var. """
76     import bb
77     import os
78     from itertools import izip, chain
79     from glob import glob
80
81     def setifunset(k, v):
82         if d.getVar(k, 0) is None:
83             d.setVar(k, v)
84
85     collections = d.getVar("COLLECTIONS", 1)
86     if not collections:
87         return
88     globbed = (glob(path) for path in collections.split())
89     collections = list(chain(*globbed))
90
91     collectionmap = {}
92     namemap = {}
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
101
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)
105
106         name = collectionmap[collection]
107         if not name:
108             bb.fatal("Unable to determine collection name for %s" % collection)
109
110         if not os.path.isdir(collection):
111             del collectionmap[collection]
112             unpacked = collection_unpack(collection, name, d)
113             if unpacked:
114                 collection = unpacked
115                 collectionmap[collection] = name
116             else:
117                 bb.fatal("Unable to unpack collection %s" % collection)
118
119         setifunset("BBFILE_PATTERN_%s" % name, "^%s/" % collection)
120         setifunset("BBFILE_PRIORITY_%s" % name, str(priority))
121
122     setifunset("BBFILE_COLLECTIONS", " ".join(collectionmap.values()))
123     setifunset("BBFILES", " ".join(collectionmap.keys()))
124
125 addhandler collections_eh
126 python collections_eh () {
127     from bb.event import getName
128
129     if getName(e) == "ConfigParsed":
130         collections_setup(e.data)
131
132     return NotHandled
133 }