-
Notifications
You must be signed in to change notification settings - Fork 3
/
pkgvars.py
48 lines (40 loc) · 1.4 KB
/
pkgvars.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
#!/usr/bin/env python
import json
import fileinput
def main():
fileno = -1
files = ['', '', '']
for line in fileinput.input():
if fileinput.filelineno() == 1:
fileno += 1
files[fileno] += line
before = set(files[0].split('\x00'))
after = set(files[1].split('\x00'))
arrays = set(files[2].strip().split('\n'))
print(to_json(before, after, arrays))
def to_json(before, after, arrays):
"""Returns a valid JSON string containing functions and variables.
Parameters are three sets, in the format produced by ``pkgvars.sh`` and
this module's main() function.
"""
variables = {}
functions = {}
for line in after.difference(before):
split_at = line.find('=')
var = line[:split_at]
value = line[split_at+1:]
if value.startswith('() {'):
functions[var[10:-2]] = [line.strip('\n; ') for line in value.strip('() {}\n').split('\n')]
else:
variables[var] = value.rstrip()
for line in arrays:
split_at = line.find('=')
var = line[:split_at]
value = line[split_at+1:]
if value == '( )':
continue
value = [v.replace('\0', ' ').strip(' \'') for v in value.strip("'() \n").split(' ')]
variables[var] = value
return json.dumps({'variables': variables, 'functions': functions}, indent=4)
if __name__ == "__main__":
main()