main.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, QMK_FIRMWARE_UPSTREAM
  10. from .check import CheckStatus, check_binaries, check_binary_versions, check_submodules
  11. from qmk.git import git_check_repo, git_get_branch, git_get_tag, git_is_dirty, git_get_remotes, git_check_deviation
  12. from qmk.commands import in_virtualenv
  13. def os_tests():
  14. """Determine our OS and run platform specific tests
  15. """
  16. platform_id = platform.platform().lower()
  17. if 'darwin' in platform_id or 'macos' in platform_id:
  18. from .macos import os_test_macos
  19. return os_test_macos()
  20. elif 'linux' in platform_id:
  21. from .linux import os_test_linux
  22. return os_test_linux()
  23. elif 'windows' in platform_id:
  24. from .windows import os_test_windows
  25. return os_test_windows()
  26. else:
  27. cli.log.warning('Unsupported OS detected: %s', platform_id)
  28. return CheckStatus.WARNING
  29. def git_tests():
  30. """Run Git-related checks
  31. """
  32. status = CheckStatus.OK
  33. # Make sure our QMK home is a Git repo
  34. git_ok = git_check_repo()
  35. if not git_ok:
  36. cli.log.warning("{fg_yellow}QMK home does not appear to be a Git repository! (no .git folder)")
  37. status = CheckStatus.WARNING
  38. else:
  39. git_branch = git_get_branch()
  40. if git_branch:
  41. cli.log.info('Git branch: %s', git_branch)
  42. repo_version = git_get_tag()
  43. if repo_version:
  44. cli.log.info('Repo version: %s', repo_version)
  45. git_dirty = git_is_dirty()
  46. if git_dirty:
  47. cli.log.warning('{fg_yellow}Git has unstashed/uncommitted changes.')
  48. status = CheckStatus.WARNING
  49. git_remotes = git_get_remotes()
  50. if 'upstream' not in git_remotes.keys() or QMK_FIRMWARE_UPSTREAM not in git_remotes['upstream'].get('url', ''):
  51. cli.log.warning('{fg_yellow}The official repository does not seem to be configured as git remote "upstream".')
  52. status = CheckStatus.WARNING
  53. else:
  54. git_deviation = git_check_deviation(git_branch)
  55. if git_branch in ['master', 'develop'] and git_deviation:
  56. cli.log.warning('{fg_yellow}The local "%s" branch contains commits not found in the upstream branch.', git_branch)
  57. status = CheckStatus.WARNING
  58. return status
  59. @cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.')
  60. @cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.')
  61. @cli.subcommand('Basic QMK environment checks')
  62. def doctor(cli):
  63. """Basic QMK environment checks.
  64. This is currently very simple, it just checks that all the expected binaries are on your system.
  65. TODO(unclaimed):
  66. * [ ] Compile a trivial program with each compiler
  67. """
  68. cli.log.info('QMK Doctor is checking your environment.')
  69. cli.log.info('CLI version: %s', cli.version)
  70. cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE)
  71. status = os_status = os_tests()
  72. git_status = git_tests()
  73. if git_status == CheckStatus.ERROR or (os_status == CheckStatus.OK and git_status == CheckStatus.WARNING):
  74. status = git_status
  75. if in_virtualenv():
  76. cli.log.info('CLI installed in virtualenv.')
  77. # Make sure the basic CLI tools we need are available and can be executed.
  78. bin_ok = check_binaries()
  79. if not bin_ok:
  80. if yesno('Would you like to install dependencies?', default=True):
  81. cli.run(['util/qmk_install.sh', '-y'], stdin=DEVNULL, capture_output=False)
  82. bin_ok = check_binaries()
  83. if bin_ok:
  84. cli.log.info('All dependencies are installed.')
  85. else:
  86. status = CheckStatus.ERROR
  87. # Make sure the tools are at the correct version
  88. ver_ok = check_binary_versions()
  89. if CheckStatus.ERROR in ver_ok:
  90. status = CheckStatus.ERROR
  91. elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK:
  92. status = CheckStatus.WARNING
  93. # Check out the QMK submodules
  94. sub_ok = check_submodules()
  95. if sub_ok == CheckStatus.OK:
  96. cli.log.info('Submodules are up to date.')
  97. else:
  98. if yesno('Would you like to clone the submodules?', default=True):
  99. submodules.update()
  100. sub_ok = check_submodules()
  101. if sub_ok == CheckStatus.ERROR:
  102. status = CheckStatus.ERROR
  103. elif sub_ok == CheckStatus.WARNING and status == CheckStatus.OK:
  104. status = CheckStatus.WARNING
  105. # Report a summary of our findings to the user
  106. if status == CheckStatus.OK:
  107. cli.log.info('{fg_green}QMK is ready to go')
  108. return 0
  109. elif status == CheckStatus.WARNING:
  110. cli.log.info('{fg_yellow}QMK is ready to go, but minor problems were found')
  111. return 1
  112. else:
  113. cli.log.info('{fg_red}Major problems detected, please fix these problems before proceeding.')
  114. 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.')
  115. return 2