-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjsonfmt.py
109 lines (89 loc) · 1.93 KB
/
jsonfmt.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" @todo add docstring """
# ### imports ###
from jsoncomment import JsonComment
# from jsonschema import validate
import json
import os
import pprint
import sys
def decode(s):
if sys.version_info >= (3, 0):
return s
for encoding in 'utf-8-sig', 'utf-16':
try:
return s.decode(encoding)
except UnicodeDecodeError:
continue
return s.decode('latin-1')
def touch(filename, mtime):
with open(filename, 'a+'):
pass
os.utime(filename, (mtime, mtime))
return 0
def add(key, old, new):
if key in old:
new[key] = old[key]
return new
file = sys.argv[1]
if file == 'schema.json':
sys.exit(0)
print('Updating %s' % file)
mtime = os.path.getmtime(file)
with open(file, 'r') as f:
jstr = f.read()
jstr_no_bom = decode(jstr)
parser = JsonComment(json)
json_data = parser.loads(jstr_no_bom)
keys = [
'##',
'_comment',
'version',
'description',
'homepage',
'license',
'notes',
'depends',
'suggest',
'cookie',
'architecture',
'hash',
'url',
'innosetup',
'extract_dir',
'extract_to',
'pre_install',
'installer',
'post_install',
'uninstaller',
'bin',
'shortcuts',
'psmodule',
'env_add_path',
'env_set',
'persist',
'checkver',
'autoupdate',
]
old_json = json_data
new_json = {}
for key in keys:
new_json = add(key, json_data, new_json)
if key in old_json:
del old_json[key]
if old_json:
pprint.pprint(old_json)
sys.exit(1)
new_data = json.dumps(
new_json, sort_keys=False, indent=4, separators=(',', ': '))
with open(file + '.tmp', 'wb') as f:
new_data = new_data.encode('utf-8')
new_data += b"\n"
f.write(new_data)
if os.path.isfile(file + '.bak'):
os.remove(file + '.bak')
os.rename(file, file + '.bak')
os.rename(file + '.tmp', file)
# touch(file, mtime)
sys.exit(0)