forked from undera/jmeter-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_repo.py
More file actions
55 lines (43 loc) · 1.43 KB
/
format_repo.py
File metadata and controls
55 lines (43 loc) · 1.43 KB
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
#!/usr/bin/env python3
"""Format JSON files in site/dat/repo/ to canonical style (2-space indent)."""
import json
import os
import sys
repo_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "site", "dat", "repo")
def canonical(text):
return json.dumps(json.loads(text), indent=2, ensure_ascii=False) + "\n"
def format_file(path):
with open(path) as f:
raw = f.read()
formatted = canonical(raw)
if raw != formatted:
with open(path, "w") as f:
f.write(formatted)
print("Formatted %s" % path)
def repo_files():
for fname in sorted(os.listdir(repo_dir)):
if fname != "repo.json" and fname.endswith(".json"):
yield os.path.join(repo_dir, fname)
def check():
base = os.path.dirname(os.path.abspath(__file__))
bad = []
for path in repo_files():
with open(path) as f:
raw = f.read()
if raw != canonical(raw):
bad.append(os.path.relpath(path, base))
if bad:
for path in bad:
print("Not formatted: %s" % path)
print("Run 'python3 format_repo.py %s' to fix." % " ".join(bad))
sys.exit(1)
print("All repo JSON files are properly formatted.")
if __name__ == "__main__":
if "--check" in sys.argv:
check()
elif len(sys.argv) > 1:
for path in sys.argv[1:]:
format_file(path)
else:
for path in repo_files():
format_file(path)