From 46713482f49690f96e55f872c087d245b4ce0489 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 27 Apr 2020 14:36:14 -0700 Subject: [PATCH] Add verification script --- Makefile | 4 ++-- tools/extract_pyi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tools/extract_pyi.py diff --git a/Makefile b/Makefile index 5e949cd40f..aa8fc26d1f 100644 --- a/Makefile +++ b/Makefile @@ -213,8 +213,8 @@ check-translate: locale/circuitpython.pot $(wildcard locale/*.po) $(PYTHON) tools/check_translations.py $^ stubs: - rst2pyi $(VALIDATE) shared-bindings/ $(STUBDIR) - python setup.py sdist + python tools/extract_pyi.py shared-bindings/ $(STUBDIR) + #python setup.py sdist update-frozen-libraries: @echo "Updating all frozen libraries to latest tagged version." diff --git a/tools/extract_pyi.py b/tools/extract_pyi.py new file mode 100644 index 0000000000..c7f3e4604e --- /dev/null +++ b/tools/extract_pyi.py @@ -0,0 +1,46 @@ +import os +import sys +import astroid +import traceback + +top_level = sys.argv[1].strip("/") +stub_directory = sys.argv[2] + +if top_level.count("/") == 1: + top_level, module = top_level.split("/") + modules = [module] +else: + modules = os.listdir(top_level) + modules = sorted(modules) + +ok = 0 +total = 0 +for module in modules: + module_path = os.path.join(top_level, module) + if not os.path.isdir(module_path): + continue + pyi_lines = [] + for class_file in os.listdir(module_path): + class_path = os.path.join(module_path, class_file) + with open(class_path, "r") as f: + for line in f: + if line.startswith("//| "): + pyi_lines.append(line[4:]) + + stub_filename = os.path.join(stub_directory, module + ".pyi") + print(stub_filename) + stub_contents = "".join(pyi_lines) + with open(stub_filename, "w") as f: + f.write(stub_contents) + + # Validate that the module is a parseable stub. + total += 1 + try: + astroid.parse(stub_contents) + ok += 1 + except astroid.exceptions.AstroidSyntaxError as e: + e = e.__cause__ + traceback.print_exception(type(e), e, e.__traceback__) + print() + +print(f"{ok} ok out of {total}")