blob: b2ed8b5a3d8dc0a32216e87441d6745daedc254e [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007import codecs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05008import os
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
10def packaged(pkg, d):
11 return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK)
12
13def read_pkgdatafile(fn):
14 pkgdata = {}
15
16 def decode(str):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017 c = codecs.getdecoder("unicode_escape")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018 return c(str)[0]
19
20 if os.access(fn, os.R_OK):
21 import re
Brad Bishop64c979e2019-11-04 13:55:29 -050022 with open(fn, 'r') as f:
23 lines = f.readlines()
Andrew Geisslereff27472021-10-29 15:35:00 -050024 r = re.compile(r"(^.+?):\s+(.*)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 for l in lines:
26 m = r.match(l)
27 if m:
28 pkgdata[m.group(1)] = decode(m.group(2))
29
30 return pkgdata
31
32def get_subpkgedata_fn(pkg, d):
33 return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg)
34
35def has_subpkgdata(pkg, d):
36 return os.access(get_subpkgedata_fn(pkg, d), os.R_OK)
37
38def read_subpkgdata(pkg, d):
39 return read_pkgdatafile(get_subpkgedata_fn(pkg, d))
40
41def has_pkgdata(pn, d):
42 fn = d.expand('${PKGDATA_DIR}/%s' % pn)
43 return os.access(fn, os.R_OK)
44
45def read_pkgdata(pn, d):
46 fn = d.expand('${PKGDATA_DIR}/%s' % pn)
47 return read_pkgdatafile(fn)
48
49#
Patrick Williams0ca19cc2021-08-16 14:03:13 -050050# Collapse FOO:pkg variables into FOO
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051#
52def read_subpkgdata_dict(pkg, d):
53 ret = {}
54 subd = read_pkgdatafile(get_subpkgedata_fn(pkg, d))
55 for var in subd:
Patrick Williams0ca19cc2021-08-16 14:03:13 -050056 newvar = var.replace(":" + pkg, "")
57 if newvar == var and var + ":" + pkg in subd:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 continue
59 ret[newvar] = subd[var]
60 return ret
61
Andrew Geissler5199d832021-09-24 16:47:35 -050062def read_subpkgdata_extended(pkg, d):
63 import json
64 import bb.compress.zstd
65
66 fn = d.expand("${PKGDATA_DIR}/extended/%s.json.zstd" % pkg)
67 try:
68 num_threads = int(d.getVar("BB_NUMBER_THREADS"))
69 with bb.compress.zstd.open(fn, "rt", encoding="utf-8", num_threads=num_threads) as f:
70 return json.load(f)
71 except FileNotFoundError:
72 return None
73
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074def _pkgmap(d):
75 """Return a dictionary mapping package to recipe name."""
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 pkgdatadir = d.getVar("PKGDATA_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79 pkgmap = {}
80 try:
81 files = os.listdir(pkgdatadir)
82 except OSError:
83 bb.warn("No files in %s?" % pkgdatadir)
84 files = []
85
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086 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 -050087 try:
88 pkgdata = read_pkgdatafile(os.path.join(pkgdatadir, pn))
89 except OSError:
90 continue
91
92 packages = pkgdata.get("PACKAGES") or ""
93 for pkg in packages.split():
94 pkgmap[pkg] = pn
95
96 return pkgmap
97
98def pkgmap(d):
99 """Return a dictionary mapping package to recipe name.
100 Cache the mapping in the metadata"""
101
102 pkgmap_data = d.getVar("__pkgmap_data", False)
103 if pkgmap_data is None:
104 pkgmap_data = _pkgmap(d)
105 d.setVar("__pkgmap_data", pkgmap_data)
106
107 return pkgmap_data
108
109def recipename(pkg, d):
110 """Return the recipe name for the given binary package name."""
111
112 return pkgmap(d).get(pkg)