From 02ad71468f1d52f9521bf89dd8b9594a4d437e51 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Sat, 22 Aug 2020 16:49:10 +0100 Subject: [PATCH] tools/gen-cpydiff.py: Use os.path.join and os.path.isdir. Makes path handling clearer and simpler. Signed-off-by: Jim Mussared --- tools/gen-cpydiff.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/tools/gen-cpydiff.py b/tools/gen-cpydiff.py index 46b3079cac..de69a94673 100644 --- a/tools/gen-cpydiff.py +++ b/tools/gen-cpydiff.py @@ -27,7 +27,6 @@ manually using the command make gen-cpydiff. """ import os -import errno import subprocess import time import re @@ -44,8 +43,8 @@ else: CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") -TESTPATH = "../tests/cpydiff/" -DOCPATH = "../docs/genrst/" +TESTPATH = "../tests/cpydiff" +DOCPATH = "../docs/genrst" INDEXTEMPLATE = "../docs/differences/index_template.txt" INDEX = "index.rst" @@ -79,7 +78,8 @@ def readfiles(): files = [] for test in tests: - text = open(TESTPATH + test, "r").read() + test_fullpath = os.path.join(TESTPATH, test) + text = open(test_fullpath, "r").read() try: class_, desc, cause, workaround, code = [ @@ -98,7 +98,7 @@ def readfiles(): output = Output(test, class_, desc, cause, workaround, code, "", "", "") files.append(output) except IndexError: - print("Incorrect format in file " + TESTPATH + test) + print("Incorrect format in file " + test_fullpath) return files @@ -107,7 +107,8 @@ def run_tests(tests): """executes all tests""" results = [] for test in tests: - with open(TESTPATH + test.name, "rb") as f: + test_fullpath = os.path.join(TESTPATH, test.name) + with open(test_fullpath, "rb") as f: input_py = f.read() process = subprocess.Popen( @@ -130,7 +131,7 @@ def run_tests(tests): if output_cpy[0] == output_upy[0] and output_cpy[1] == output_upy[1]: status = "Supported" - print("Supported operation!\nFile: " + TESTPATH + test.name) + print("Supported operation!\nFile: " + test_fullpath) else: status = "Unsupported" @@ -197,11 +198,8 @@ def gen_rst(results): """creates restructured text documents to display tests""" # make sure the destination directory exists - try: + if not os.path.isdir(DOCPATH): os.mkdir(DOCPATH) - except OSError as e: - if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR: - raise toctree = [] class_ = [] @@ -214,7 +212,7 @@ def gen_rst(results): if i >= len(class_) or section[i] != class_[i]: if i == 0: filename = section[i].replace(" ", "_").lower() - rst = open(DOCPATH + filename + ".rst", "w") + rst = open(os.path.join(DOCPATH, filename + ".rst"), "w") rst.write(HEADER) rst.write(section[i] + "\n") rst.write(RSTCHARS[0] * len(section[i])) @@ -225,7 +223,7 @@ def gen_rst(results): rst.write(RSTCHARS[min(i, len(RSTCHARS) - 1)] * len(section[i])) rst.write("\n\n") class_ = section - rst.write(".. _cpydiff_%s:\n\n" % output.name.rsplit(".", 1)[0]) + rst.write(".. _cpydiff_%s:\n\n" % os.path.splitext(output.name)[0]) rst.write(output.desc + "\n") rst.write("~" * len(output.desc) + "\n\n") if output.cause != "Unknown": @@ -242,7 +240,7 @@ def gen_rst(results): rst.write(table) template = open(INDEXTEMPLATE, "r") - index = open(DOCPATH + INDEX, "w") + index = open(os.path.join(DOCPATH, INDEX), "w") index.write(HEADER) index.write(template.read()) for section in INDEXPRIORITY: