You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.8 KiB

  1. """Check for specific programs.
  2. """
  3. from enum import Enum
  4. import re
  5. import shutil
  6. from subprocess import DEVNULL
  7. from milc import cli
  8. from qmk import submodules
  9. from qmk.constants import QMK_FIRMWARE
  10. class CheckStatus(Enum):
  11. OK = 1
  12. WARNING = 2
  13. ERROR = 3
  14. ESSENTIAL_BINARIES = {
  15. 'dfu-programmer': {},
  16. 'avrdude': {},
  17. 'dfu-util': {},
  18. 'avr-gcc': {
  19. 'version_arg': '-dumpversion'
  20. },
  21. 'arm-none-eabi-gcc': {
  22. 'version_arg': '-dumpversion'
  23. },
  24. 'bin/qmk': {},
  25. }
  26. def _parse_gcc_version(version):
  27. m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)
  28. return {
  29. 'major': int(m.group(1)),
  30. 'minor': int(m.group(2)) if m.group(2) else 0,
  31. 'patch': int(m.group(3)) if m.group(3) else 0,
  32. }
  33. def _check_arm_gcc_version():
  34. """Returns True if the arm-none-eabi-gcc version is not known to cause problems.
  35. """
  36. if 'output' in ESSENTIAL_BINARIES['arm-none-eabi-gcc']:
  37. version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
  38. cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
  39. return CheckStatus.OK # Right now all known arm versions are ok
  40. def _check_avr_gcc_version():
  41. """Returns True if the avr-gcc version is not known to cause problems.
  42. """
  43. rc = CheckStatus.ERROR
  44. if 'output' in ESSENTIAL_BINARIES['avr-gcc']:
  45. version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
  46. cli.log.info('Found avr-gcc version %s', version_number)
  47. rc = CheckStatus.OK
  48. parsed_version = _parse_gcc_version(version_number)
  49. if parsed_version['major'] > 8:
  50. cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
  51. rc = CheckStatus.WARNING
  52. return rc
  53. def _check_avrdude_version():
  54. if 'output' in ESSENTIAL_BINARIES['avrdude']:
  55. last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
  56. version_number = last_line.split()[2][:-1]
  57. cli.log.info('Found avrdude version %s', version_number)
  58. return CheckStatus.OK
  59. def _check_dfu_util_version():
  60. if 'output' in ESSENTIAL_BINARIES['dfu-util']:
  61. first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
  62. version_number = first_line.split()[1]
  63. cli.log.info('Found dfu-util version %s', version_number)
  64. return CheckStatus.OK
  65. def _check_dfu_programmer_version():
  66. if 'output' in ESSENTIAL_BINARIES['dfu-programmer']:
  67. first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
  68. version_number = first_line.split()[1]
  69. cli.log.info('Found dfu-programmer version %s', version_number)
  70. return CheckStatus.OK
  71. def check_binaries():
  72. """Iterates through ESSENTIAL_BINARIES and tests them.
  73. """
  74. ok = True
  75. for binary in sorted(ESSENTIAL_BINARIES):
  76. if not is_executable(binary):
  77. ok = False
  78. return ok
  79. def check_binary_versions():
  80. """Check the versions of ESSENTIAL_BINARIES
  81. """
  82. versions = []
  83. for check in (_check_arm_gcc_version, _check_avr_gcc_version, _check_avrdude_version, _check_dfu_util_version, _check_dfu_programmer_version):
  84. versions.append(check())
  85. return versions
  86. def check_submodules():
  87. """Iterates through all submodules to make sure they're cloned and up to date.
  88. """
  89. for submodule in submodules.status().values():
  90. if submodule['status'] is None:
  91. cli.log.error('Submodule %s has not yet been cloned!', submodule['name'])
  92. return CheckStatus.ERROR
  93. elif not submodule['status']:
  94. cli.log.warning('Submodule %s is not up to date!', submodule['name'])
  95. return CheckStatus.WARNING
  96. return CheckStatus.OK
  97. def is_executable(command):
  98. """Returns True if command exists and can be executed.
  99. """
  100. # Make sure the command is in the path.
  101. res = shutil.which(command)
  102. if res is None:
  103. cli.log.error("{fg_red}Can't find %s in your path.", command)
  104. return False
  105. # Make sure the command can be executed
  106. version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
  107. check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5)
  108. ESSENTIAL_BINARIES[command]['output'] = check.stdout
  109. if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
  110. cli.log.debug('Found {fg_cyan}%s', command)
  111. return True
  112. cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
  113. return False
  114. def check_git_repo():
  115. """Checks that the .git directory exists inside QMK_HOME.
  116. This is a decent enough indicator that the qmk_firmware directory is a
  117. proper Git repository, rather than a .zip download from GitHub.
  118. """
  119. dot_git_dir = QMK_FIRMWARE / '.git'
  120. return CheckStatus.OK if dot_git_dir.is_dir() else CheckStatus.WARNING