annotate py/receive.py @ 493:20db270cc23a

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