check.py 4.8 KB

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