main.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_tests()
  68. status = git_tests()
  69. venv = in_virtualenv()
  70. if venv:
  71. cli.log.info('CLI installed in virtualenv.')
  72. # Make sure the basic CLI tools we need are available and can be executed.
  73. bin_ok = check_binaries()
  74. if not bin_ok:
  75. if yesno('Would you like to install dependencies?', default=True):
  76. cli.run(['util/qmk_install.sh', '-y'], stdin=DEVNULL, capture_output=False)
  77. bin_ok = check_binaries()
  78. if bin_ok:
  79. cli.log.info('All dependencies are installed.')
  80. else:
  81. status = CheckStatus.ERROR
  82. # Make sure the tools are at the correct version
  83. ver_ok = check_binary_versions()
  84. if CheckStatus.ERROR in ver_ok:
  85. status = CheckStatus.ERROR
  86. elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK:
  87. status = CheckStatus.WARNING
  88. # Check out the QMK submodules
  89. sub_ok = check_submodules()
  90. if sub_ok == CheckStatus.OK:
  91. cli.log.info('Submodules are up to date.')
  92. else:
  93. if yesno('Would you like to clone the submodules?', default=True):
  94. submodules.update()
  95. sub_ok = check_submodules()
  96. if sub_ok == CheckStatus.ERROR:
  97. status = CheckStatus.ERROR
  98. elif sub_ok == CheckStatus.WARNING and status == CheckStatus.OK:
  99. status = CheckStatus.WARNING
  100. # Report a summary of our findings to the user
  101. if status == CheckStatus.OK:
  102. cli.log.info('{fg_green}QMK is ready to go')
  103. return 0
  104. elif status == CheckStatus.WARNING:
  105. cli.log.info('{fg_yellow}QMK is ready to go, but minor problems were found')
  106. return 1
  107. else:
  108. cli.log.info('{fg_red}Major problems detected, please fix these problems before proceeding.')
  109. 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.')
  110. return 2