xref: /openbmc/openbmc/poky/meta/lib/oe/packagedata.py (revision 15ae2509)
1#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
5import codecs
6import os
7
8def packaged(pkg, d):
9    return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK)
10
11def read_pkgdatafile(fn):
12    pkgdata = {}
13
14    def decode(str):
15        c = codecs.getdecoder("unicode_escape")
16        return c(str)[0]
17
18    if os.access(fn, os.R_OK):
19        import re
20        f = open(fn, 'r')
21        lines = f.readlines()
22        f.close()
23        r = re.compile("([^:]+):\s*(.*)")
24        for l in lines:
25            m = r.match(l)
26            if m:
27                pkgdata[m.group(1)] = decode(m.group(2))
28
29    return pkgdata
30
31def get_subpkgedata_fn(pkg, d):
32    return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg)
33
34def has_subpkgdata(pkg, d):
35    return os.access(get_subpkgedata_fn(pkg, d), os.R_OK)
36
37def read_subpkgdata(pkg, d):
38    return read_pkgdatafile(get_subpkgedata_fn(pkg, d))
39
40def has_pkgdata(pn, d):
41    fn = d.expand('${PKGDATA_DIR}/%s' % pn)
42    return os.access(fn, os.R_OK)
43
44def read_pkgdata(pn, d):
45    fn = d.expand('${PKGDATA_DIR}/%s' % pn)
46    return read_pkgdatafile(fn)
47
48#
49# Collapse FOO_pkg variables into FOO
50#
51def read_subpkgdata_dict(pkg, d):
52    ret = {}
53    subd = read_pkgdatafile(get_subpkgedata_fn(pkg, d))
54    for var in subd:
55        newvar = var.replace("_" + pkg, "")
56        if newvar == var and var + "_" + pkg in subd:
57            continue
58        ret[newvar] = subd[var]
59    return ret
60
61def _pkgmap(d):
62    """Return a dictionary mapping package to recipe name."""
63
64    pkgdatadir = d.getVar("PKGDATA_DIR")
65
66    pkgmap = {}
67    try:
68        files = os.listdir(pkgdatadir)
69    except OSError:
70        bb.warn("No files in %s?" % pkgdatadir)
71        files = []
72
73    for pn in [f for f in files if not os.path.isdir(os.path.join(pkgdatadir, f))]:
74        try:
75            pkgdata = read_pkgdatafile(os.path.join(pkgdatadir, pn))
76        except OSError:
77            continue
78
79        packages = pkgdata.get("PACKAGES") or ""
80        for pkg in packages.split():
81            pkgmap[pkg] = pn
82
83    return pkgmap
84
85def pkgmap(d):
86    """Return a dictionary mapping package to recipe name.
87    Cache the mapping in the metadata"""
88
89    pkgmap_data = d.getVar("__pkgmap_data", False)
90    if pkgmap_data is None:
91        pkgmap_data = _pkgmap(d)
92        d.setVar("__pkgmap_data", pkgmap_data)
93
94    return pkgmap_data
95
96def recipename(pkg, d):
97    """Return the recipe name for the given binary package name."""
98
99    return pkgmap(d).get(pkg)
100