diff options
Diffstat (limited to 'lib/python/qmk/commands.py')
-rw-r--r-- | lib/python/qmk/commands.py | 133 |
1 files changed, 108 insertions, 25 deletions
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 3a35c1103..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' |
@@ -86,11 +87,17 @@ def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars): | |||
86 | return create_make_target(':'.join(make_args), parallel, **env_vars) | 87 | return create_make_target(':'.join(make_args), parallel, **env_vars) |
87 | 88 | ||
88 | 89 | ||
89 | def get_git_version(repo_dir='.', check_dir='.'): | 90 | def get_git_version(current_time, repo_dir='.', check_dir='.'): |
90 | """Returns the current git version for a repo, or the current time. | 91 | """Returns the current git version for a repo, or the current time. |
91 | """ | 92 | """ |
92 | git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] | 93 | git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] |
93 | 94 | ||
95 | if repo_dir != '.': | ||
96 | repo_dir = Path('lib') / repo_dir | ||
97 | |||
98 | if check_dir != '.': | ||
99 | check_dir = repo_dir / check_dir | ||
100 | |||
94 | if Path(check_dir).exists(): | 101 | if Path(check_dir).exists(): |
95 | git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir) | 102 | git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir) |
96 | 103 | ||
@@ -100,23 +107,40 @@ def get_git_version(repo_dir='.', check_dir='.'): | |||
100 | else: | 107 | else: |
101 | cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}') | 108 | cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}') |
102 | print(git_describe.stderr) | 109 | print(git_describe.stderr) |
103 | return strftime(time_fmt) | 110 | return current_time |
104 | 111 | ||
105 | return strftime(time_fmt) | 112 | return current_time |
106 | 113 | ||
107 | 114 | ||
108 | def write_version_h(git_version, build_date, chibios_version, chibios_contrib_version): | 115 | def create_version_h(skip_git=False, skip_all=False): |
109 | """Generate and write quantum/version.h | 116 | """Generate version.h contents |
110 | """ | 117 | """ |
111 | version_h = [ | 118 | if skip_all: |
112 | f'#define QMK_VERSION "{git_version}"', | 119 | current_time = "1970-01-01-00:00:00" |
113 | f'#define QMK_BUILDDATE "{build_date}"', | 120 | else: |
114 | f'#define CHIBIOS_VERSION "{chibios_version}"', | 121 | current_time = strftime(time_fmt) |
115 | f'#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"', | 122 | |
116 | ] | 123 | if skip_git: |
124 | git_version = "NA" | ||
125 | chibios_version = "NA" | ||
126 | chibios_contrib_version = "NA" | ||
127 | else: | ||
128 | git_version = get_git_version(current_time) | ||
129 | chibios_version = get_git_version(current_time, "chibios", "os") | ||
130 | chibios_contrib_version = get_git_version(current_time, "chibios-contrib", "os") | ||
131 | |||
132 | version_h_lines = f"""/* This file was automatically generated. Do not edit or copy. | ||
133 | */ | ||
134 | |||
135 | #pragma once | ||
136 | |||
137 | #define QMK_VERSION "{git_version}" | ||
138 | #define QMK_BUILDDATE "{current_time}" | ||
139 | #define CHIBIOS_VERSION "{chibios_version}" | ||
140 | #define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}" | ||
141 | """ | ||
117 | 142 | ||
118 | version_h_file = Path('quantum/version.h') | 143 | return version_h_lines |
119 | version_h_file.write_text('\n'.join(version_h)) | ||
120 | 144 | ||
121 | 145 | ||
122 | def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars): | 146 | def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars): |
@@ -149,13 +173,8 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va | |||
149 | keymap_dir.mkdir(exist_ok=True, parents=True) | 173 | keymap_dir.mkdir(exist_ok=True, parents=True) |
150 | keymap_c.write_text(c_text) | 174 | keymap_c.write_text(c_text) |
151 | 175 | ||
152 | # Write the version.h file | 176 | version_h = Path('quantum/version.h') |
153 | git_version = get_git_version() | 177 | version_h.write_text(create_version_h()) |
154 | build_date = strftime('%Y-%m-%d-%H:%M:%S') | ||
155 | chibios_version = get_git_version("lib/chibios", "lib/chibios/os") | ||
156 | chibios_contrib_version = get_git_version("lib/chibios-contrib", "lib/chibios-contrib/os") | ||
157 | |||
158 | write_version_h(git_version, build_date, chibios_version, chibios_contrib_version) | ||
159 | 178 | ||
160 | # Return a command that can be run to make the keymap and flash if given | 179 | # Return a command that can be run to make the keymap and flash if given |
161 | verbose = 'true' if cli.config.general.verbose else 'false' | 180 | verbose = 'true' if cli.config.general.verbose else 'false' |
@@ -181,10 +200,6 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va | |||
181 | make_command.append(f'{key}={value}') | 200 | make_command.append(f'{key}={value}') |
182 | 201 | ||
183 | make_command.extend([ | 202 | make_command.extend([ |
184 | f'GIT_VERSION={git_version}', | ||
185 | f'BUILD_DATE={build_date}', | ||
186 | f'CHIBIOS_VERSION={chibios_version}', | ||
187 | f'CHIBIOS_CONTRIB_VERSION={chibios_contrib_version}', | ||
188 | f'KEYBOARD={user_keymap["keyboard"]}', | 203 | f'KEYBOARD={user_keymap["keyboard"]}', |
189 | f'KEYMAP={user_keymap["keymap"]}', | 204 | f'KEYMAP={user_keymap["keymap"]}', |
190 | f'KEYBOARD_FILESAFE={keyboard_filesafe}', | 205 | f'KEYBOARD_FILESAFE={keyboard_filesafe}', |
@@ -223,3 +238,71 @@ def parse_configurator_json(configurator_file): | |||
223 | user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] | 238 | user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] |
224 | 239 | ||
225 | 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 | ||