blob: db2c381b8123bd4507e9ca88d72f912875db4037 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#############################################################################
##
## Copyright (C) 2021 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
import os
import subprocess
# get the current branch of git repo
def get_branch(git_repo: str = '.') -> str:
cwd = os.getcwd()
os.chdir(git_repo)
cmd = 'git rev-parse --abbrev-ref HEAD'.split(' ')
completed_process = subprocess.run(cmd, capture_output=True)
output = completed_process.stdout.decode('utf-8').strip()
os.chdir(cwd)
return output
# Gets a list of submodules from given git repo path and optionally, branch
# Where each module is represented by a dict
def get_submodules(git_repo: str = '.', branch: str = 'current') -> {}:
cwd = os.getcwd()
os.chdir(git_repo)
current_branch = get_branch()
if branch != 'current':
os.system(f'git checkout {branch}')
modules = {}
modules_path = '.gitmodules'
if not os.path.exists(modules_path):
print(f'Error: {modules_path} not found')
return modules
read_state = 0
current_module = {}
with open(modules_path) as modules_file:
for line in modules_file:
if line.startswith('[submodule'):
if 'name' in current_module:
modules[current_module['name']] = current_module
read_state = 1
module_name = line.split(' ')[1].replace('"', '').replace(']', '').strip()
current_module = {'name': module_name}
elif read_state == 1:
if '=' in line:
elements = line.split('=')
key = elements[0].strip()
value = elements[1].strip()
current_module[key] = value
if branch != 'current':
os.system(f'git checkout {current_branch}')
os.chdir(cwd)
return modules
# Returns a list of active submodules for the given repo and branch
def get_active_submodules(git_repo: str = '.', branch: str = 'current') -> []:
modules = []
for module_name, module in get_submodules(git_repo, branch).items():
if module['status'] != 'ignore':
modules.append(module_name)
return modules
|