doctor.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 milc import cli
  6. from milc.questions import yesno
  7. from qmk import submodules
  8. from qmk.constants import QMK_FIRMWARE
  9. from qmk.commands import run
  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. cli.log.info("Detected {fg_cyan}Linux.")
  28. from qmk.os_helpers.linux import check_udev_rules
  29. return check_udev_rules()
  30. def os_test_macos():
  31. """Run the Mac specific tests.
  32. """
  33. cli.log.info("Detected {fg_cyan}macOS.")
  34. return CheckStatus.OK
  35. def os_test_windows():
  36. """Run the Windows specific tests.
  37. """
  38. cli.log.info("Detected {fg_cyan}Windows.")
  39. return CheckStatus.OK
  40. @cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.')
  41. @cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.')
  42. @cli.subcommand('Basic QMK environment checks')
  43. def doctor(cli):
  44. """Basic QMK environment checks.
  45. This is currently very simple, it just checks that all the expected binaries are on your system.
  46. TODO(unclaimed):
  47. * [ ] Compile a trivial program with each compiler
  48. """
  49. cli.log.info('QMK Doctor is checking your environment.')
  50. status = os_tests()
  51. cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE)
  52. # Make sure our QMK home is a Git repo
  53. git_ok = check_git_repo()
  54. if git_ok == CheckStatus.WARNING:
  55. cli.log.warning("QMK home does not appear to be a Git repository! (no .git folder)")
  56. status = CheckStatus.WARNING
  57. # Make sure the basic CLI tools we need are available and can be executed.
  58. bin_ok = check_binaries()
  59. if not bin_ok:
  60. if yesno('Would you like to install dependencies?', default=True):
  61. run(['util/qmk_install.sh'])
  62. bin_ok = check_binaries()
  63. if bin_ok:
  64. cli.log.info('All dependencies are installed.')
  65. else:
  66. status = CheckStatus.ERROR
  67. # Make sure the tools are at the correct version
  68. ver_ok = check_binary_versions()
  69. if CheckStatus.ERROR in ver_ok:
  70. status = CheckStatus.ERROR
  71. elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK:
  72. status = CheckStatus.WARNING
  73. # Check out the QMK submodules
  74. sub_ok = check_submodules()
  75. if sub_ok == CheckStatus.OK:
  76. cli.log.info('Submodules are up to date.')
  77. else:
  78. if yesno('Would you like to clone the submodules?', default=True):
  79. submodules.update()
  80. sub_ok = check_submodules()
  81. if CheckStatus.ERROR in sub_ok:
  82. status = CheckStatus.ERROR
  83. elif CheckStatus.WARNING in sub_ok and status == CheckStatus.OK:
  84. status = CheckStatus.WARNING
  85. # Report a summary of our findings to the user
  86. if status == CheckStatus.OK:
  87. cli.log.info('{fg_green}QMK is ready to go')
  88. return 0
  89. elif status == CheckStatus.WARNING:
  90. cli.log.info('{fg_yellow}QMK is ready to go, but minor problems were found')
  91. return 1
  92. else:
  93. cli.log.info('{fg_red}Major problems detected, please fix these problems before proceeding.')
  94. 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.')
  95. return 2