-
Notifications
You must be signed in to change notification settings - Fork 18
/
update_manifest.py
executable file
·96 lines (81 loc) · 3.13 KB
/
update_manifest.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
#!/usr/bin/env python3
import argparse
import os
import sys
from typing import Dict, Set
from generate import spec_files
def update_manifest(stone_root: str):
stone_files = spec_files(stone_root)
deps: Dict[str, Set[str]] = {}
for filepath in stone_files:
module = None
with open(filepath) as f:
for line in f:
if line.startswith('namespace '):
module = line.strip().split('namespace ')[1]
break
if module is None or module == "":
# this can happen if the stone file is empty
print('unknown namespace for ' + filepath)
continue
print(f'{filepath} = {module}')
if module == "stone_cfg":
continue
if not module in deps:
deps[module] = set()
with open(filepath) as f:
for line in f:
if line.startswith('import '):
imported = line.strip().split('import ')[1]
deps[module].add(imported)
with open('namespaces.dot', 'w') as dot:
dot.write('digraph deps {\n')
for module, imports in sorted(deps.items()):
if not imports:
dot.write(f' {module};\n')
else:
deps_list = ' '.join(sorted(imports))
dot.write(f' {module} -> {{ {deps_list} }};\n')
dot.write('}\n')
# super hacky toml reader and editor
in_features = False
in_default = False
with open("Cargo.toml", "r") as old, open("Cargo.toml.new", "w") as new:
for line in old:
if in_default:
if line.startswith(" \"dbx_"):
in_features = True
continue
else:
# found the end
# write an indented list of features
for module in sorted(deps):
new.write(f' "dbx_{module}",\n')
in_features = False
in_default = False
elif in_features:
if line.startswith("dbx_"):
continue
else:
# found the end of the features list.
# write out the new features list
for module in sorted(deps):
deps_list = ', '.join([f'"dbx_{x}"' for x in sorted(deps[module])])
new.write(f'dbx_{module} = [{deps_list}]\n')
in_features = False
else:
if line.startswith('dbx_'):
in_features = True
continue
elif line == 'default = [\n':
in_default = True
new.write(line)
os.replace("Cargo.toml.new", "Cargo.toml")
def main():
parser = argparse.ArgumentParser(description="update the namespace features in Cargo.toml")
parser.add_argument("--spec-path", type=str, default="dropbox-api-spec",
help="Path to the API spec submodule.")
args = parser.parse_args()
update_manifest(args.spec_path)
if __name__ == "__main__":
main()