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