check.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """Check for specific programs.
  2. """
  3. from enum import Enum
  4. import re
  5. import shutil
  6. from subprocess import DEVNULL, TimeoutExpired
  7. from milc import cli
  8. from qmk import submodules
  9. class CheckStatus(Enum):
  10. OK = 1
  11. WARNING = 2
  12. ERROR = 3
  13. ESSENTIAL_BINARIES = {
  14. 'dfu-programmer': {},
  15. 'avrdude': {},
  16. 'dfu-util': {},
  17. 'avr-gcc': {
  18. 'version_arg': '-dumpversion'
  19. },
  20. 'arm-none-eabi-gcc': {
  21. 'version_arg': '-dumpversion'
  22. },
  23. }
  24. def _parse_gcc_version(version):
  25. m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)
  26. return {
  27. 'major': int(m.group(1)),
  28. 'minor': int(m.group(2)) if m.group(2) else 0,
  29. 'patch': int(m.group(3)) if m.group(3) else 0,
  30. }
  31. def _check_arm_gcc_version():
  32. """Returns True if the arm-none-eabi-gcc version is not known to cause problems.
  33. """
  34. version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
  35. cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
  36. return CheckStatus.OK # Right now all known arm versions are ok
  37. def _check_avr_gcc_version():
  38. """Returns True if the avr-gcc version is not known to cause problems.
  39. """
  40. version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
  41. cli.log.info('Found avr-gcc version %s', version_number)
  42. parsed_version = _parse_gcc_version(version_number)
  43. if parsed_version['major'] > 8:
  44. cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
  45. return CheckStatus.WARNING
  46. return CheckStatus.OK
  47. def _check_avrdude_version():
  48. last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
  49. version_number = last_line.split()[2][:-1]
  50. cli.log.info('Found avrdude version %s', version_number)
  51. return CheckStatus.OK
  52. def _check_dfu_util_version():
  53. first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
  54. version_number = first_line.split()[1]
  55. cli.log.info('Found dfu-util version %s', version_number)
  56. return CheckStatus.OK
  57. def _check_dfu_programmer_version():
  58. first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
  59. version_number = first_line.split()[1]
  60. cli.log.info('Found dfu-programmer version %s', version_number)
  61. return CheckStatus.OK
  62. def check_binaries():
  63. """Iterates through ESSENTIAL_BINARIES and tests them.
  64. """
  65. ok = CheckStatus.OK
  66. for binary in sorted(ESSENTIAL_BINARIES):
  67. try:
  68. if not is_executable(binary):
  69. ok = CheckStatus.ERROR
  70. except TimeoutExpired:
  71. cli.log.debug('Timeout checking %s', binary)
  72. if ok != CheckStatus.ERROR:
  73. ok = CheckStatus.WARNING
  74. return ok
  75. def check_binary_versions():
  76. """Check the versions of ESSENTIAL_BINARIES
  77. """
  78. checks = {
  79. 'arm-none-eabi-gcc': _check_arm_gcc_version,
  80. 'avr-gcc': _check_avr_gcc_version,
  81. 'avrdude': _check_avrdude_version,
  82. 'dfu-util': _check_dfu_util_version,
  83. 'dfu-programmer': _check_dfu_programmer_version,
  84. }
  85. versions = []
  86. for binary in sorted(ESSENTIAL_BINARIES):
  87. if 'output' not in ESSENTIAL_BINARIES[binary]:
  88. cli.log.warning('Unknown version for %s', binary)
  89. versions.append(CheckStatus.WARNING)
  90. continue
  91. check = checks[binary]
  92. versions.append(check())
  93. return versions
  94. def check_submodules():
  95. """Iterates through all submodules to make sure they're cloned and up to date.
  96. """
  97. for submodule in submodules.status().values():
  98. if submodule['status'] is None:
  99. return CheckStatus.ERROR
  100. elif not submodule['status']:
  101. return CheckStatus.WARNING
  102. return CheckStatus.OK
  103. def is_executable(command):
  104. """Returns True if command exists and can be executed.
  105. """
  106. # Make sure the command is in the path.
  107. res = shutil.which(command)
  108. if res is None:
  109. cli.log.error("{fg_red}Can't find %s in your path.", command)
  110. return False
  111. # Make sure the command can be executed
  112. version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
  113. check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5)
  114. ESSENTIAL_BINARIES[command]['output'] = check.stdout
  115. if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
  116. cli.log.debug('Found {fg_cyan}%s', command)
  117. return True
  118. cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
  119. return False
  120. def release_info(file='/etc/os-release'):
  121. """Parse release info to dict
  122. """
  123. ret = {}
  124. try:
  125. with open(file) as f:
  126. for line in f:
  127. if '=' in line:
  128. key, value = map(str.strip, line.split('=', 1))
  129. if value.startswith('"') and value.endswith('"'):
  130. value = value[1:-1]
  131. ret[key] = value
  132. except (PermissionError, FileNotFoundError):
  133. pass
  134. return ret