Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | import codecs |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 6 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
| 8 | def packaged(pkg, d): |
| 9 | return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK) |
| 10 | |
| 11 | def read_pkgdatafile(fn): |
| 12 | pkgdata = {} |
| 13 | |
| 14 | def decode(str): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | c = codecs.getdecoder("unicode_escape") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 31 | def get_subpkgedata_fn(pkg, d): |
| 32 | return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg) |
| 33 | |
| 34 | def has_subpkgdata(pkg, d): |
| 35 | return os.access(get_subpkgedata_fn(pkg, d), os.R_OK) |
| 36 | |
| 37 | def read_subpkgdata(pkg, d): |
| 38 | return read_pkgdatafile(get_subpkgedata_fn(pkg, d)) |
| 39 | |
| 40 | def has_pkgdata(pn, d): |
| 41 | fn = d.expand('${PKGDATA_DIR}/%s' % pn) |
| 42 | return os.access(fn, os.R_OK) |
| 43 | |
| 44 | def 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 | # |
| 51 | def 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 | |
| 61 | def _pkgmap(d): |
| 62 | """Return a dictionary mapping package to recipe name.""" |
| 63 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 64 | pkgdatadir = d.getVar("PKGDATA_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | |
| 66 | pkgmap = {} |
| 67 | try: |
| 68 | files = os.listdir(pkgdatadir) |
| 69 | except OSError: |
| 70 | bb.warn("No files in %s?" % pkgdatadir) |
| 71 | files = [] |
| 72 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 73 | for pn in [f for f in files if not os.path.isdir(os.path.join(pkgdatadir, f))]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 85 | def 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 | |
| 96 | def recipename(pkg, d): |
| 97 | """Return the recipe name for the given binary package name.""" |
| 98 | |
| 99 | return pkgmap(d).get(pkg) |