blob: 0b17897e4024951318f4630615875a86dc9f0dea [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
Brad Bishop64c979e2019-11-04 13:55:29 -050020 with open(fn, 'r') as f:
21 lines = f.readlines()
Patrick Williams213cb262021-08-07 19:21:33 -050022 r = re.compile("(^.+?):\s+(.*)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 for l in lines:
24 m = r.match(l)
25 if m:
26 pkgdata[m.group(1)] = decode(m.group(2))
27
28 return pkgdata
29
30def get_subpkgedata_fn(pkg, d):
31 return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg)
32
33def has_subpkgdata(pkg, d):
34 return os.access(get_subpkgedata_fn(pkg, d), os.R_OK)
35
36def read_subpkgdata(pkg, d):
37 return read_pkgdatafile(get_subpkgedata_fn(pkg, d))
38
39def has_pkgdata(pn, d):
40 fn = d.expand('${PKGDATA_DIR}/%s' % pn)
41 return os.access(fn, os.R_OK)
42
43def read_pkgdata(pn, d):
44 fn = d.expand('${PKGDATA_DIR}/%s' % pn)
45 return read_pkgdatafile(fn)
46
47#
Patrick Williams0ca19cc2021-08-16 14:03:13 -050048# Collapse FOO:pkg variables into FOO
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049#
50def read_subpkgdata_dict(pkg, d):
51 ret = {}
52 subd = read_pkgdatafile(get_subpkgedata_fn(pkg, d))
53 for var in subd:
Patrick Williams0ca19cc2021-08-16 14:03:13 -050054 newvar = var.replace(":" + pkg, "")
55 if newvar == var and var + ":" + pkg in subd:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 continue
57 ret[newvar] = subd[var]
58 return ret
59
60def _pkgmap(d):
61 """Return a dictionary mapping package to recipe name."""
62
Brad Bishop6e60e8b2018-02-01 10:27:11 -050063 pkgdatadir = d.getVar("PKGDATA_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064
65 pkgmap = {}
66 try:
67 files = os.listdir(pkgdatadir)
68 except OSError:
69 bb.warn("No files in %s?" % pkgdatadir)
70 files = []
71
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072 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 -050073 try:
74 pkgdata = read_pkgdatafile(os.path.join(pkgdatadir, pn))
75 except OSError:
76 continue
77
78 packages = pkgdata.get("PACKAGES") or ""
79 for pkg in packages.split():
80 pkgmap[pkg] = pn
81
82 return pkgmap
83
84def pkgmap(d):
85 """Return a dictionary mapping package to recipe name.
86 Cache the mapping in the metadata"""
87
88 pkgmap_data = d.getVar("__pkgmap_data", False)
89 if pkgmap_data is None:
90 pkgmap_data = _pkgmap(d)
91 d.setVar("__pkgmap_data", pkgmap_data)
92
93 return pkgmap_data
94
95def recipename(pkg, d):
96 """Return the recipe name for the given binary package name."""
97
98 return pkgmap(d).get(pkg)