def PythonRunner(Prog: str, In: str) -> str: """Run a program against input given :param Prog: Program to run :param In: Input data file name :return: Actual output file name or Exception """ with open(In) as fIn, tempf(prefix=basename(In) + ".", dir=_ToDelete, delete=False) as fOut: try: res = run([sys.executable, Prog], stdin=fIn, stdout=fOut, stderr=PIPE, timeout=TMOUT) except TimeoutExpired as E: res = CompletedProcess(E.args, -1, stderr=str(E).encode(errors='replace')) # TODO syntax error if res.returncode: return RuntimeError(res.stderr.decode(errors='replace')) return fOut.name
def py_runner(prog, infile): #print(f"Generate {prog} < {infile}") with open(infile) as fIn, tempf(prefix=bname(infile) + ".", delete=False) as fOut: _ToDelete.append(fOut) res = run([sys.executable, prog], stdin=fIn, stdout=fOut, timeout=TMOUT) return fOut.name
def osable(code, data="", timeout=10): with tempf("w+", dir=codepath, suffix=".abe") as codef, tempf("w+", dir=codepath, suffix=".txt") as inpf: codef.write(code) inpf.write(data) codef.flush() inpf.flush() # If it fails, bubbles up TimeoutError + CalledProcessError codef.seek(0) output = subprocess.run( f'osabie "{codef.name}" < "{inpf.name}"', stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout, shell=True, encoding="UTF-8", ) return output.stdout
def gitlabfile(url: str) -> str: """ Download single file from GitLab :param url: File URL :return: Temporary file name """ # https://git.cs.msu.ru/gkuryachiy/prac/-/blob/master/checker/README.md # https://git.cs.msu.ru/gkuryachiy/prac/-/raw/master/checker/README.md?inline=false base, path = url.split("/-/") dpath = path.replace("blob/", "raw/") + "?inline=false" outf = tempf(suffix="." + basename(path), dir=_ToDelete, delete=False) with urllib.request.urlopen(base + "/-/" + dpath) as f: outf.write(f.read()) outf.close() return outf.name
def githubfile(url: str) -> str: """ Download single file from GitHub :param url: File URL :return: Temporary file name """ # https://github.com/cmc-python-dev/python2020/blob/master/README.md # https://raw.githubusercontent.com/cmc-python-dev/python2020/master/README.md durl = url.replace("/blob/", "/").replace("/github.com/", "/raw.githubusercontent.com/") print("##", durl) outf = tempf(suffix="." + basename(url), dir=_ToDelete, delete=False) with urllib.request.urlopen(durl) as f: outf.write(f.read()) outf.close() return outf.name sys.exit(res)
def write_kmu(key, perm, target_addr, slot, snr): target_addr_offset = 0x00FF8400 + slot * 0x8 perm_offset = 0x00FF8404 + slot * 0x8 keyslot_offset = 0x00FF8800 + slot * 0x10 select_offset = 0x50039500 ih_nrf = ih() ih_nrf.frombytes(pack("<I", target_addr), offset = target_addr_offset) ih_nrf.frombytes(key, offset = keyslot_offset) run(["nrfjprog", "--snr", snr, "--memwr", str(select_offset), "--val", str(slot+1)]).check_returncode() with tempf(suffix = ".hex", buffering = 0) as f: ih_nrf.tofile(f.name, "hex") run(["nrfjprog", "--snr", snr, "--program", f.name]).check_returncode() run(["nrfjprog", "--snr", snr, "--memwr", str(perm_offset), "--val", str(perm)]).check_returncode() run(["nrfjprog", "--snr", snr, "--memwr", str(select_offset), "--val", str(0)]).check_returncode()