-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadJsonStatusFile.py
74 lines (59 loc) · 1.73 KB
/
uploadJsonStatusFile.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
import yaml
import json
import sys
import jsonschema
from jsonschema import SchemaError, ValidationError
from os import popen
def main(config="config.yaml"):
"""
Create a json file from 3 yaml files, validate it against a JSON schema, and scp it to a server if it has changed.
:param config: A yaml config file
:return: None
"""
c=readYaml(config)
d=readYaml(c['notifications_file'])
d2=readYaml(c['resources_file'])
d3=readYaml(c['status_file'])
d.update(d2)
d.update(d3)
schema = readJson(c['schema_file'])
try:
jsonschema.validate(d,schema)
except SchemaError as e:
print("Schema has errors: {}".format(e))
return
except ValidationError as e2:
print("Validation error: {}".format(e2))
return
j = json.dumps(d)
try:
currentJson = readFile(c['json_file'])
except FileNotFoundError:
currentJson = "{}"
if currentJson != j:
writeFile(c['json_file'], j)
status=popen(fmt("echo scp {} {}:{}/{}", c['json_file'], c['host'], c['path'], c['remote_json_file']))
print(list(status))
else:
print("Json file '{}' is unchanged; no need to remotely update".format(c['json_file']))
def printf(str, *args, **kw):
print(fmt(str,*args,**kw))
def fmt(str, *args, **kw):
return str.format(*args, **kw)
def writeFile(name, data):
with open(name,"w") as h:
h.write(data)
def readYaml(name):
with open(name) as h:
return yaml.load(h)
def readJson(name):
with open(name) as h:
return json.load(h)
def readFile(name):
with open(name) as h:
return h.read()
if __name__ == "__main__":
if len(sys.argv) < 2:
main()
else:
main(sys.argv[1])