annotate py/receive.py @ 193:063ff856f9c5

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