packagedata.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import codecs
  5. import os
  6. def packaged(pkg, d):
  7. return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK)
  8. def read_pkgdatafile(fn):
  9. pkgdata = {}
  10. def decode(str):
  11. c = codecs.getdecoder("unicode_escape")
  12. return c(str)[0]
  13. if os.access(fn, os.R_OK):
  14. import re
  15. with open(fn, 'r') as f:
  16. lines = f.readlines()
  17. r = re.compile("([^:]+):\s*(.*)")
  18. for l in lines:
  19. m = r.match(l)
  20. if m:
  21. pkgdata[m.group(1)] = decode(m.group(2))
  22. return pkgdata
  23. def get_subpkgedata_fn(pkg, d):
  24. return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg)
  25. def has_subpkgdata(pkg, d):
  26. return os.access(get_subpkgedata_fn(pkg, d), os.R_OK)
  27. def read_subpkgdata(pkg, d):
  28. return read_pkgdatafile(get_subpkgedata_fn(pkg, d))
  29. def has_pkgdata(pn, d):
  30. fn = d.expand('${PKGDATA_DIR}/%s' % pn)
  31. return os.access(fn, os.R_OK)
  32. def read_pkgdata(pn, d):
  33. fn = d.expand('${PKGDATA_DIR}/%s' % pn)
  34. return read_pkgdatafile(fn)
  35. #
  36. # Collapse FOO_pkg variables into FOO
  37. #
  38. def read_subpkgdata_dict(pkg, d):
  39. ret = {}
  40. subd = read_pkgdatafile(get_subpkgedata_fn(pkg, d))
  41. for var in subd:
  42. newvar = var.replace("_" + pkg, "")
  43. if newvar == var and var + "_" + pkg in subd:
  44. continue
  45. ret[newvar] = subd[var]
  46. return ret
  47. def _pkgmap(d):
  48. """Return a dictionary mapping package to recipe name."""
  49. pkgdatadir = d.getVar("PKGDATA_DIR")
  50. pkgmap = {}
  51. try:
  52. files = os.listdir(pkgdatadir)
  53. except OSError:
  54. bb.warn("No files in %s?" % pkgdatadir)
  55. files = []
  56. for pn in [f for f in files if not os.path.isdir(os.path.join(pkgdatadir, f))]:
  57. try:
  58. pkgdata = read_pkgdatafile(os.path.join(pkgdatadir, pn))
  59. except OSError:
  60. continue
  61. packages = pkgdata.get("PACKAGES") or ""
  62. for pkg in packages.split():
  63. pkgmap[pkg] = pn
  64. return pkgmap
  65. def pkgmap(d):
  66. """Return a dictionary mapping package to recipe name.
  67. Cache the mapping in the metadata"""
  68. pkgmap_data = d.getVar("__pkgmap_data", False)
  69. if pkgmap_data is None:
  70. pkgmap_data = _pkgmap(d)
  71. d.setVar("__pkgmap_data", pkgmap_data)
  72. return pkgmap_data
  73. def recipename(pkg, d):
  74. """Return the recipe name for the given binary package name."""
  75. return pkgmap(d).get(pkg)