__init__.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """QMK CLI Subcommands
  2. We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup.
  3. """
  4. import os
  5. import shlex
  6. import sys
  7. from importlib.util import find_spec
  8. from pathlib import Path
  9. from subprocess import run
  10. from milc import cli, __VERSION__
  11. from milc.questions import yesno
  12. import_names = {
  13. # A mapping of package name to importable name
  14. 'pep8-naming': 'pep8ext_naming',
  15. 'pyusb': 'usb.core',
  16. 'qmk-dotty-dict': 'dotty_dict'
  17. }
  18. safe_commands = [
  19. # A list of subcommands we always run, even when the module imports fail
  20. 'clone',
  21. 'config',
  22. 'doctor',
  23. 'env',
  24. 'setup',
  25. ]
  26. subcommands = [
  27. 'qmk.cli.bux',
  28. 'qmk.cli.c2json',
  29. 'qmk.cli.cformat',
  30. 'qmk.cli.chibios.confmigrate',
  31. 'qmk.cli.clean',
  32. 'qmk.cli.compile',
  33. 'qmk.cli.console',
  34. 'qmk.cli.docs',
  35. 'qmk.cli.doctor',
  36. 'qmk.cli.fileformat',
  37. 'qmk.cli.flash',
  38. 'qmk.cli.format.json',
  39. 'qmk.cli.generate.api',
  40. 'qmk.cli.generate.config_h',
  41. 'qmk.cli.generate.dfu_header',
  42. 'qmk.cli.generate.docs',
  43. 'qmk.cli.generate.info_json',
  44. 'qmk.cli.generate.keyboard_h',
  45. 'qmk.cli.generate.layouts',
  46. 'qmk.cli.generate.rgb_breathe_table',
  47. 'qmk.cli.generate.rules_mk',
  48. 'qmk.cli.hello',
  49. 'qmk.cli.info',
  50. 'qmk.cli.json2c',
  51. 'qmk.cli.lint',
  52. 'qmk.cli.list.keyboards',
  53. 'qmk.cli.list.keymaps',
  54. 'qmk.cli.kle2json',
  55. 'qmk.cli.multibuild',
  56. 'qmk.cli.new.keyboard',
  57. 'qmk.cli.new.keymap',
  58. 'qmk.cli.pyformat',
  59. 'qmk.cli.pytest',
  60. ]
  61. def _run_cmd(*command):
  62. """Run a command in a subshell.
  63. """
  64. if 'windows' in cli.platform.lower():
  65. safecmd = map(shlex.quote, command)
  66. safecmd = ' '.join(safecmd)
  67. command = [os.environ['SHELL'], '-c', safecmd]
  68. return run(command)
  69. def _find_broken_requirements(requirements):
  70. """ Check if the modules in the given requirements.txt are available.
  71. Args:
  72. requirements
  73. The path to a requirements.txt file
  74. Returns a list of modules that couldn't be imported
  75. """
  76. with Path(requirements).open() as fd:
  77. broken_modules = []
  78. for line in fd.readlines():
  79. line = line.strip().replace('<', '=').replace('>', '=')
  80. if len(line) == 0 or line[0] == '#' or line.startswith('-r'):
  81. continue
  82. if '#' in line:
  83. line = line.split('#')[0]
  84. module_name = line.split('=')[0] if '=' in line else line
  85. module_import = module_name.replace('-', '_')
  86. # Not every module is importable by its own name.
  87. if module_name in import_names:
  88. module_import = import_names[module_name]
  89. if not find_spec(module_import):
  90. broken_modules.append(module_name)
  91. return broken_modules
  92. def _broken_module_imports(requirements):
  93. """Make sure we can import all the python modules.
  94. """
  95. broken_modules = _find_broken_requirements(requirements)
  96. for module in broken_modules:
  97. print('Could not find module %s!' % module)
  98. if broken_modules:
  99. return True
  100. return False
  101. # Make sure our python is new enough
  102. #
  103. # Supported version information
  104. #
  105. # Based on the OSes we support these are the minimum python version available by default.
  106. # Last update: 2021 Jan 02
  107. #
  108. # Arch: 3.9
  109. # Debian: 3.7
  110. # Fedora 31: 3.7
  111. # Fedora 32: 3.8
  112. # Fedora 33: 3.9
  113. # FreeBSD: 3.7
  114. # Gentoo: 3.7
  115. # macOS: 3.9 (from homebrew)
  116. # msys2: 3.8
  117. # Slackware: 3.7
  118. # solus: 3.7
  119. # void: 3.9
  120. if sys.version_info[0] != 3 or sys.version_info[1] < 7:
  121. print('Error: Your Python is too old! Please upgrade to Python 3.7 or later.')
  122. exit(127)
  123. milc_version = __VERSION__.split('.')
  124. if int(milc_version[0]) < 2 and int(milc_version[1]) < 4:
  125. requirements = Path('requirements.txt').resolve()
  126. print(f'Your MILC library is too old! Please upgrade: python3 -m pip install -U -r {str(requirements)}')
  127. exit(127)
  128. # Check to make sure we have all our dependencies
  129. msg_install = 'Please run `python3 -m pip install -r %s` to install required python dependencies.'
  130. args = sys.argv[1:]
  131. while args and args[0][0] == '-':
  132. del args[0]
  133. safe_command = args and args[0] in safe_commands
  134. if not safe_command:
  135. if _broken_module_imports('requirements.txt'):
  136. if yesno('Would you like to install the required Python modules?'):
  137. _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt')
  138. else:
  139. print()
  140. print(msg_install % (str(Path('requirements.txt').resolve()),))
  141. print()
  142. exit(1)
  143. if cli.config.user.developer and _broken_module_imports('requirements-dev.txt'):
  144. if yesno('Would you like to install the required developer Python modules?'):
  145. _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements-dev.txt')
  146. elif yesno('Would you like to disable developer mode?'):
  147. _run_cmd(sys.argv[0], 'config', 'user.developer=None')
  148. else:
  149. print()
  150. print(msg_install % (str(Path('requirements-dev.txt').resolve()),))
  151. print('You can also turn off developer mode: qmk config user.developer=None')
  152. print()
  153. exit(1)
  154. # Import our subcommands
  155. for subcommand in subcommands:
  156. try:
  157. __import__(subcommand)
  158. except (ImportError, ModuleNotFoundError) as e:
  159. if safe_command:
  160. print(f'Warning: Could not import {subcommand}: {e.__class__.__name__}, {e}')
  161. else:
  162. raise