install_util.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. # This script is used from the $IDF_PATH/install.* scripts. This way the argument parsing can be done at one place and
  6. # doesn't have to be implemented for all shells.
  7. import argparse
  8. from itertools import chain
  9. try:
  10. import python_version_checker
  11. # check the Python version before it will fail with an exception on syntax or package incompatibility.
  12. python_version_checker.check()
  13. except RuntimeError as e:
  14. print(e)
  15. raise SystemExit(1)
  16. def action_extract_features(args: str) -> None:
  17. """
  18. Command line arguments starting with "--enable-" are features. This function selects those and prints them.
  19. """
  20. features = ['core'] # "core" features should be always installed
  21. if args:
  22. arg_prefix = '--enable-'
  23. features += [arg[len(arg_prefix):] for arg in args.split() if arg.startswith(arg_prefix)]
  24. print(','.join(features))
  25. def action_extract_targets(args: str) -> None:
  26. """
  27. Command line arguments starting with "esp" are chip targets. This function selects those and prints them.
  28. """
  29. target_sep = ','
  30. targets = []
  31. if args:
  32. target_args = (arg for arg in args.split() if arg.lower().startswith('esp'))
  33. # target_args can be comma-separated lists of chip targets
  34. targets = list(chain.from_iterable(commalist.split(target_sep) for commalist in target_args))
  35. print(target_sep.join(targets or ['all']))
  36. def main() -> None:
  37. parser = argparse.ArgumentParser()
  38. subparsers = parser.add_subparsers(dest='action', required=True)
  39. extract = subparsers.add_parser('extract', help='Process arguments and extract part of it')
  40. extract.add_argument('type', choices=['targets', 'features'])
  41. extract.add_argument('str-to-parse', nargs='?')
  42. args, unknown_args = parser.parse_known_args()
  43. # standalone "--enable-" won't be included into str-to-parse
  44. action_func = globals()['action_{}_{}'.format(args.action, args.type)]
  45. str_to_parse = vars(args)['str-to-parse'] or ''
  46. action_func(' '.join(chain([str_to_parse], unknown_args)))
  47. if __name__ == '__main__':
  48. main()