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.

165 lines
4.9 KiB

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