annotate py/receive.py @ 607:282fae1c12e4 rust

NotTooOften
author Matt Johnston <matt@ucc.asn.au>
date Fri, 17 Feb 2017 22:27:44 +0800
parents 185621f47040
children
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
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 196
diff changeset
31 if def_params.keys() != new_params.keys():
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 196
diff changeset
32 diff = def_params.keys() ^ new_params.keys()
193
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')
196
e27075b1e088 Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents: 193
diff changeset
47 name = t.name
e27075b1e088 Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents: 193
diff changeset
48 t.close()
193
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
196
e27075b1e088 Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents: 193
diff changeset
50 os.rename(name, config.PARAMS_FILE)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 196
diff changeset
51 except Exception as e:
193
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 return "Problem: %s" % e
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 try:
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 pid = int(open('%s/tempserver.pid' % dir, 'r').read())
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if pid < 2:
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return "Bad pid %d" % pid
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 os.kill(pid, signal.SIGHUP)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 196
diff changeset
59 except Exception as e:
193
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return "HUP problem: %s" % e
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 return 'Good Update'
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if __name__ == '__main__':
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 196
diff changeset
65 print(main())
193
063ff856f9c5 working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66