-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgrade.py
executable file
·108 lines (92 loc) · 4.85 KB
/
upgrade.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
#!/usr/bin/env python3
import argparse
import json
import os
from lib import core as cl
def getArgs():
parser = argparse.ArgumentParser(description="")
parser.add_argument('-p', dest="path", type=str, default='test/datarmor/', help='Path to properties files')
arg = parser.parse_args()
return arg
def extract_properties(jsons):
tools_properties = {}
for f in sorted(jsons):
print(f)
bio_json = json.load(open(f))
tool_name = os.path.dirname(f).split(os.sep)[-2]
if tool_name not in tools_properties:
tools_properties[tool_name] = {'path': os.path.join(args.path, tool_name),
'properties': {'name': bio_json['NAME'],
'bio.tools_id': bio_json['NAME'],
'description': bio_json['DESCRIPTION'],
'homepage': bio_json['URLDOC'],
"operation": bio_json['KEYWORDS'],
"topic": bio_json['TOPIC'],
'version': {
bio_json['VERSION']: {
"localInstallUser": bio_json['OWNER'],
"environment": bio_json['CMD_INSTALL'],
"localInstallDate": bio_json['DATE_INSTALL'],
"isCmdline": bio_json['CMDLINE'],
"isGalaxy": bio_json['GALAXY'],
"isWorkflow": "false",
"status": "active"
}
}}}
else:
tools_properties[tool_name]['properties']['version'][bio_json['VERSION']] = {
"localInstallUser": bio_json['OWNER'],
"environment": bio_json['CMD_INSTALL'],
"localInstallDate": bio_json['DATE_INSTALL'],
"isCmdline": bio_json['CMDLINE'],
"isGalaxy": bio_json['GALAXY'],
"isWorkflow": "false",
"status": "active"
}
return tools_properties
def update_from_biotools(properties):
for tool_id, tool_property in properties.items():
try:
print(f"Try to update {tool_property['properties']['name']}")
response, code = cl.biotools_api_request(tool_property['properties']['name'])
if code == 404:
tool_property['properties']['bio.tools_id'] = ''
print(f"Enable to find {tool_property['properties']['name']} in Bio.tools")
else:
bio_update = json.loads(response)
operation = []
for i in bio_update['function']:
operation.append(i['operation'][0]['term'].lower().replace(' ', '-'))
operations = ','.join(list(set(operation)))
topic = []
for i in bio_update['topic']:
topic.append(i['term'])
topics = ','.join(list(set(topic)))
tool_property['properties']['description'] = bio_update['description'].split('\n')[0]
tool_property['properties']['homepage'] = bio_update['homepage']
tool_property['properties']['operation'] = operations
tool_property['properties']['topic'] = topics
print(f"Successfully update {tool_property['properties']['name']}")
except:
print(f"ERROR: Enable to update {tool_property['properties']['name']} for unknow reason")
pass
return properties
def export_new_json_properties(properties):
for tool_id in properties.keys():
with open(os.path.join(properties[tool_id]['path'], 'properties.v2.json'), 'w') as out_json:
json.dump(properties[tool_id]['properties'], out_json, sort_keys=False, indent=2)
out_json.close()
def main(args):
print('Explorer path')
directories = cl.walk_level(args.path)
print('Collect propertie files')
jsons = cl.get_json(directories)
print('Update json to new format')
old_properties = extract_properties(jsons)
print('Update tool metadata if possible')
new_properties = update_from_biotools(old_properties)
print('Write new propertie in the new format')
export_new_json_properties(new_properties)
if __name__ == '__main__':
args = getArgs()
main(args)