forked from orhanobut/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_info.py
executable file
·111 lines (85 loc) · 3.56 KB
/
build_info.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
import os
import re
import logging
SETTINGS_GRADLE = 'settings.gradle'
BUILD_GRADLE = 'build.gradle'
REGEX_MODULE = '\s*include\s*[\']:(\S+)[\']'
REGEX_DEP_1 = '\s*([^\s(]+)\s*\(?[\'\"]([^\s(]+):([^\s(]+):([^\s(]+)[\'\"]'
REGEX_DEP_2 = '\s*([^\s(]+)\s*\(? group:\s*[\'\"](\S+)[\'\"]\s*,\s*name:\s*[\'\"](\S+)[\'\"]\s*,\s*version:\s*[\'\"](\S+)[\'\"]\s*'
REGEX_EXTRA_PROPS = '[^ext\.?]?(\s*\S+\s*)=\s*([^\n]*)'
REGEX_PROPS = '\s+(compileSdkVersion|buildToolsVersion)\s+\$?\'?\"?([\w\.\-]+)\'?\"?'
extra_props = {}
class BuildInfo:
@staticmethod
def fetch():
try:
build_info = {}
# root gradle info
build_info.update(process_root_module())
# module dependencies & other info e.g. compileSdkVersion, buildToolsVersion..
process_modules(build_info)
return {"build": build_info}
except:
logging.error('Constructing Build info failed')
raise
def process_root_module():
global extra_props
deps = {'deps': []}
with open(BUILD_GRADLE, 'r') as f:
reg = re.compile(REGEX_EXTRA_PROPS, re.MULTILINE)
reg_dep = re.compile(REGEX_DEP_1, re.MULTILINE)
for line in f:
# process extra properties
match = reg.match(line)
if match:
data = re.split('=', match.group().replace('ext.', '').replace(' ', '')) # todo
extra_props[data[0]] = data[1]
# process root dependencies
match = reg_dep.match(line)
if match:
deps['deps'] += [{'config': match.group(1),
'group': match.group(2),
'name': match.group(3),
'version': match.group(4)}]
return deps
def process_modules(json):
with open(SETTINGS_GRADLE) as f:
modules_found = []
for line in f:
match = re.compile(REGEX_MODULE, re.VERBOSE).match(line)
if match:
# replace include, ' and , with empty string
for ch in ['include','\'', ',']:
if ch in line:
line=line.replace(ch,'')
modules = re.findall(r'[:\S+]+', line)
modules_found.extend(modules)
for module in modules_found:
process_module(module, json)
return json
def process_module(module, json):
module = module.replace(':','/')[1:]
with open(module + os.path.join(module, '/build.gradle')) as f:
process_module_dependencies(f, module, json)
def process_module_dependencies(f, module, json):
global extra_props
regex1 = re.compile(REGEX_DEP_1, re.VERBOSE)
regex2 = re.compile(REGEX_DEP_2, re.VERBOSE)
for line in f:
match = regex1.match(line) or regex2.match(line)
if match:
version = match.group(4)
if version.startswith(("$", "rootProject")):
version = extra_props[version.split('.')[-1]].replace("\"", "")
json['deps'] += [{'config': match.group(1),
'group': match.group(2),
'name': match.group(3),
'version': version,
'module': module}]
match = re.compile(REGEX_PROPS, re.VERBOSE).match(line)
if match:
value = match.group(2)
if value.startswith(("$", "rootProject")):
value = extra_props[value.split('.')[-1]]
json.update({match.group(1): value.replace('\"', '')})