blob: 32e5c82a948939e783c7a630f1baa5291ffd1ab3 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001import codecs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05002import os
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003
4def packaged(pkg, d):
5 return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK)
6
7def read_pkgdatafile(fn):
8 pkgdata = {}
9
10 def decode(str):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011 c = codecs.getdecoder("unicode_escape")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012 return c(str)[0]
13
14 if os.access(fn, os.R_OK):
15 import re
16 f = open(fn, 'r')
17 lines = f.readlines()
18 f.close()
19 r = re.compile("([^:]+):\s*(.*)")
20 for l in lines:
21 m = r.match(l)
22 if m:
23 pkgdata[m.group(1)] = decode(m.group(2))
24
25 return pkgdata
26
27def get_subpkgedata_fn(pkg, d):
28 return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg)
29
30def has_subpkgdata(pkg, d):
31 return os.access(get_subpkgedata_fn(pkg, d), os.R_OK)
32
33def read_subpkgdata(pkg, d):
34 return read_pkgdatafile(get_subpkgedata_fn(pkg, d))
35
36def has_pkgdata(pn, d):
37 fn = d.expand('${PKGDATA_DIR}/%s' % pn)
38 return os.access(fn, os.R_OK)
39
40def read_pkgdata(pn, d):
41 fn = d.expand('${PKGDATA_DIR}/%s' % pn)
42 return read_pkgdatafile(fn)
43
44#
45# Collapse FOO_pkg variables into FOO
46#
47def read_subpkgdata_dict(pkg, d):
48 ret = {}
49 subd = read_pkgdatafile(get_subpkgedata_fn(pkg, d))
50 for var in subd:
51 newvar = var.replace("_" + pkg, "")
52 if newvar == var and var + "_" + pkg in subd:
53 continue
54 ret[newvar] = subd[var]
55 return ret
56
57def _pkgmap(d):
58 """Return a dictionary mapping package to recipe name."""
59
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 pkgdatadir = d.getVar("PKGDATA_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061
62 pkgmap = {}
63 try:
64 files = os.listdir(pkgdatadir)
65 except OSError:
66 bb.warn("No files in %s?" % pkgdatadir)
67 files = []
68
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 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 -050070 try:
71 pkgdata = read_pkgdatafile(os.path.join(pkgdatadir, pn))
72 except OSError:
73 continue
74
75 packages = pkgdata.get("PACKAGES") or ""
76 for pkg in packages.split():
77 pkgmap[pkg] = pn
78
79 return pkgmap
80
81def pkgmap(d):
82 """Return a dictionary mapping package to recipe name.
83 Cache the mapping in the metadata"""
84
85 pkgmap_data = d.getVar("__pkgmap_data", False)
86 if pkgmap_data is None:
87 pkgmap_data = _pkgmap(d)
88 d.setVar("__pkgmap_data", pkgmap_data)
89
90 return pkgmap_data
91
92def recipename(pkg, d):
93 """Return the recipe name for the given binary package name."""
94
95 return pkgmap(d).get(pkg)