check.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return CheckStatus.OK
  43. def _check_avrdude_version():
  44. last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
  45. version_number = last_line.split()[2][:-1]
  46. cli.log.info('Found avrdude version %s', version_number)
  47. return CheckStatus.OK
  48. def _check_dfu_util_version():
  49. first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
  50. version_number = first_line.split()[1]
  51. cli.log.info('Found dfu-util version %s', version_number)
  52. return CheckStatus.OK
  53. def _check_dfu_programmer_version():
  54. first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
  55. version_number = first_line.split()[1]
  56. cli.log.info('Found dfu-programmer version %s', version_number)
  57. return CheckStatus.OK
  58. def check_binaries():
  59. """Iterates through ESSENTIAL_BINARIES and tests them.
  60. """
  61. ok = CheckStatus.OK
  62. for binary in sorted(ESSENTIAL_BINARIES):
  63. try:
  64. if not is_executable(binary):
  65. ok = CheckStatus.ERROR
  66. except TimeoutExpired:
  67. cli.log.debug('Timeout checking %s', binary)
  68. if ok != CheckStatus.ERROR:
  69. ok = CheckStatus.WARNING
  70. return ok
  71. def check_binary_versions():
  72. """Check the versions of ESSENTIAL_BINARIES
  73. """
  74. checks = {
  75. 'arm-none-eabi-gcc': _check_arm_gcc_version,
  76. 'avr-gcc': _check_avr_gcc_version,
  77. 'avrdude': _check_avrdude_version,
  78. 'dfu-util': _check_dfu_util_version,
  79. 'dfu-programmer': _check_dfu_programmer_version,
  80. }
  81. versions = []
  82. for binary in sorted(ESSENTIAL_BINARIES):
  83. if 'output' not in ESSENTIAL_BINARIES[binary]:
  84. cli.log.warning('Unknown version for %s', binary)
  85. versions.append(CheckStatus.WARNING)
  86. continue
  87. check = checks[binary]
  88. versions.append(check())
  89. return versions
  90. def check_submodules():
  91. """Iterates through all submodules to make sure they're cloned and up to date.
  92. """
  93. for submodule in submodules.status().values():
  94. if submodule['status'] is None:
  95. return CheckStatus.ERROR
  96. elif not submodule['status']:
  97. return CheckStatus.WARNING
  98. return CheckStatus.OK
  99. def is_executable(command):
  100. """Returns True if command exists and can be executed.
  101. """
  102. # Make sure the command is in the path.
  103. res = shutil.which(command)
  104. if res is None:
  105. cli.log.error("{fg_red}Can't find %s in your path.", command)
  106. return False
  107. # Make sure the command can be executed
  108. version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
  109. check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5)
  110. ESSENTIAL_BINARIES[command]['output'] = check.stdout
  111. if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
  112. cli.log.debug('Found {fg_cyan}%s', command)
  113. return True
  114. cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
  115. return False
  116. def release_info(file='/etc/os-release'):
  117. """Parse release info to dict
  118. """
  119. ret = {}
  120. try:
  121. with open(file) as f:
  122. for line in f:
  123. if '=' in line:
  124. key, value = map(str.strip, line.split('=', 1))
  125. if value.startswith('"') and value.endswith('"'):
  126. value = value[1:-1]
  127. ret[key] = value
  128. except (PermissionError, FileNotFoundError):
  129. pass
  130. return ret