doctor.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """QMK Doctor
  2. Check out the user's QMK environment and make sure it's ready to compile.
  3. """
  4. import platform
  5. from subprocess import DEVNULL
  6. from milc import cli
  7. from milc.questions import yesno
  8. from qmk import submodules
  9. from qmk.constants import QMK_FIRMWARE
  10. from qmk.os_helpers import CheckStatus, check_binaries, check_binary_versions, check_submodules, check_git_repo
  11. def os_tests():
  12. """Determine our OS and run platform specific tests
  13. """
  14. platform_id = platform.platform().lower()
  15. if 'darwin' in platform_id or 'macos' in platform_id:
  16. return os_test_macos()
  17. elif 'linux' in platform_id:
  18. return os_test_linux()
  19. elif 'windows' in platform_id:
  20. return os_test_windows()
  21. else:
  22. cli.log.warning('Unsupported OS detected: %s', platform_id)
  23. return CheckStatus.WARNING
  24. def os_test_linux():
  25. """Run the Linux specific tests.
  26. """
  27. # Don't bother with udev on WSL, for now
  28. if 'microsoft' in platform.uname().release.lower():
  29. cli.log.info("Detected {fg_cyan}Linux (WSL){fg_reset}.")
  30. # https://github.com/microsoft/WSL/issues/4197
  31. if QMK_FIRMWARE.as_posix().startswith("/mnt"):
  32. cli.log.warning("I/O performance on /mnt may be extremely slow.")
  33. return CheckStatus.WARNING
  34. return CheckStatus.OK
  35. else:
  36. cli.log.info("Detected {fg_cyan}Linux{fg_reset}.")
  37. from qmk.os_helpers.linux import check_udev_rules
  38. return check_udev_rules()
  39. def os_test_macos():
  40. """Run the Mac specific tests.
  41. """
  42. cli.log.info("Detected {fg_cyan}macOS %s{fg_reset}.", platform.mac_ver()[0])
  43. return CheckStatus.OK
  44. def os_test_windows():
  45. """Run the Windows specific tests.
  46. """
  47. win32_ver = platform.win32_ver()
  48. cli.log.info("Detected {fg_cyan}Windows %s (%s){fg_reset}.", win32_ver[0], win32_ver[1])
  49. return CheckStatus.OK
  50. @cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.')
  51. @cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.')
  52. @cli.subcommand('Basic QMK environment checks')
  53. def doctor(cli):
  54. """Basic QMK environment checks.
  55. This is currently very simple, it just checks that all the expected binaries are on your system.
  56. TODO(unclaimed):
  57. * [ ] Compile a trivial program with each compiler
  58. """
  59. cli.log.info('QMK Doctor is checking your environment.')
  60. cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE)
  61. status = os_tests()
  62. # Make sure our QMK home is a Git repo
  63. git_ok = check_git_repo()
  64. if git_ok == CheckStatus.WARNING:
  65. cli.log.warning("QMK home does not appear to be a Git repository! (no .git folder)")
  66. status = CheckStatus.WARNING
  67. # Make sure the basic CLI tools we need are available and can be executed.
  68. bin_ok = check_binaries()
  69. if not bin_ok:
  70. if yesno('Would you like to install dependencies?', default=True):
  71. cli.run(['util/qmk_install.sh', '-y'], stdin=DEVNULL, capture_output=False)
  72. bin_ok = check_binaries()
  73. if bin_ok:
  74. cli.log.info('All dependencies are installed.')
  75. else:
  76. status = CheckStatus.ERROR
  77. # Make sure the tools are at the correct version
  78. ver_ok = check_binary_versions()
  79. if CheckStatus.ERROR in ver_ok:
  80. status = CheckStatus.ERROR
  81. elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK:
  82. status = CheckStatus.WARNING
  83. # Check out the QMK submodules
  84. sub_ok = check_submodules()
  85. if sub_ok == CheckStatus.OK:
  86. cli.log.info('Submodules are up to date.')
  87. else:
  88. if yesno('Would you like to clone the submodules?', default=True):
  89. submodules.update()
  90. sub_ok = check_submodules()
  91. if sub_ok == CheckStatus.ERROR:
  92. status = CheckStatus.ERROR
  93. elif sub_ok == CheckStatus.WARNING and status == CheckStatus.OK:
  94. status = CheckStatus.WARNING
  95. # Report a summary of our findings to the user
  96. if status == CheckStatus.OK:
  97. cli.log.info('{fg_green}QMK is ready to go')
  98. return 0
  99. elif status == CheckStatus.WARNING:
  100. cli.log.info('{fg_yellow}QMK is ready to go, but minor problems were found')
  101. return 1
  102. else:
  103. cli.log.info('{fg_red}Major problems detected, please fix these problems before proceeding.')
  104. cli.log.info('{fg_blue}Check out the FAQ (https://docs.qmk.fm/#/faq_build) or join the QMK Discord (https://discord.gg/Uq7gcHh) for help.')
  105. return 2