# HG changeset patch # User Matt Johnston # Date 1392133683 -28800 # Node ID 063ff856f9c5fcfde2b3e6a5b97ce5ea165b3725 # Parent e13146001852d4264c1a39b7985e8fcbd73a9191 working diff -r e13146001852 -r 063ff856f9c5 py/receive.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/py/receive.py Tue Feb 11 23:48:03 2014 +0800 @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +import params +import json +import sys +import tempfile +import signal +import os + +import config + + +def same_type(a, b): + ta = type(a) + tb = type(b) + + if ta == int: + ta = float + if tb == int: + tb = float + + return (ta == tb) + +def main(): + + i = sys.stdin.read() + new_params = json.loads(i) + + def_params = params.Params() + + if def_params.viewkeys() != new_params.viewkeys(): + diff = def_params.viewkeys() ^ new_params.viewkeys() + return "Mismatching params, %s" % str(diff) + + for k, v in new_params.items(): + if not same_type(v, def_params[k]): + return "Bad type for %s" % k + + dir = os.path.dirname(config.PARAMS_FILE) + + try: + t = tempfile.NamedTemporaryFile(prefix='config', + dir = dir, + delete = False) + + t.write(json.dumps(new_params, sort_keys=True, indent=4)+'\n') + + os.rename(t.name, config.PARAMS_FILE) + except Exception, e: + return "Problem: %s" % e + + try: + pid = int(open('%s/tempserver.pid' % dir, 'r').read()) + if pid < 2: + return "Bad pid %d" % pid + os.kill(pid, signal.SIGHUP) + except Exception, e: + return "HUP problem: %s" % e + + return 'Good Update' + +if __name__ == '__main__': + print main() +