1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#!/usr/bin/python3
import os
import sys
import getopt
import subprocess
import signal
def check_unikid_json(infile, outfile, verbose=0):
if not os.path.exists(infile):
print("WARN: %s does not exist", infile)
try:
fi = open(infile, "r")
except IOError:
print("WARN: Unable to open %s", infile)
sys.exit(2)
lines = fi.readlines()
fi.close()
try:
fo = open(outfile, "w+")
except IOError:
print("WARN: Unable to open %s", f)
sys.exit(2)
curid = 1
refcount = 0
idlist = {}
myid = []
for myline in lines:
if "{" in myline:
refcount +=1
myid.append(curid)
curid = 1
idlist[refcount] = {}
if "}" in myline:
del idlist[refcount]
curid = myid.pop()
refcount -=1
try:
key_id, value = myline.split(":", 1)
except ValueError:
fo.write(myline)
continue
key_id = key_id.strip('\"\t\n\r ')
value = value.strip(',\"\t\n\r ')
if key_id in idlist[refcount]:
newkey_id = key_id + str(curid)
while newkey_id in idlist[refcount]:
curid +=1
newkey_id = key_id + str(curid)
if verbose:
print("level ", refcount, " : key ", key_id, " changed into ", newkey_id)
myline = myline.replace(key_id, newkey_id, 1)
key_id = newkey_id
idlist[refcount][key_id] = value
fo.write(myline)
fo.close()
return
def check_suspend_json(infile, outfile, verbose=0):
if not os.path.exists(infile):
print("WARN: %s does not exist", infile)
try:
fi = open(infile, "r")
except IOError:
print("WARN: Unable to open %s", infile)
sys.exit(2)
lines = fi.readlines()
fi.close()
try:
fo = open(outfile, "w+")
except IOError:
print("WARN: Unable to open %s", f)
sys.exit(2)
taskobj = 0
curid = ""
for myline in lines:
exception = 0
key_id = "exception"
try:
key_id, value = myline.split(":", 1)
except ValueError:
if "suspend" in myline:
key_id = "suspend"
exception = 1
key_id = key_id.strip('\"\t\n\r ')
if not "tasks" in key_id and \
taskobj == 0:
fo.write(myline)
continue
if "{" in myline:
taskobj += 1
if taskobj == 2:
curid = key_id
if "}" in myline:
taskobj -= 1
if "suspend" in key_id and \
exception == 1:
if verbose:
print("value ", curid, " added to suspend key")
if "," in myline:
myline = myline.replace(",", " : " + "\"" + curid + "\",", 1)
else:
myline = myline.replace("\n", " : " + "\"" + curid + "\"\n", 1)
fo.write(myline)
fo.close()
return
if __name__ == '__main__':
def handleSigTERM(signum, frame):
sys.exit
signal.signal(signal.SIGTERM, handleSigTERM)
signal.signal(signal.SIGINT, handleSigTERM)
outfile = "unikid.json"
selfupdate = 0
verbose = 0
dry_run = False
try:
opts, args = getopt.getopt(sys.argv[1:], "o:avd")
except getopt.GetoptError as err:
print(str(err)) # will print something like "option -a not recognized"
sys.exit(2)
for o, a in opts:
if o == "-o":
outfile = a
if o == "-a":
selfupate = 1
if o == "-v":
verbose = 1
if o == "-d":
dry_run = True
for f in args:
if selfupdate:
outfile = f
check_suspend_json(f, outfile)
check_unikid_json(outfile, outfile)
if not dry_run:
subprocess.call(["rt-app", outfile])
|