forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_json.py
executable file
·32 lines (27 loc) · 987 Bytes
/
create_json.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
#!/usr/bin/env python
import sys, re, json
from os import environ, popen
from os.path import dirname, realpath
from optparse import OptionParser
parser = OptionParser()
parser.add_option('--logfile')
parser.add_option('--jsonfile')
(options, args) = parser.parse_args()
def extract_data(inputfile):
list_of_dicts = []
with open(inputfile, 'r') as file:
first_char = file.read(1)
if not first_char: print("Error: Input file is empty"), sys.exit(1)
pattern = re.compile('^([a-z]+)\+([\w-]+)\+([\w.-]+)\s\(([\w]+)\)')
matched_lines = [pattern.match(l) for l in file.readlines()]
for line in matched_lines:
if line:
list_of_dicts.append(dict(
package_type = line.group(1),
name = line.group(2),
ver_suffix = line.group(3),
hashtag = line.group(4)
))
return json.dumps(list_of_dicts, sort_keys=True, indent=2)
with open(options.jsonfile, 'w' ) as file:
file.write(extract_data(options.logfile))