__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  17. safe_commands = [
  18. # A list of subcommands we always run, even when the module imports fail
  19. 'clone',
  20. 'config',
  21. 'env',
  22. 'setup',
  23. ]
  24. def _run_cmd(*command):
  25. """Run a command in a subshell.
  26. """
  27. if 'windows' in cli.platform.lower():
  28. safecmd = map(shlex.quote, command)
  29. safecmd = ' '.join(safecmd)
  30. command = [os.environ['SHELL'], '-c', safecmd]
  31. return run(command)
  32. def _find_broken_requirements(requirements):
  33. """ Check if the modules in the given requirements.txt are available.
  34. Args:
  35. requirements
  36. The path to a requirements.txt file
  37. Returns a list of modules that couldn't be imported
  38. """
  39. with Path(requirements).open() as fd:
  40. broken_modules = []
  41. for line in fd.readlines():
  42. line = line.strip().replace('<', '=').replace('>', '=')
  43. if len(line) == 0 or line[0] == '#' or line.startswith('-r'):
  44. continue
  45. if '#' in line:
  46. line = line.split('#')[0]
  47. module_name = line.split('=')[0] if '=' in line else line
  48. module_import = module_name.replace('-', '_')
  49. # Not every module is importable by its own name.
  50. if module_name in import_names:
  51. module_import = import_names[module_name]
  52. if not find_spec(module_import):
  53. broken_modules.append(module_name)
  54. return broken_modules
  55. def _broken_module_imports(requirements):
  56. """Make sure we can import all the python modules.
  57. """
  58. broken_modules = _find_broken_requirements(requirements)
  59. for module in broken_modules:
  60. print('Could not find module %s!' % module)
  61. if broken_modules:
  62. return True
  63. return False
  64. # Make sure our python is new enough
  65. #
  66. # Supported version information
  67. #
  68. # Based on the OSes we support these are the minimum python version available by default.
  69. # Last update: 2021 Jan 02
  70. #
  71. # Arch: 3.9
  72. # Debian: 3.7
  73. # Fedora 31: 3.7
  74. # Fedora 32: 3.8
  75. # Fedora 33: 3.9
  76. # FreeBSD: 3.7
  77. # Gentoo: 3.7
  78. # macOS: 3.9 (from homebrew)
  79. # msys2: 3.8
  80. # Slackware: 3.7
  81. # solus: 3.7
  82. # void: 3.9
  83. if sys.version_info[0] != 3 or sys.version_info[1] < 7:
  84. print('Error: Your Python is too old! Please upgrade to Python 3.7 or later.')
  85. exit(127)
  86. milc_version = __VERSION__.split('.')
  87. if int(milc_version[0]) < 2 and int(milc_version[1]) < 3:
  88. requirements = Path('requirements.txt').resolve()
  89. print(f'Your MILC library is too old! Please upgrade: python3 -m pip install -U -r {str(requirements)}')
  90. exit(127)
  91. # Check to make sure we have all our dependencies
  92. msg_install = 'Please run `python3 -m pip install -r %s` to install required python dependencies.'
  93. args = sys.argv[1:]
  94. while args and args[0][0] == '-':
  95. del args[0]
  96. if not args or args[0] not in safe_commands:
  97. if _broken_module_imports('requirements.txt'):
  98. if yesno('Would you like to install the required Python modules?'):
  99. _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt')
  100. else:
  101. print()
  102. print(msg_install % (str(Path('requirements.txt').resolve()),))
  103. print()
  104. exit(1)
  105. if cli.config.user.developer and _broken_module_imports('requirements-dev.txt'):
  106. if yesno('Would you like to install the required developer Python modules?'):
  107. _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements-dev.txt')
  108. elif yesno('Would you like to disable developer mode?'):
  109. _run_cmd(sys.argv[0], 'config', 'user.developer=None')
  110. else:
  111. print()
  112. print(msg_install % (str(Path('requirements-dev.txt').resolve()),))
  113. print('You can also turn off developer mode: qmk config user.developer=None')
  114. print()
  115. exit(1)
  116. # Import our subcommands
  117. from . import bux # noqa
  118. from . import c2json # noqa
  119. from . import cformat # noqa
  120. from . import chibios # noqa
  121. from . import clean # noqa
  122. from . import compile # noqa
  123. from milc.subcommand import config # noqa
  124. from . import console # noqa
  125. from . import docs # noqa
  126. from . import doctor # noqa
  127. from . import fileformat # noqa
  128. from . import flash # noqa
  129. from . import format # noqa
  130. from . import generate # noqa
  131. from . import hello # noqa
  132. from . import info # noqa
  133. from . import json2c # noqa
  134. from . import lint # noqa
  135. from . import list # noqa
  136. from . import kle2json # noqa
  137. from . import multibuild # noqa
  138. from . import new # noqa
  139. from . import pyformat # noqa
  140. from . import pytest # noqa