diff options
Diffstat (limited to 'lib/python/qmk/commands.py')
-rw-r--r-- | lib/python/qmk/commands.py | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 104c87545..8ff8501bf 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py | |||
@@ -2,6 +2,7 @@ | |||
2 | """ | 2 | """ |
3 | import json | 3 | import json |
4 | import os | 4 | import os |
5 | import sys | ||
5 | import shutil | 6 | import shutil |
6 | from pathlib import Path | 7 | from pathlib import Path |
7 | from subprocess import DEVNULL | 8 | from subprocess import DEVNULL |
@@ -10,7 +11,7 @@ from time import strftime | |||
10 | from milc import cli | 11 | from milc import cli |
11 | 12 | ||
12 | import qmk.keymap | 13 | import qmk.keymap |
13 | from qmk.constants import KEYBOARD_OUTPUT_PREFIX | 14 | from qmk.constants import QMK_FIRMWARE, KEYBOARD_OUTPUT_PREFIX |
14 | from qmk.json_schema import json_load | 15 | from qmk.json_schema import json_load |
15 | 16 | ||
16 | time_fmt = '%Y-%m-%d-%H:%M:%S' | 17 | time_fmt = '%Y-%m-%d-%H:%M:%S' |
@@ -237,3 +238,71 @@ def parse_configurator_json(configurator_file): | |||
237 | user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] | 238 | user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] |
238 | 239 | ||
239 | return user_keymap | 240 | return user_keymap |
241 | |||
242 | |||
243 | def git_check_repo(): | ||
244 | """Checks that the .git directory exists inside QMK_HOME. | ||
245 | |||
246 | This is a decent enough indicator that the qmk_firmware directory is a | ||
247 | proper Git repository, rather than a .zip download from GitHub. | ||
248 | """ | ||
249 | dot_git_dir = QMK_FIRMWARE / '.git' | ||
250 | |||
251 | return dot_git_dir.is_dir() | ||
252 | |||
253 | |||
254 | def git_get_branch(): | ||
255 | """Returns the current branch for a repo, or None. | ||
256 | """ | ||
257 | git_branch = cli.run(['git', 'branch', '--show-current']) | ||
258 | if not git_branch.returncode != 0 or not git_branch.stdout: | ||
259 | # Workaround for Git pre-2.22 | ||
260 | git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) | ||
261 | |||
262 | if git_branch.returncode == 0: | ||
263 | return git_branch.stdout.strip() | ||
264 | |||
265 | |||
266 | def git_is_dirty(): | ||
267 | """Returns 1 if repo is dirty, or 0 if clean | ||
268 | """ | ||
269 | git_diff_staged_cmd = ['git', 'diff', '--quiet'] | ||
270 | git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached'] | ||
271 | |||
272 | unstaged = cli.run(git_diff_staged_cmd) | ||
273 | staged = cli.run(git_diff_unstaged_cmd) | ||
274 | |||
275 | return unstaged.returncode != 0 or staged.returncode != 0 | ||
276 | |||
277 | |||
278 | def git_get_remotes(): | ||
279 | """Returns the current remotes for a repo. | ||
280 | """ | ||
281 | remotes = {} | ||
282 | |||
283 | git_remote_show_cmd = ['git', 'remote', 'show'] | ||
284 | git_remote_get_cmd = ['git', 'remote', 'get-url'] | ||
285 | |||
286 | git_remote_show = cli.run(git_remote_show_cmd) | ||
287 | if git_remote_show.returncode == 0: | ||
288 | for name in git_remote_show.stdout.splitlines(): | ||
289 | git_remote_name = cli.run([*git_remote_get_cmd, name]) | ||
290 | remotes[name.strip()] = {"url": git_remote_name.stdout.strip()} | ||
291 | |||
292 | return remotes | ||
293 | |||
294 | |||
295 | def git_check_deviation(active_branch): | ||
296 | """Return True if branch has custom commits | ||
297 | """ | ||
298 | cli.run(['git', 'fetch', 'upstream', active_branch]) | ||
299 | deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}']) | ||
300 | return bool(deviations.returncode) | ||
301 | |||
302 | |||
303 | def in_virtualenv(): | ||
304 | """Check if running inside a virtualenv. | ||
305 | Based on https://stackoverflow.com/a/1883251 | ||
306 | """ | ||
307 | active_prefix = getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix | ||
308 | return active_prefix != sys.prefix | ||