Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import codecs |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 2 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | |
| 4 | def packaged(pkg, d): |
| 5 | return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK) |
| 6 | |
| 7 | def read_pkgdatafile(fn): |
| 8 | pkgdata = {} |
| 9 | |
| 10 | def decode(str): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | c = codecs.getdecoder("unicode_escape") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | 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 | |
| 27 | def get_subpkgedata_fn(pkg, d): |
| 28 | return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg) |
| 29 | |
| 30 | def has_subpkgdata(pkg, d): |
| 31 | return os.access(get_subpkgedata_fn(pkg, d), os.R_OK) |
| 32 | |
| 33 | def read_subpkgdata(pkg, d): |
| 34 | return read_pkgdatafile(get_subpkgedata_fn(pkg, d)) |
| 35 | |
| 36 | def has_pkgdata(pn, d): |
| 37 | fn = d.expand('${PKGDATA_DIR}/%s' % pn) |
| 38 | return os.access(fn, os.R_OK) |
| 39 | |
| 40 | def 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 | # |
| 47 | def 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 | |
| 57 | def _pkgmap(d): |
| 58 | """Return a dictionary mapping package to recipe name.""" |
| 59 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 60 | pkgdatadir = d.getVar("PKGDATA_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | |
| 62 | pkgmap = {} |
| 63 | try: |
| 64 | files = os.listdir(pkgdatadir) |
| 65 | except OSError: |
| 66 | bb.warn("No files in %s?" % pkgdatadir) |
| 67 | files = [] |
| 68 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 69 | 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] | 70 | 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 | |
| 81 | def 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 | |
| 92 | def recipename(pkg, d): |
| 93 | """Return the recipe name for the given binary package name.""" |
| 94 | |
| 95 | return pkgmap(d).get(pkg) |