diff options
Diffstat (limited to 'lib/python/qmk/submodules.py')
| -rw-r--r-- | lib/python/qmk/submodules.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/python/qmk/submodules.py b/lib/python/qmk/submodules.py new file mode 100644 index 000000000..be51a6804 --- /dev/null +++ b/lib/python/qmk/submodules.py | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | """Functions for working with QMK's submodules. | ||
| 2 | """ | ||
| 3 | |||
| 4 | import subprocess | ||
| 5 | |||
| 6 | |||
| 7 | def status(): | ||
| 8 | """Returns a dictionary of submodules. | ||
| 9 | |||
| 10 | Each entry is a dict of the form: | ||
| 11 | |||
| 12 | { | ||
| 13 | 'name': 'submodule_name', | ||
| 14 | 'status': None/False/True, | ||
| 15 | 'githash': '<sha-1 hash for the submodule> | ||
| 16 | } | ||
| 17 | |||
| 18 | status is None when the submodule doesn't exist, False when it's out of date, and True when it's current | ||
| 19 | """ | ||
| 20 | submodules = {} | ||
| 21 | git_cmd = subprocess.run(['git', 'submodule', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30, universal_newlines=True) | ||
| 22 | |||
| 23 | for line in git_cmd.stdout.split('\n'): | ||
| 24 | if not line: | ||
| 25 | continue | ||
| 26 | |||
| 27 | status = line[0] | ||
| 28 | githash, submodule = line[1:].split()[:2] | ||
| 29 | submodules[submodule] = {'name': submodule, 'githash': githash} | ||
| 30 | |||
| 31 | if status == '-': | ||
| 32 | submodules[submodule]['status'] = None | ||
| 33 | elif status == '+': | ||
| 34 | submodules[submodule]['status'] = False | ||
| 35 | elif status == ' ': | ||
| 36 | submodules[submodule]['status'] = True | ||
| 37 | else: | ||
| 38 | raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status) | ||
| 39 | |||
| 40 | return submodules | ||
| 41 | |||
| 42 | |||
| 43 | def update(submodules=None): | ||
| 44 | """Update the submodules. | ||
| 45 | |||
| 46 | submodules | ||
| 47 | A string containing a single submodule or a list of submodules. | ||
| 48 | """ | ||
| 49 | git_sync_cmd = ['git', 'submodule', 'sync'] | ||
| 50 | git_update_cmd = ['git', 'submodule', 'update', '--init'] | ||
| 51 | |||
| 52 | if submodules is None: | ||
| 53 | # Update everything | ||
| 54 | git_sync_cmd.append('--recursive') | ||
| 55 | git_update_cmd.append('--recursive') | ||
| 56 | subprocess.run(git_sync_cmd, check=True) | ||
| 57 | subprocess.run(git_update_cmd, check=True) | ||
| 58 | |||
| 59 | else: | ||
| 60 | if isinstance(submodules, str): | ||
| 61 | # Update only a single submodule | ||
| 62 | git_sync_cmd.append(submodules) | ||
| 63 | git_update_cmd.append(submodules) | ||
| 64 | subprocess.run(git_sync_cmd, check=True) | ||
| 65 | subprocess.run(git_update_cmd, check=True) | ||
| 66 | |||
| 67 | else: | ||
| 68 | # Update submodules in a list | ||
| 69 | for submodule in submodules: | ||
| 70 | subprocess.run(git_sync_cmd + [submodule], check=True) | ||
| 71 | subprocess.run(git_update_cmd + [submodule], check=True) | ||
