setconfig.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2019, Ulf Magnusson
  3. # SPDX-License-Identifier: ISC
  4. """
  5. Simple utility for setting configuration values from the command line.
  6. Sample usage:
  7. $ setconfig FOO_SUPPORT=y BAR_BITS=8
  8. Note: Symbol names should not be prefixed with 'CONFIG_'.
  9. The exit status on errors is 1.
  10. The default input/output configuration file is '.config'. A different filename
  11. can be passed in the KCONFIG_CONFIG environment variable.
  12. When overwriting a configuration file, the old version is saved to
  13. <filename>.old (e.g. .config.old).
  14. """
  15. import argparse
  16. import sys
  17. import kconfiglib
  18. def main():
  19. parser = argparse.ArgumentParser(
  20. formatter_class=argparse.RawDescriptionHelpFormatter,
  21. description=__doc__)
  22. parser.add_argument(
  23. "--kconfig",
  24. default="Kconfig",
  25. help="Top-level Kconfig file (default: Kconfig)")
  26. parser.add_argument(
  27. "--no-check-exists",
  28. dest="check_exists",
  29. action="store_false",
  30. help="Ignore assignments to non-existent symbols instead of erroring "
  31. "out")
  32. parser.add_argument(
  33. "--no-check-value",
  34. dest="check_value",
  35. action="store_false",
  36. help="Ignore assignments that didn't \"take\" (where the symbol got a "
  37. "different value, e.g. due to unsatisfied dependencies) instead "
  38. "of erroring out")
  39. parser.add_argument(
  40. "assignments",
  41. metavar="ASSIGNMENT",
  42. nargs="*",
  43. help="A 'NAME=value' assignment")
  44. args = parser.parse_args()
  45. kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True)
  46. print(kconf.load_config())
  47. for arg in args.assignments:
  48. if "=" not in arg:
  49. sys.exit("error: no '=' in assignment: '{}'".format(arg))
  50. name, value = arg.split("=", 1)
  51. if name not in kconf.syms:
  52. if not args.check_exists:
  53. continue
  54. sys.exit("error: no symbol '{}' in configuration".format(name))
  55. sym = kconf.syms[name]
  56. if not sym.set_value(value):
  57. sys.exit("error: '{}' is an invalid value for the {} symbol {}"
  58. .format(value, kconfiglib.TYPE_TO_STR[sym.orig_type],
  59. name))
  60. if args.check_value and sym.str_value != value:
  61. sys.exit("error: {} was assigned the value '{}', but got the "
  62. "value '{}'. Check the symbol's dependencies, and make "
  63. "sure that it has a prompt."
  64. .format(name, value, sym.str_value))
  65. print(kconf.write_config())
  66. if __name__ == "__main__":
  67. main()