install_util.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. def action_extract_features(args: str) -> None:
  10. """
  11. Command line arguments starting with "--enable-" are features. This function selects those and prints them.
  12. """
  13. features = ['core'] # "core" features should be always installed
  14. if args:
  15. arg_prefix = '--enable-'
  16. features += [arg[len(arg_prefix):] for arg in args.split() if arg.startswith(arg_prefix)]
  17. print(','.join(features))
  18. def action_extract_targets(args: str) -> None:
  19. """
  20. Command line arguments starting with "esp" are chip targets. This function selects those and prints them.
  21. """
  22. target_sep = ','
  23. targets = []
  24. if args:
  25. target_args = (arg for arg in args.split() if arg.lower().startswith('esp'))
  26. # target_args can be comma-separated lists of chip targets
  27. targets = list(chain.from_iterable(commalist.split(target_sep) for commalist in target_args))
  28. print(target_sep.join(targets or ['all']))
  29. def main() -> None:
  30. parser = argparse.ArgumentParser()
  31. subparsers = parser.add_subparsers(dest='action', required=True)
  32. extract = subparsers.add_parser('extract', help='Process arguments and extract part of it')
  33. extract.add_argument('type', choices=['targets', 'features'])
  34. extract.add_argument('str-to-parse', nargs='?')
  35. args, unknown_args = parser.parse_known_args()
  36. # standalone "--enable-" won't be included into str-to-parse
  37. action_func = globals()['action_{}_{}'.format(args.action, args.type)]
  38. str_to_parse = vars(args)['str-to-parse'] or ''
  39. action_func(' '.join(chain([str_to_parse], unknown_args)))
  40. if __name__ == '__main__':
  41. main()