packagedata.py 2.4 KB

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