comparison py/receive.py @ 494:1800a07f20e0

merge
author Matt Johnston <matt@ucc.asn.au>
date Tue, 11 Feb 2014 23:48:35 +0800
parents 20db270cc23a
children e27075b1e088
comparison
equal deleted inserted replaced
492:23c6cf01d237 494:1800a07f20e0
1 #!/usr/bin/env python
2
3 import params
4 import json
5 import sys
6 import tempfile
7 import signal
8 import os
9
10 import config
11
12
13 def same_type(a, b):
14 ta = type(a)
15 tb = type(b)
16
17 if ta == int:
18 ta = float
19 if tb == int:
20 tb = float
21
22 return (ta == tb)
23
24 def main():
25
26 i = sys.stdin.read()
27 new_params = json.loads(i)
28
29 def_params = params.Params()
30
31 if def_params.viewkeys() != new_params.viewkeys():
32 diff = def_params.viewkeys() ^ new_params.viewkeys()
33 return "Mismatching params, %s" % str(diff)
34
35 for k, v in new_params.items():
36 if not same_type(v, def_params[k]):
37 return "Bad type for %s" % k
38
39 dir = os.path.dirname(config.PARAMS_FILE)
40
41 try:
42 t = tempfile.NamedTemporaryFile(prefix='config',
43 dir = dir,
44 delete = False)
45
46 t.write(json.dumps(new_params, sort_keys=True, indent=4)+'\n')
47
48 os.rename(t.name, config.PARAMS_FILE)
49 except Exception, e:
50 return "Problem: %s" % e
51
52 try:
53 pid = int(open('%s/tempserver.pid' % dir, 'r').read())
54 if pid < 2:
55 return "Bad pid %d" % pid
56 os.kill(pid, signal.SIGHUP)
57 except Exception, e:
58 return "HUP problem: %s" % e
59
60 return 'Good Update'
61
62 if __name__ == '__main__':
63 print main()
64