Mercurial > templog
annotate py/receive.py @ 632:bde302def78e rust
moving to riker, nowhere near yet
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 22 Aug 2019 23:59:50 +0800 |
parents | 185621f47040 |
children |
rev | line source |
---|---|
193 | 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 | |
230 | 31 if def_params.keys() != new_params.keys(): |
32 diff = def_params.keys() ^ new_params.keys() | |
193 | 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') | |
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 | 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 | 51 except Exception as e: |
193 | 52 return "Problem: %s" % e |
53 | |
54 try: | |
55 pid = int(open('%s/tempserver.pid' % dir, 'r').read()) | |
56 if pid < 2: | |
57 return "Bad pid %d" % pid | |
58 os.kill(pid, signal.SIGHUP) | |
230 | 59 except Exception as e: |
193 | 60 return "HUP problem: %s" % e |
61 | |
62 return 'Good Update' | |
63 | |
64 if __name__ == '__main__': | |
230 | 65 print(main()) |
193 | 66 |