summaryrefslogtreecommitdiff
path: root/conf/collections.inc
blob: 831ca935e61760613a38d1b164cee7050cd1f225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (c) 2009 MontaVista Software, Inc.  All rights reserved.
#
# Released under the MIT license (see COPYING.MIT for the terms)
#
# Take a list of directories in COLLECTIONS, in priority order (highest to
# lowest), and use those to populate BBFILES, BBFILE_COLLECTIONS,
# BBFILE_PATTERN_*, and BBFILE_PRIORITY_*.
#
# Specifying an archive in COLLECTIONS is also supported.  Any archives of a
# supported format will be unpacked into COLLECTIONS_UNPACKDIR and used from
# there.

COLLECTIONS = "${@' '.join(d.getVar('BBPATH', 1).split(':'))}"
COLLECTIONS_UNPACKDIR = "${TMPDIR}/collections"

COLLECTIONINFO = "${@get_collection(d.getVar('FILE', 1), d)}"

def has_collection(name, d):
    for (uniquename, info) in d.getVar("COLLECTIONSINFO", 1).iteritems():
        if info["name"] == name:
            return True
    return False

def get_collection(file, d):
    if not os.path.isabs(file):
        file = bb.which(d.getVar("BBPATH", 1), file)
    filedir = os.path.realpath(os.path.dirname(file))
    for (uniquename, info) in d.getVar("COLLECTIONSINFO", 1).iteritems():
        path = os.path.realpath(info["path"])
        if filedir.startswith(path + os.path.sep):
            return info

def collection_unpack(collection, d):
    """ Unpack a collection archive and return the path to it. """
    import bb
    import os
    from md5 import md5

    handlers = {
        ("tar"): "tar x --no-same-owner -f %s",
        ("tar.gz", "tgz", "tar.Z"): "tar xz --no-same-owner -f %s",
        ("tar.bz2", "tbz", "tbz2"): "tar xj --no-same-owner -f %s",
        ("zip", "jar"): "unzip -q -o %s",
    }

    basename = os.path.basename(collection)
    try:
        cmd, name = ((cmd, basename[:-len(e)-1]) for (exts, cmd) in handlers.iteritems()
                     for e in exts
                     if basename.endswith(e)).next()
    except StopIteration:
        bb.fatal("No method available to unpack %s (unsupported file type?)" % collection)
    else:
        outpath = os.path.join(d.getVar("COLLECTIONS_UNPACKDIR", 1), name)
        cmd = "cd %s && PATH=\"%s\" %s" % (outpath, d.getVar("PATH", 1), cmd)

    try:
        collectiondata = open(collection, "r").read()
    except IOError:
        bb.fatal("Unable to open %s to calculate md5 sum" % collection)

    md5obj = md5()
    md5obj.update(collectiondata)
    md5sum = md5obj.hexdigest()

    md5file = os.path.join(outpath, "md5")
    if os.path.exists(md5file):
        try:
            oldmd5sum = open(md5file).read()
        except IOError:
            pass
        else:
            if oldmd5sum == md5sum:
                bb.debug(1, "Using existing %s for collection '%s'" % (outpath, name))
                return outpath, False, name

        bb.note("Removing old unpacked collection at %s" % outpath)
        os.system("rm -rf %s" % outpath)

    if not os.path.isdir(outpath):
        os.makedirs(outpath)

    bb.note("Unpacking %s to %s/" % (collection, outpath))
    ret = os.system(cmd % collection)
    if ret != 0:
        bb.fatal("Unable to unpack %s" % collection)

    md5out = open(md5file, "w")
    md5out.write(md5sum)
    md5out.close()
    return outpath, True, name

def collections_setup(d):
    """ Populate collection and bbfiles metadata from the COLLECTIONS var. """
    import bb
    import os
    from itertools import izip, chain
    from glob import glob

    def setifunset(k, v):
        if d.getVar(k, 0) is None:
            d.setVar(k, v)

    collections = d.getVar("COLLECTIONS", 1)
    if not collections:
        return

    bb.debug(1, "Processing COLLECTIONS (%s)" % collections)

    globbed = []
    for path in collections.split():
        paths = glob(os.path.normpath(path))
        if not paths:
            bb.msg.warn(None, "No matches in filesystem for %s in COLLECTIONS" % path)
        globbed += paths
    collections = globbed

    collectionmap = {}
    namemap = {}
    collectioninfo = {}
    unpackedthisexec = False
    oldbbpath = d.getVar("BBPATH", 1)
    bbpath = (oldbbpath or "").split(":")
    for (collection, priority) in izip(collections, xrange(len(collections), 0, -1)):
        if not os.path.exists(collection):
            bb.fatal("Collection %s does not exist" % collection)

        origpath = collection
        if not os.path.isdir(collection):
            unpacked, unpackedthisexec, name = collection_unpack(collection, d)
            if unpacked:
                collection = unpacked
                for dir in glob("%s/*/" % collection):
                    if not dir in bbpath:
                        bbpath.append(dir)
            else:
                bb.fatal("Unable to unpack collection %s" % collection)
        else:
            name = os.path.basename(collection)
            if not collection in bbpath:
                bbpath.append(collection)

        if namemap.get(name):
            name = "%s-%s" % (name, hash(collection))
        namemap[name] = collection
        collectionmap[collection] = name

        collectioninfo[name] = {
            "name": name,
            "originalpath": origpath,
            "path": collection,
            "priority": priority,
        }

        setifunset("BBFILE_PATTERN_%s" % name, "^%s/" % collection)
        setifunset("BBFILE_PRIORITY_%s" % name, str(priority))

    d.setVar("COLLECTIONSINFO", collectioninfo)

    setifunset("BBFILE_COLLECTIONS", " ".join(collectionmap.values()))
    setifunset("BBFILES", " ".join(collectionmap.keys()))

    bbpath = [os.path.realpath(dir) for dir in bbpath if os.path.exists(dir)]
    d.setVar("BBPATH", ":".join(bbpath))
    if unpackedthisexec or (set(bbpath) != set(oldbbpath.split(":"))):
        import sys
        bb.debug(1, "Re-executing bitbake with BBPATH of %s" % d.getVar("BBPATH", 0))
        os.environ["BBPATH"] = d.getVar("BBPATH", 0)
        os.environ["PYTHONPATH"] = ":".join(sys.path)
        sys.argv.insert(0, sys.executable)
        os.execvpe(sys.executable, sys.argv, os.environ)

addhandler collections_eh
python collections_eh () {
    from bb.event import getName

    if getName(e) == "ConfigParsed":
        collections_setup(e.data)

    return NotHandled
}