bitbake-getvar 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #! /usr/bin/env python3
  2. #
  3. # Copyright (C) 2021 Richard Purdie
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. import argparse
  8. import io
  9. import os
  10. import sys
  11. import warnings
  12. warnings.simplefilter("default")
  13. bindir = os.path.dirname(__file__)
  14. topdir = os.path.dirname(bindir)
  15. sys.path[0:0] = [os.path.join(topdir, 'lib')]
  16. import bb.providers
  17. import bb.tinfoil
  18. if __name__ == "__main__":
  19. parser = argparse.ArgumentParser(description="Bitbake Query Variable")
  20. parser.add_argument("variable", help="variable name to query")
  21. parser.add_argument("-r", "--recipe", help="Recipe name to query", default=None, required=False)
  22. parser.add_argument('-u', '--unexpand', help='Do not expand the value (with --value)', action="store_true")
  23. parser.add_argument('-f', '--flag', help='Specify a variable flag to query (with --value)', default=None)
  24. parser.add_argument('--value', help='Only report the value, no history and no variable name', action="store_true")
  25. parser.add_argument('-q', '--quiet', help='Silence bitbake server logging', action="store_true")
  26. parser.add_argument('--ignore-undefined', help='Suppress any errors related to undefined variables', action="store_true")
  27. args = parser.parse_args()
  28. if not args.value:
  29. if args.unexpand:
  30. sys.exit("--unexpand only makes sense with --value")
  31. if args.flag:
  32. sys.exit("--flag only makes sense with --value")
  33. quiet = args.quiet or args.value
  34. with bb.tinfoil.Tinfoil(tracking=True, setup_logging=not quiet) as tinfoil:
  35. if args.recipe:
  36. tinfoil.prepare(quiet=3 if quiet else 2)
  37. try:
  38. d = tinfoil.parse_recipe(args.recipe)
  39. except bb.providers.NoProvider as e:
  40. sys.exit(str(e))
  41. else:
  42. tinfoil.prepare(quiet=2, config_only=True)
  43. # Expand keys and run anonymous functions to get identical result to
  44. # "bitbake -e"
  45. d = tinfoil.finalizeData()
  46. value = None
  47. if args.flag:
  48. value = d.getVarFlag(args.variable, args.flag, expand=not args.unexpand)
  49. if value is None and not args.ignore_undefined:
  50. sys.exit(f"The flag '{args.flag}' is not defined for variable '{args.variable}'")
  51. else:
  52. value = d.getVar(args.variable, expand=not args.unexpand)
  53. if value is None and not args.ignore_undefined:
  54. sys.exit(f"The variable '{args.variable}' is not defined")
  55. if args.value:
  56. print(str(value if value is not None else ""))
  57. else:
  58. bb.data.emit_var(args.variable, d=d, all=True)