2017-02-07 00:43:41 -05:00
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
#
|
|
|
|
# The MIT License (MIT)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2016 Rami Ali
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
""" gen-cpydiff generates documentation which outlines operations that differ between MicroPython
|
|
|
|
and CPython. This script is called by the docs Makefile for html and Latex and may be run
|
|
|
|
manually using the command make gen-cpydiff. """
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import re
|
|
|
|
from collections import namedtuple
|
|
|
|
|
2017-06-30 03:22:17 -04:00
|
|
|
# MicroPython supports syntax of CPython 3.4 with some features from 3.5, and
|
2017-02-25 07:53:56 -05:00
|
|
|
# such version should be used to test for differences. If your default python3
|
|
|
|
# executable is of lower version, you can point MICROPY_CPYTHON3 environment var
|
|
|
|
# to the correct executable.
|
2020-02-26 23:36:53 -05:00
|
|
|
if os.name == "nt":
|
|
|
|
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3.exe")
|
|
|
|
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe")
|
2017-02-25 07:53:56 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
|
2022-08-17 21:46:53 -04:00
|
|
|
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython")
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-08-22 11:49:10 -04:00
|
|
|
TESTPATH = "../tests/cpydiff"
|
|
|
|
DOCPATH = "../docs/genrst"
|
2020-02-26 23:36:53 -05:00
|
|
|
INDEXTEMPLATE = "../docs/differences/index_template.txt"
|
|
|
|
INDEX = "index.rst"
|
|
|
|
|
|
|
|
HEADER = ".. This document was generated by tools/gen-cpydiff.py\n\n"
|
|
|
|
CLASSMAP = {"Core": "Core language", "Types": "Builtin types"}
|
|
|
|
INDEXPRIORITY = ["syntax", "core_language", "builtin_types", "modules"]
|
|
|
|
RSTCHARS = ["=", "-", "~", "`", ":"]
|
2017-02-07 00:43:41 -05:00
|
|
|
SPLIT = '"""\n|categories: |description: |cause: |workaround: '
|
2020-02-26 23:36:53 -05:00
|
|
|
TAB = " "
|
|
|
|
|
|
|
|
Output = namedtuple(
|
|
|
|
"output",
|
|
|
|
[
|
|
|
|
"name",
|
|
|
|
"class_",
|
|
|
|
"desc",
|
|
|
|
"cause",
|
|
|
|
"workaround",
|
|
|
|
"code",
|
|
|
|
"output_cpy",
|
|
|
|
"output_upy",
|
|
|
|
"status",
|
|
|
|
],
|
|
|
|
)
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
def readfiles():
|
2021-04-26 19:15:43 -04:00
|
|
|
"""Reads test files"""
|
2020-02-26 23:36:53 -05:00
|
|
|
tests = list(filter(lambda x: x.endswith(".py"), os.listdir(TESTPATH)))
|
2017-02-07 00:43:41 -05:00
|
|
|
tests.sort()
|
|
|
|
files = []
|
|
|
|
|
|
|
|
for test in tests:
|
2020-08-22 11:49:10 -04:00
|
|
|
test_fullpath = os.path.join(TESTPATH, test)
|
|
|
|
text = open(test_fullpath, "r").read()
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
try:
|
2020-02-26 23:36:53 -05:00
|
|
|
class_, desc, cause, workaround, code = [
|
|
|
|
x.rstrip() for x in list(filter(None, re.split(SPLIT, text)))
|
|
|
|
]
|
2022-03-23 15:18:23 -04:00
|
|
|
|
|
|
|
# remove black `fmt: on/off/skip` comments
|
|
|
|
code = "".join(
|
|
|
|
# skip comments are inline, so we replace just the comment
|
|
|
|
re.sub(r"\s*# fmt: skip", "", x)
|
|
|
|
for x in code.splitlines(keepends=True)
|
|
|
|
# on/off comments are on their own line, so we omit the entire line
|
|
|
|
if not re.match(r"\s*# fmt: (on|off)\s*", x)
|
|
|
|
)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
output = Output(test, class_, desc, cause, workaround, code, "", "", "")
|
2017-02-07 00:43:41 -05:00
|
|
|
files.append(output)
|
|
|
|
except IndexError:
|
2020-08-22 11:49:10 -04:00
|
|
|
print("Incorrect format in file " + test_fullpath)
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
return files
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2017-02-07 00:43:41 -05:00
|
|
|
def run_tests(tests):
|
2021-04-26 19:15:43 -04:00
|
|
|
"""executes all tests"""
|
2017-02-07 00:43:41 -05:00
|
|
|
results = []
|
|
|
|
for test in tests:
|
2020-08-22 11:49:10 -04:00
|
|
|
test_fullpath = os.path.join(TESTPATH, test.name)
|
|
|
|
with open(test_fullpath, "rb") as f:
|
2021-08-12 01:28:12 -04:00
|
|
|
input_py = f.read()
|
2017-02-07 00:43:41 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
process = subprocess.Popen(
|
|
|
|
CPYTHON3,
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
2021-08-12 01:28:12 -04:00
|
|
|
output_cpy = [com.decode("utf8") for com in process.communicate(input_py)]
|
2017-02-07 00:43:41 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
process = subprocess.Popen(
|
|
|
|
MICROPYTHON,
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
2021-08-12 01:28:12 -04:00
|
|
|
output_upy = [com.decode("utf8") for com in process.communicate(input_py)]
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
if output_cpy[0] == output_upy[0] and output_cpy[1] == output_upy[1]:
|
2020-02-26 23:36:53 -05:00
|
|
|
status = "Supported"
|
2020-08-22 11:49:10 -04:00
|
|
|
print("Supported operation!\nFile: " + test_fullpath)
|
2017-02-07 00:43:41 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
status = "Unsupported"
|
2017-02-07 00:43:41 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
output = Output(
|
|
|
|
test.name,
|
|
|
|
test.class_,
|
|
|
|
test.desc,
|
|
|
|
test.cause,
|
|
|
|
test.workaround,
|
|
|
|
test.code,
|
|
|
|
output_cpy,
|
|
|
|
output_upy,
|
|
|
|
status,
|
|
|
|
)
|
2017-02-07 00:43:41 -05:00
|
|
|
results.append(output)
|
|
|
|
|
|
|
|
results.sort(key=lambda x: x.class_)
|
|
|
|
return results
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2017-02-07 00:43:41 -05:00
|
|
|
def indent(block, spaces):
|
2021-04-26 19:15:43 -04:00
|
|
|
"""indents paragraphs of text for rst formatting"""
|
2020-02-26 23:36:53 -05:00
|
|
|
new_block = ""
|
|
|
|
for line in block.split("\n"):
|
|
|
|
new_block += spaces + line + "\n"
|
2017-02-07 00:43:41 -05:00
|
|
|
return new_block
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2017-02-07 00:43:41 -05:00
|
|
|
def gen_table(contents):
|
2021-04-26 19:15:43 -04:00
|
|
|
"""creates a table given any set of columns"""
|
2017-02-07 00:43:41 -05:00
|
|
|
xlengths = []
|
|
|
|
ylengths = []
|
|
|
|
for column in contents:
|
|
|
|
col_len = 0
|
|
|
|
for entry in column:
|
2020-02-26 23:36:53 -05:00
|
|
|
lines = entry.split("\n")
|
2017-02-07 00:43:41 -05:00
|
|
|
for line in lines:
|
|
|
|
col_len = max(len(line) + 2, col_len)
|
|
|
|
xlengths.append(col_len)
|
|
|
|
for i in range(len(contents[0])):
|
|
|
|
ymax = 0
|
|
|
|
for j in range(len(contents)):
|
2020-02-26 23:36:53 -05:00
|
|
|
ymax = max(ymax, len(contents[j][i].split("\n")))
|
2017-02-07 00:43:41 -05:00
|
|
|
ylengths.append(ymax)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
table_divider = "+" + "".join(["-" * i + "+" for i in xlengths]) + "\n"
|
2017-02-07 00:43:41 -05:00
|
|
|
table = table_divider
|
|
|
|
for i in range(len(ylengths)):
|
|
|
|
row = [column[i] for column in contents]
|
2020-02-26 23:36:53 -05:00
|
|
|
row = [entry + "\n" * (ylengths[i] - len(entry.split("\n"))) for entry in row]
|
|
|
|
row = [entry.split("\n") for entry in row]
|
2017-02-07 00:43:41 -05:00
|
|
|
for j in range(ylengths[i]):
|
|
|
|
k = 0
|
|
|
|
for entry in row:
|
|
|
|
width = xlengths[k]
|
2020-02-26 23:36:53 -05:00
|
|
|
table += "".join(["| {:{}}".format(entry[j], width - 1)])
|
2017-02-07 00:43:41 -05:00
|
|
|
k += 1
|
2020-02-26 23:36:53 -05:00
|
|
|
table += "|\n"
|
2017-02-07 00:43:41 -05:00
|
|
|
table += table_divider
|
2020-02-26 23:36:53 -05:00
|
|
|
return table + "\n"
|
|
|
|
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
def gen_rst(results):
|
2021-04-26 19:15:43 -04:00
|
|
|
"""creates restructured text documents to display tests"""
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
# make sure the destination directory exists
|
2020-08-22 11:49:10 -04:00
|
|
|
if not os.path.isdir(DOCPATH):
|
2017-02-07 00:43:41 -05:00
|
|
|
os.mkdir(DOCPATH)
|
|
|
|
|
|
|
|
toctree = []
|
|
|
|
class_ = []
|
|
|
|
for output in results:
|
2020-02-26 23:36:53 -05:00
|
|
|
section = output.class_.split(",")
|
2017-02-07 00:43:41 -05:00
|
|
|
for i in range(len(section)):
|
|
|
|
section[i] = section[i].rstrip()
|
|
|
|
if section[i] in CLASSMAP:
|
|
|
|
section[i] = CLASSMAP[section[i]]
|
|
|
|
if i >= len(class_) or section[i] != class_[i]:
|
|
|
|
if i == 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
filename = section[i].replace(" ", "_").lower()
|
2020-08-22 11:49:10 -04:00
|
|
|
rst = open(os.path.join(DOCPATH, filename + ".rst"), "w")
|
2017-02-07 00:43:41 -05:00
|
|
|
rst.write(HEADER)
|
2020-02-26 23:36:53 -05:00
|
|
|
rst.write(section[i] + "\n")
|
2017-02-07 00:43:41 -05:00
|
|
|
rst.write(RSTCHARS[0] * len(section[i]))
|
|
|
|
rst.write(time.strftime("\nGenerated %a %d %b %Y %X UTC\n\n", time.gmtime()))
|
|
|
|
toctree.append(filename)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
rst.write(section[i] + "\n")
|
|
|
|
rst.write(RSTCHARS[min(i, len(RSTCHARS) - 1)] * len(section[i]))
|
|
|
|
rst.write("\n\n")
|
2017-02-07 00:43:41 -05:00
|
|
|
class_ = section
|
2020-08-22 11:49:10 -04:00
|
|
|
rst.write(".. _cpydiff_%s:\n\n" % os.path.splitext(output.name)[0])
|
2020-02-26 23:36:53 -05:00
|
|
|
rst.write(output.desc + "\n")
|
|
|
|
rst.write("~" * len(output.desc) + "\n\n")
|
|
|
|
if output.cause != "Unknown":
|
|
|
|
rst.write("**Cause:** " + output.cause + "\n\n")
|
|
|
|
if output.workaround != "Unknown":
|
|
|
|
rst.write("**Workaround:** " + output.workaround + "\n\n")
|
|
|
|
|
|
|
|
rst.write("Sample code::\n\n" + indent(output.code, TAB) + "\n")
|
|
|
|
output_cpy = indent("".join(output.output_cpy[0:2]), TAB).rstrip()
|
|
|
|
output_cpy = ("::\n\n" if output_cpy != "" else "") + output_cpy
|
|
|
|
output_upy = indent("".join(output.output_upy[0:2]), TAB).rstrip()
|
|
|
|
output_upy = ("::\n\n" if output_upy != "" else "") + output_upy
|
|
|
|
table = gen_table([["CPy output:", output_cpy], ["uPy output:", output_upy]])
|
2017-02-07 00:43:41 -05:00
|
|
|
rst.write(table)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
template = open(INDEXTEMPLATE, "r")
|
2020-08-22 11:49:10 -04:00
|
|
|
index = open(os.path.join(DOCPATH, INDEX), "w")
|
2017-02-07 00:43:41 -05:00
|
|
|
index.write(HEADER)
|
|
|
|
index.write(template.read())
|
|
|
|
for section in INDEXPRIORITY:
|
|
|
|
if section in toctree:
|
2020-02-26 23:36:53 -05:00
|
|
|
index.write(indent(section + ".rst", TAB))
|
2017-02-07 00:43:41 -05:00
|
|
|
toctree.remove(section)
|
|
|
|
for section in toctree:
|
2020-02-26 23:36:53 -05:00
|
|
|
index.write(indent(section + ".rst", TAB))
|
|
|
|
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
def main():
|
2021-04-26 19:15:43 -04:00
|
|
|
"""Main function"""
|
2017-02-07 00:43:41 -05:00
|
|
|
|
2017-02-21 23:50:58 -05:00
|
|
|
# set search path so that test scripts find the test modules (and no other ones)
|
2020-02-26 23:36:53 -05:00
|
|
|
os.environ["PYTHONPATH"] = TESTPATH
|
|
|
|
os.environ["MICROPYPATH"] = TESTPATH
|
2017-02-07 00:43:41 -05:00
|
|
|
|
|
|
|
files = readfiles()
|
|
|
|
results = run_tests(files)
|
|
|
|
gen_rst(results)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2017-02-07 00:43:41 -05:00
|
|
|
main()
|