blob: cbde380b034f8b4caf0d2d6ca630c5ae08bda30d [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005import codecs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05006import os
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
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):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015 c = codecs.getdecoder("unicode_escape")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064 pkgdatadir = d.getVar("PKGDATA_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
66 pkgmap = {}
67 try:
68 files = os.listdir(pkgdatadir)
69 except OSError:
70 bb.warn("No files in %s?" % pkgdatadir)
71 files = []
72
Patrick Williamsc0f7c042017-02-23 20:41:17 -060073 for pn in [f for f in files if not os.path.isdir(os.path.join(pkgdatadir, f))]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 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)