Skip to content

Commit 02ad714

Browse files
lurchdpgeorge
authored andcommitted
tools/gen-cpydiff.py: Use os.path.join and os.path.isdir.
Makes path handling clearer and simpler. Signed-off-by: Jim Mussared <[email protected]>
1 parent c1ae7d7 commit 02ad714

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

tools/gen-cpydiff.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
manually using the command make gen-cpydiff. """
2828

2929
import os
30-
import errno
3130
import subprocess
3231
import time
3332
import re
@@ -44,8 +43,8 @@
4443
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
4544
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython")
4645

47-
TESTPATH = "../tests/cpydiff/"
48-
DOCPATH = "../docs/genrst/"
46+
TESTPATH = "../tests/cpydiff"
47+
DOCPATH = "../docs/genrst"
4948
INDEXTEMPLATE = "../docs/differences/index_template.txt"
5049
INDEX = "index.rst"
5150

@@ -79,7 +78,8 @@ def readfiles():
7978
files = []
8079

8180
for test in tests:
82-
text = open(TESTPATH + test, "r").read()
81+
test_fullpath = os.path.join(TESTPATH, test)
82+
text = open(test_fullpath, "r").read()
8383

8484
try:
8585
class_, desc, cause, workaround, code = [
@@ -98,7 +98,7 @@ def readfiles():
9898
output = Output(test, class_, desc, cause, workaround, code, "", "", "")
9999
files.append(output)
100100
except IndexError:
101-
print("Incorrect format in file " + TESTPATH + test)
101+
print("Incorrect format in file " + test_fullpath)
102102

103103
return files
104104

@@ -107,7 +107,8 @@ def run_tests(tests):
107107
"""executes all tests"""
108108
results = []
109109
for test in tests:
110-
with open(TESTPATH + test.name, "rb") as f:
110+
test_fullpath = os.path.join(TESTPATH, test.name)
111+
with open(test_fullpath, "rb") as f:
111112
input_py = f.read()
112113

113114
process = subprocess.Popen(
@@ -130,7 +131,7 @@ def run_tests(tests):
130131

131132
if output_cpy[0] == output_upy[0] and output_cpy[1] == output_upy[1]:
132133
status = "Supported"
133-
print("Supported operation!\nFile: " + TESTPATH + test.name)
134+
print("Supported operation!\nFile: " + test_fullpath)
134135
else:
135136
status = "Unsupported"
136137

@@ -197,11 +198,8 @@ def gen_rst(results):
197198
"""creates restructured text documents to display tests"""
198199

199200
# make sure the destination directory exists
200-
try:
201+
if not os.path.isdir(DOCPATH):
201202
os.mkdir(DOCPATH)
202-
except OSError as e:
203-
if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR:
204-
raise
205203

206204
toctree = []
207205
class_ = []
@@ -214,7 +212,7 @@ def gen_rst(results):
214212
if i >= len(class_) or section[i] != class_[i]:
215213
if i == 0:
216214
filename = section[i].replace(" ", "_").lower()
217-
rst = open(DOCPATH + filename + ".rst", "w")
215+
rst = open(os.path.join(DOCPATH, filename + ".rst"), "w")
218216
rst.write(HEADER)
219217
rst.write(section[i] + "\n")
220218
rst.write(RSTCHARS[0] * len(section[i]))
@@ -225,7 +223,7 @@ def gen_rst(results):
225223
rst.write(RSTCHARS[min(i, len(RSTCHARS) - 1)] * len(section[i]))
226224
rst.write("\n\n")
227225
class_ = section
228-
rst.write(".. _cpydiff_%s:\n\n" % output.name.rsplit(".", 1)[0])
226+
rst.write(".. _cpydiff_%s:\n\n" % os.path.splitext(output.name)[0])
229227
rst.write(output.desc + "\n")
230228
rst.write("~" * len(output.desc) + "\n\n")
231229
if output.cause != "Unknown":
@@ -242,7 +240,7 @@ def gen_rst(results):
242240
rst.write(table)
243241

244242
template = open(INDEXTEMPLATE, "r")
245-
index = open(DOCPATH + INDEX, "w")
243+
index = open(os.path.join(DOCPATH, INDEX), "w")
246244
index.write(HEADER)
247245
index.write(template.read())
248246
for section in INDEXPRIORITY:

0 commit comments

Comments
 (0)