forked from gkkachi/gapi-grpc-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild.py
executable file
·122 lines (98 loc) · 2.95 KB
/
prebuild.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
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from functools import reduce
import subprocess
import os
from jinja2 import Environment, FileSystemLoader
def getFromDict(dataDict, mapList):
def reducer(acc, x):
if (not x in acc):
acc[x] = {}
return acc[x]
return reduce(reducer, mapList, dataDict)
def setInDict(dataDict, mapList, value):
getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value
def main():
env = Environment(
loader=FileSystemLoader('./templates/', encoding='utf8'),
trim_blocks=True,
lstrip_blocks=True)
res = subprocess.run(
'./utils/services.sh', stdout=subprocess.PIPE)
cmd_lines = res.stdout.decode('utf-8').split('\n')[:-1]
ignored = []
for p in cmd_lines:
if 'ondemandscanning' in p:
ignored.append(p)
elif 'google.ads' in p:
ignored.append(p)
elif 'google.cloud' in p:
ignored.append(p)
elif 'ccc.hosted' in p:
ignored.append(p)
elif 'spanner' in p:
ignored.append(p)
elif 'grafeas' in p:
ignored.append(p)
elif 'firestore' in p:
ignored.append(p)
elif 'google.devtools' in p:
ignored.append(p)
elif 'bigtable' in p:
ignored.append(p)
elif 'analytics' in p:
ignored.append(p)
elif 'example' in p:
ignored.append(p)
elif 'genomics' in p:
ignored.append(p)
print(ignored)
for i in ignored:
cmd_lines.remove(i)
#
# lib.rs
#
packages_list = [x.split(' ')[0] for x in cmd_lines]
mods = []
for i in range(len(packages_list) - 1):
p1 = packages_list[i]
p2 = packages_list[i + 1]
if p1 == p2:
packages_list[i] = None
if p2.startswith(p1 + '.'):
packages_list[i] = None
mods.append(p1)
ignore_packages = [
'google.firebase.fcm.connection.v1alpha1',
]
for package in ignore_packages:
packages_list.remove(package)
packages = {}
for package in packages_list:
if package is None:
continue
setInDict(packages, package.split('.'), package)
for package in mods:
package_split = package.split('.')
package_split.append('self')
setInDict(packages, package_split, package)
tpl_lib_rs = env.get_template('lib.rs.jinja')
txt_lib_rs = tpl_lib_rs.render({'packages': packages})
#
# build.rs
#
paths = [x.split(' ')[1] for x in cmd_lines]
tpl_build_rs = env.get_template('build.rs.jinja')
txt_built_rs = tpl_build_rs.render({'paths': paths})
#
# write
#
os.makedirs('src', exist_ok=True)
with open('src/lib.rs', 'w') as f:
f.write(txt_lib_rs)
with open('build.rs', 'w') as f:
f.write(txt_built_rs)
subprocess.run(['cargo', 'fmt'])
print('Success.')
if __name__ == "__main__":
main()