From e87d23164522371b0c9560e81f36ed08caadc0ff Mon Sep 17 00:00:00 2001 From: Zach White Date: Tue, 22 Jun 2021 11:50:53 -0700 Subject: Refactor doctor.py into a directory (#13298) --- lib/python/qmk/cli/doctor/main.py | 103 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 lib/python/qmk/cli/doctor/main.py (limited to 'lib/python/qmk/cli/doctor/main.py') diff --git a/lib/python/qmk/cli/doctor/main.py b/lib/python/qmk/cli/doctor/main.py new file mode 100755 index 0000000000..45b0203b6e --- /dev/null +++ b/lib/python/qmk/cli/doctor/main.py @@ -0,0 +1,103 @@ +"""QMK Doctor + +Check out the user's QMK environment and make sure it's ready to compile. +""" +import platform +from subprocess import DEVNULL + +from milc import cli +from milc.questions import yesno + +from qmk import submodules +from qmk.constants import QMK_FIRMWARE +from .check import CheckStatus, check_binaries, check_binary_versions, check_submodules, check_git_repo + + +def os_tests(): + """Determine our OS and run platform specific tests + """ + platform_id = platform.platform().lower() + + if 'darwin' in platform_id or 'macos' in platform_id: + from .macos import os_test_macos + return os_test_macos() + elif 'linux' in platform_id: + from .linux import os_test_linux + return os_test_linux() + elif 'windows' in platform_id: + from .windows import os_test_windows + return os_test_windows() + else: + cli.log.warning('Unsupported OS detected: %s', platform_id) + return CheckStatus.WARNING + + +@cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.') +@cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.') +@cli.subcommand('Basic QMK environment checks') +def doctor(cli): + """Basic QMK environment checks. + + This is currently very simple, it just checks that all the expected binaries are on your system. + + TODO(unclaimed): + * [ ] Compile a trivial program with each compiler + """ + cli.log.info('QMK Doctor is checking your environment.') + cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE) + + status = os_tests() + + # Make sure our QMK home is a Git repo + git_ok = check_git_repo() + + if git_ok == CheckStatus.WARNING: + cli.log.warning("QMK home does not appear to be a Git repository! (no .git folder)") + status = CheckStatus.WARNING + + # Make sure the basic CLI tools we need are available and can be executed. + bin_ok = check_binaries() + + if not bin_ok: + if yesno('Would you like to install dependencies?', default=True): + cli.run(['util/qmk_install.sh', '-y'], stdin=DEVNULL, capture_output=False) + bin_ok = check_binaries() + + if bin_ok: + cli.log.info('All dependencies are installed.') + else: + status = CheckStatus.ERROR + + # Make sure the tools are at the correct version + ver_ok = check_binary_versions() + if CheckStatus.ERROR in ver_ok: + status = CheckStatus.ERROR + elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK: + status = CheckStatus.WARNING + + # Check out the QMK submodules + sub_ok = check_submodules() + + if sub_ok == CheckStatus.OK: + cli.log.info('Submodules are up to date.') + else: + if yesno('Would you like to clone the submodules?', default=True): + submodules.update() + sub_ok = check_submodules() + + if sub_ok == CheckStatus.ERROR: + status = CheckStatus.ERROR + elif sub_ok == CheckStatus.WARNING and status == CheckStatus.OK: + status = CheckStatus.WARNING + + # Report a summary of our findings to the user + if status == CheckStatus.OK: + cli.log.info('{fg_green}QMK is ready to go') + return 0 + elif status == CheckStatus.WARNING: + cli.log.info('{fg_yellow}QMK is ready to go, but minor problems were found') + return 1 + else: + cli.log.info('{fg_red}Major problems detected, please fix these problems before proceeding.') + 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.') + return 2 -- cgit v1.2.3 From e05f9c4a08cef384989cd20ebe27b66622912055 Mon Sep 17 00:00:00 2001 From: Erovia Date: Sat, 10 Jul 2021 16:04:50 +0100 Subject: CLI: Add git and venv info to doctor's output (#13405) Most of the checks are saved from zvecr's retired 'up/status' subcommand PR. --- lib/python/qmk/cli/doctor/main.py | 45 +++++++++++++++++++++---- lib/python/qmk/commands.py | 71 ++++++++++++++++++++++++++++++++++++++- lib/python/qmk/constants.py | 3 ++ 3 files changed, 111 insertions(+), 8 deletions(-) (limited to 'lib/python/qmk/cli/doctor/main.py') diff --git a/lib/python/qmk/cli/doctor/main.py b/lib/python/qmk/cli/doctor/main.py index 5e93fad367..6a31ccdfdd 100755 --- a/lib/python/qmk/cli/doctor/main.py +++ b/lib/python/qmk/cli/doctor/main.py @@ -9,8 +9,9 @@ from milc import cli from milc.questions import yesno from qmk import submodules -from qmk.constants import QMK_FIRMWARE -from .check import CheckStatus, check_binaries, check_binary_versions, check_submodules, check_git_repo +from qmk.constants import QMK_FIRMWARE, QMK_FIRMWARE_UPSTREAM +from .check import CheckStatus, check_binaries, check_binary_versions, check_submodules +from qmk.commands import git_check_repo, git_get_branch, git_is_dirty, git_get_remotes, git_check_deviation, in_virtualenv def os_tests(): @@ -32,6 +33,37 @@ def os_tests(): return CheckStatus.WARNING +def git_tests(): + """Run Git-related checks + """ + status = CheckStatus.OK + + # Make sure our QMK home is a Git repo + git_ok = git_check_repo() + if not git_ok: + cli.log.warning("{fg_yellow}QMK home does not appear to be a Git repository! (no .git folder)") + status = CheckStatus.WARNING + else: + git_branch = git_get_branch() + if git_branch: + cli.log.info('Git branch: %s', git_branch) + git_dirty = git_is_dirty() + if git_dirty: + cli.log.warning('{fg_yellow}Git has unstashed/uncommitted changes.') + status = CheckStatus.WARNING + git_remotes = git_get_remotes() + if 'upstream' not in git_remotes.keys() or QMK_FIRMWARE_UPSTREAM not in git_remotes['upstream'].get('url', ''): + cli.log.warning('{fg_yellow}The official repository does not seem to be configured as git remote "upstream".') + status = CheckStatus.WARNING + else: + git_deviation = git_check_deviation(git_branch) + if git_branch in ['master', 'develop'] and git_deviation: + cli.log.warning('{fg_yellow}The local "%s" branch contains commits not found in the upstream branch.', git_branch) + status = CheckStatus.WARNING + + return status + + @cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.') @cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.') @cli.subcommand('Basic QMK environment checks') @@ -49,12 +81,11 @@ def doctor(cli): status = os_tests() - # Make sure our QMK home is a Git repo - git_ok = check_git_repo() + status = git_tests() - if git_ok == CheckStatus.WARNING: - cli.log.warning("QMK home does not appear to be a Git repository! (no .git folder)") - status = CheckStatus.WARNING + venv = in_virtualenv() + if venv: + cli.log.info('CLI installed in virtualenv.') # Make sure the basic CLI tools we need are available and can be executed. bin_ok = check_binaries() diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 104c875457..8ff8501bf6 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -2,6 +2,7 @@ """ import json import os +import sys import shutil from pathlib import Path from subprocess import DEVNULL @@ -10,7 +11,7 @@ from time import strftime from milc import cli import qmk.keymap -from qmk.constants import KEYBOARD_OUTPUT_PREFIX +from qmk.constants import QMK_FIRMWARE, KEYBOARD_OUTPUT_PREFIX from qmk.json_schema import json_load time_fmt = '%Y-%m-%d-%H:%M:%S' @@ -237,3 +238,71 @@ def parse_configurator_json(configurator_file): user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] return user_keymap + + +def git_check_repo(): + """Checks that the .git directory exists inside QMK_HOME. + + This is a decent enough indicator that the qmk_firmware directory is a + proper Git repository, rather than a .zip download from GitHub. + """ + dot_git_dir = QMK_FIRMWARE / '.git' + + return dot_git_dir.is_dir() + + +def git_get_branch(): + """Returns the current branch for a repo, or None. + """ + git_branch = cli.run(['git', 'branch', '--show-current']) + if not git_branch.returncode != 0 or not git_branch.stdout: + # Workaround for Git pre-2.22 + git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) + + if git_branch.returncode == 0: + return git_branch.stdout.strip() + + +def git_is_dirty(): + """Returns 1 if repo is dirty, or 0 if clean + """ + git_diff_staged_cmd = ['git', 'diff', '--quiet'] + git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached'] + + unstaged = cli.run(git_diff_staged_cmd) + staged = cli.run(git_diff_unstaged_cmd) + + return unstaged.returncode != 0 or staged.returncode != 0 + + +def git_get_remotes(): + """Returns the current remotes for a repo. + """ + remotes = {} + + git_remote_show_cmd = ['git', 'remote', 'show'] + git_remote_get_cmd = ['git', 'remote', 'get-url'] + + git_remote_show = cli.run(git_remote_show_cmd) + if git_remote_show.returncode == 0: + for name in git_remote_show.stdout.splitlines(): + git_remote_name = cli.run([*git_remote_get_cmd, name]) + remotes[name.strip()] = {"url": git_remote_name.stdout.strip()} + + return remotes + + +def git_check_deviation(active_branch): + """Return True if branch has custom commits + """ + cli.run(['git', 'fetch', 'upstream', active_branch]) + deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}']) + return bool(deviations.returncode) + + +def in_virtualenv(): + """Check if running inside a virtualenv. + Based on https://stackoverflow.com/a/1883251 + """ + active_prefix = getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix + return active_prefix != sys.prefix diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py index be5ce5d4ed..1078f4ad5e 100644 --- a/lib/python/qmk/constants.py +++ b/lib/python/qmk/constants.py @@ -6,6 +6,9 @@ from pathlib import Path # The root of the qmk_firmware tree. QMK_FIRMWARE = Path.cwd() +# Upstream repo url +QMK_FIRMWARE_UPSTREAM = 'qmk/qmk_firmware' + # This is the number of directories under `qmk_firmware/keyboards` that will be traversed. This is currently a limitation of our make system. MAX_KEYBOARD_SUBFOLDERS = 5 -- cgit v1.2.3