#!/usr/bin/env python3# This file is part of the MicroPython project, http://micropython.org/# The MIT License (MIT)# Copyright (c) 2019 Damien P. Georgeimport osimport subprocessimport sysimport argparsesys.path.append("../tools")import pyboard# Paths for host executablesCPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython-coverage")NATMOD_EXAMPLE_DIR = "../examples/natmod/"# Supported tests and their corresponding mpy moduleTEST_MAPPINGS = {"btree": "btree/btree_$(ARCH).mpy","framebuf": "framebuf/framebuf_$(ARCH).mpy","uheapq": "uheapq/uheapq_$(ARCH).mpy","urandom": "urandom/urandom_$(ARCH).mpy","ure": "ure/ure_$(ARCH).mpy","uzlib": "uzlib/uzlib_$(ARCH).mpy",}# Code to allow a target MicroPython to import an .mpy from RAMinjected_import_hook_code = """\import usys, uos, uioclass __File(uio.IOBase):def __init__(self):self.off = 0def ioctl(self, request, arg):return 0def readinto(self, buf):buf[:] = memoryview(__buf)[self.off:self.off + len(buf)]self.off += len(buf)return len(buf)class __FS:def mount(self, readonly, mkfs):passdef chdir(self, path):passdef stat(self, path):if path == '__injected.mpy':return tuple(0 for _ in range(10))else:raise OSError(-2) # ENOENTdef open(self, path, mode):return __File()uos.mount(__FS(), '/__remote')uos.chdir('/__remote')usys.modules['{}'] = __import__('__injected')"""class TargetSubprocess:def __init__(self, cmd):self.cmd = cmddef close(self):passdef run_script(self, script):try:p = subprocess.run(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script)return p.stdout, Noneexcept subprocess.CalledProcessError as er:return b"", erclass TargetPyboard:def __init__(self, pyb):self.pyb = pybself.pyb.enter_raw_repl()def close(self):self.pyb.exit_raw_repl()self.pyb.close()def run_script(self, script):try:self.pyb.enter_raw_repl()output = self.pyb.exec_(script)output = output.replace(b"\r\n", b"\n")return output, Noneexcept pyboard.PyboardError as er:return b"", erdef run_tests(target_truth, target, args, stats):for test_file in args.files:# Find supported testfor k, v in TEST_MAPPINGS.items():if test_file.find(k) != -1:test_module = ktest_mpy = v.replace("$(ARCH)", args.arch)breakelse:print("---- {} - no matching mpy".format(test_file))continue# Read test scriptwith open(test_file, "rb") as f:test_file_data = f.read()# Create full test with embedded .mpytry:with open(NATMOD_EXAMPLE_DIR + test_mpy, "rb") as f:test_script = b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n"except OSError:print("---- {} - mpy file not compiled".format(test_file))continuetest_script += bytes(injected_import_hook_code.format(test_module), "ascii")test_script += test_file_data# Run test under MicroPythonresult_out, error = target.run_script(test_script)# Work out result of testextra = ""if error is None and result_out == b"SKIP\n":result = "SKIP"elif error is not None:result = "FAIL"extra = " - " + str(error)else:# Check result against truthtry:with open(test_file + ".exp", "rb") as f:result_exp = f.read()error = Noneexcept OSError:result_exp, error = target_truth.run_script(test_file_data)if error is not None:result = "TRUTH FAIL"elif result_out != result_exp:result = "FAIL"print(result_out)else:result = "pass"# Accumulate statisticsstats["total"] += 1if result == "pass":stats["pass"] += 1elif result == "SKIP":stats["skip"] += 1else:stats["fail"] += 1# Print resultprint("{:4} {}{}".format(result, test_file, extra))def main():cmd_parser = argparse.ArgumentParser(description="Run dynamic-native-module tests under MicroPython")cmd_parser.add_argument("-p", "--pyboard", action="store_true", help="run tests via pyboard.py")cmd_parser.add_argument("-d", "--device", default="/dev/ttyACM0", help="the device for pyboard.py")cmd_parser.add_argument("-a", "--arch", default="x64", help="native architecture of the target")cmd_parser.add_argument("files", nargs="*", help="input test files")args = cmd_parser.parse_args()target_truth = TargetSubprocess([CPYTHON3])if args.pyboard:target = TargetPyboard(pyboard.Pyboard(args.device))else:target = TargetSubprocess([MICROPYTHON])stats = {"total": 0, "pass": 0, "fail": 0, "skip": 0}run_tests(target_truth, target, args, stats)target.close()target_truth.close()print("{} tests performed".format(stats["total"]))print("{} tests passed".format(stats["pass"]))if stats["fail"]:print("{} tests failed".format(stats["fail"]))if stats["skip"]:print("{} tests skipped".format(stats["skip"]))if stats["fail"]:sys.exit(1)if __name__ == "__main__":main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。