install_util.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2022-2023 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. import json
  9. import os
  10. import sys
  11. from itertools import chain
  12. def action_extract_features(args: str) -> None:
  13. """
  14. Command line arguments starting with "--enable-" or "--disable" are features. This function selects those, add them signs '+' or '-' and prints them.
  15. """
  16. features = ['+core'] # "core" features should be always installed
  17. if args:
  18. arg_enable_prefix = '--enable-'
  19. arg_disable_prefix = '--disable-'
  20. # features to be enabled has prefix '+', disabled has prefix '-'
  21. for arg in args.split():
  22. if arg.startswith(arg_enable_prefix):
  23. features.append('+' + arg[len(arg_enable_prefix):])
  24. elif arg.startswith(arg_disable_prefix):
  25. features.append('-' + arg[len(arg_disable_prefix):])
  26. features = list(set(features))
  27. print(','.join(features))
  28. def action_extract_targets(args: str) -> None:
  29. """
  30. Command line arguments starting with "esp" are chip targets. This function selects those and prints them.
  31. """
  32. target_sep = ','
  33. targets = []
  34. if args:
  35. target_args = (arg for arg in args.split() if arg.lower().startswith('esp'))
  36. # target_args can be comma-separated lists of chip targets
  37. targets = list(chain.from_iterable(commalist.split(target_sep) for commalist in target_args))
  38. print(target_sep.join(targets or ['all']))
  39. def action_print_help(script_extension: str) -> None:
  40. """
  41. This function prints a help message explaining how to use the install scripts.
  42. """
  43. # extract the list of features from ./requirements.json
  44. thisdir = os.path.dirname(os.path.realpath(__file__))
  45. with open(f'{thisdir}/requirements.json', 'r') as f:
  46. json_data = json.load(f)
  47. features = [feat['name'] for feat in json_data['features']]
  48. if script_extension == 'bat':
  49. help_opts = '/?, -h, --help'
  50. elif script_extension == 'ps1':
  51. help_opts = '-h '
  52. else:
  53. help_opts = '-h, --help '
  54. print(f"""usage: .{os.path.sep}install.{script_extension} [-h] [targets-to-install] [--enable-*] [--disable-*]
  55. optional arguments:
  56. targets-to-install 'all', a single target (e.g. 'esp32s2'), or a comma-separated list of targets (e.g. 'esp32,esp32c3,esp32h2')
  57. --enable-* a specific feature to enable (e.g. '--enable-pytest' will enable feature pytest)
  58. --disable-* a specific feature to disable (e.g. '--disable-pytest' will disable feature pytest)
  59. supported features: {', '.join(features)}
  60. {help_opts} show this help message and exit
  61. For more information, please see https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/tools/idf-tools.html#install-scripts
  62. """)
  63. def main() -> None:
  64. parser = argparse.ArgumentParser()
  65. subparsers = parser.add_subparsers(dest='action', required=True)
  66. extract = subparsers.add_parser('extract', help='Process arguments and extract part of it')
  67. extract.add_argument('type', choices=['targets', 'features'])
  68. extract.add_argument('str-to-parse', nargs='?')
  69. if 'print_help' in sys.argv: # keep this option hidden until used
  70. print_help = subparsers.add_parser('print_help', help='Show install script help')
  71. print_help.add_argument('extension', choices=['sh', 'fish', 'ps1', 'bat'])
  72. args, unknown_args = parser.parse_known_args()
  73. # standalone "--enable-" won't be included into str-to-parse
  74. if args.action == 'print_help':
  75. action_print_help(args.extension)
  76. sys.exit(0)
  77. action_func = globals()['action_{}_{}'.format(args.action, args.type)]
  78. str_to_parse = vars(args)['str-to-parse'] or ''
  79. action_func(' '.join(chain([str_to_parse], unknown_args)))
  80. if __name__ == '__main__':
  81. main()