|
| 1 | +import time |
| 2 | +import os |
| 3 | +from ast import literal_eval |
| 4 | +import sys |
| 5 | +import re |
| 6 | + |
| 7 | +#if windows, create script to kill this process |
| 8 | +# because batch files don't provide easy way to know pid of last command |
| 9 | +# ignored for posix!=windows, because "concorepid" is handled by script |
| 10 | +# ignored for docker (linux!=windows), because handled by docker stop |
| 11 | +if hasattr(sys, 'getwindowsversion'): |
| 12 | + with open("concorekill.bat","w") as fpid: |
| 13 | + fpid.write("taskkill /F /PID "+str(os.getpid())+"\n") |
| 14 | + |
| 15 | +try: |
| 16 | + iport = literal_eval(open("concore.iport").read()) |
| 17 | +except: |
| 18 | + iport = dict() |
| 19 | +try: |
| 20 | + oport = literal_eval(open("concore.oport").read()) |
| 21 | +except: |
| 22 | + oport = dict() |
| 23 | + |
| 24 | + |
| 25 | +s = '' |
| 26 | +olds = '' |
| 27 | +delay = 1 |
| 28 | +retrycount = 0 |
| 29 | +inpath = "./in" #must be rel path for local |
| 30 | +outpath = "./out" |
| 31 | + |
| 32 | +#9/21/22 |
| 33 | +try: |
| 34 | + sparams = open(inpath+"1/concore.params").read() |
| 35 | + if sparams[0] == '"': #windows keeps "" need to remove |
| 36 | + sparams = sparams[1:] |
| 37 | + sparams = sparams[0:sparams.find('"')] |
| 38 | + if sparams != '{': |
| 39 | + print("converting sparams: "+sparams) |
| 40 | + sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}" |
| 41 | + print("converted sparams: " + sparams) |
| 42 | + try: |
| 43 | + params = literal_eval(sparams) |
| 44 | + except: |
| 45 | + print("bad params: "+sparams) |
| 46 | +except: |
| 47 | + params = dict() |
| 48 | +#9/30/22 |
| 49 | +def tryparam(n,i): |
| 50 | + try: |
| 51 | + return params[n] |
| 52 | + except: |
| 53 | + return i |
| 54 | + |
| 55 | + |
| 56 | +#9/12/21 |
| 57 | +def default_maxtime(default): |
| 58 | + global maxtime |
| 59 | + try: |
| 60 | + maxtime = literal_eval(open(inpath+"1/concore.maxtime").read()) |
| 61 | + except: |
| 62 | + maxtime = default |
| 63 | +default_maxtime(100) |
| 64 | + |
| 65 | +def unchanged(): |
| 66 | + global olds,s |
| 67 | + if olds==s: |
| 68 | + s = '' |
| 69 | + return True |
| 70 | + else: |
| 71 | + olds = s |
| 72 | + return False |
| 73 | + |
| 74 | +def read(port, name, initstr): |
| 75 | + global s,simtime,retrycount |
| 76 | + time.sleep(delay) |
| 77 | + try: |
| 78 | + infile = open(inpath+str(port)+"/"+name); |
| 79 | + ins = infile.read() |
| 80 | + except: |
| 81 | + ins = initstr |
| 82 | + while len(ins)==0: |
| 83 | + time.sleep(delay) |
| 84 | + ins = infile.read() |
| 85 | + retrycount += 1 |
| 86 | + s += ins |
| 87 | + inval = literal_eval(ins) |
| 88 | + simtime = max(simtime,inval[0]) |
| 89 | + return inval[1:] |
| 90 | + |
| 91 | +def write(port, name, val, delta=0): |
| 92 | + global outpath,simtime |
| 93 | + if isinstance(val,str): |
| 94 | + time.sleep(2*delay) |
| 95 | + elif isinstance(val,list)==False: |
| 96 | + print("mywrite must have list or str") |
| 97 | + quit() |
| 98 | + try: |
| 99 | + with open(outpath+str(port)+"/"+name,"w") as outfile: |
| 100 | + if isinstance(val,list): |
| 101 | + outfile.write(str([simtime+delta]+val)) |
| 102 | + simtime += delta |
| 103 | + else: |
| 104 | + outfile.write(val) |
| 105 | + except: |
| 106 | + print("skipping"+outpath+str(port)+"/"+name); |
| 107 | + |
| 108 | +def initval(simtime_val): |
| 109 | + global simtime |
| 110 | + val = literal_eval(simtime_val) |
| 111 | + simtime = val[0] |
| 112 | + return val[1:] |
| 113 | + |
0 commit comments