annotate py/receive.py @ 507:3a3cbda62a9e

Add readme
author Matt Johnston <matt@ucc.asn.au>
date Sun, 30 Mar 2014 20:31:54 +0800
parents 905bbab632f7
children 185621f47040
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')
495
905bbab632f7 Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents: 493
diff changeset
47 name = t.name
905bbab632f7 Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents: 493
diff changeset
48 t.close()
493
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
495
905bbab632f7 Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents: 493
diff changeset
50 os.rename(name, config.PARAMS_FILE)
493
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 except Exception, e:
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 return "Problem: %s" % e
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 try:
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 pid = int(open('%s/tempserver.pid' % dir, 'r').read())
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if pid < 2:
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return "Bad pid %d" % pid
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 os.kill(pid, signal.SIGHUP)
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 except Exception, e:
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return "HUP problem: %s" % e
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 return 'Good Update'
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if __name__ == '__main__':
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 print main()
20db270cc23a working
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66