|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import astroid |
| 4 | +import traceback |
| 5 | + |
| 6 | +top_level = sys.argv[1].strip("/") |
| 7 | + |
| 8 | +if top_level.count("/") == 1: |
| 9 | + top_level, module = top_level.split("/") |
| 10 | + modules = [module] |
| 11 | +else: |
| 12 | + modules = os.listdir(top_level) |
| 13 | + modules = sorted(modules) |
| 14 | + |
| 15 | +ok = 0 |
| 16 | +total = 0 |
| 17 | +for module in modules: |
| 18 | + module_path = os.path.join(top_level, module) |
| 19 | + if not os.path.isdir(module_path): |
| 20 | + continue |
| 21 | + pyi_lines = [] |
| 22 | + classes = os.listdir(module_path) |
| 23 | + classes = [x for x in sorted(classes) if x.endswith(".c")] |
| 24 | + if classes and classes[-1] == "__init__.c": |
| 25 | + classes.insert(0, classes.pop()) |
| 26 | + for class_file in classes: |
| 27 | + class_path = os.path.join(module_path, class_file) |
| 28 | + with open(class_path, "r") as f: |
| 29 | + for line in f: |
| 30 | + if line.startswith("//|"): |
| 31 | + if line[3] == " ": |
| 32 | + line = line[4:] |
| 33 | + elif line[3] == "\n": |
| 34 | + line = line[3:] |
| 35 | + else: |
| 36 | + continue |
| 37 | + pyi_lines.append(line) |
| 38 | + |
| 39 | + raw_stubs = [x for x in sorted(classes) if x.endswith(".pyi")] |
| 40 | + if raw_stubs and raw_stubs[-1] == "__init__.pyi": |
| 41 | + raw_stubs.insert(0, raw_stubs.pop()) |
| 42 | + for raw_stub in raw_stubs: |
| 43 | + raw_stub_path = os.path.join(module_path, raw_stub) |
| 44 | + with open(raw_stub_path, "r") as f: |
| 45 | + pyi_lines.extend(f.readlines()) |
| 46 | + stub_contents = "".join(pyi_lines) |
| 47 | + |
| 48 | + # Validate that the module is a parseable stub. |
| 49 | + total += 1 |
| 50 | + try: |
| 51 | + tree = astroid.parse(stub_contents) |
| 52 | + #print(tree.repr_tree()) |
| 53 | + for i in tree.body: |
| 54 | + for j in i.body: |
| 55 | + if isinstance(j, astroid.scoped_nodes.FunctionDef): |
| 56 | + argdict = j.args.__dict__ |
| 57 | + a = argdict.pop('lineno') |
| 58 | + a = argdict.pop('col_offset') |
| 59 | + a = argdict.pop('parent') |
| 60 | + print(argdict) |
| 61 | + if j.returns: |
| 62 | + returndict = j.returns.__dict__ |
| 63 | + a = returndict.pop('lineno') |
| 64 | + a = returndict.pop('col_offset') |
| 65 | + a = returndict.pop('parent') |
| 66 | + print(returndict) |
| 67 | + print('\n') |
| 68 | + #print(tree.body[0].body[0]) |
| 69 | + else: |
| 70 | + print(type(j)) |
| 71 | + ok += 1 |
| 72 | + except astroid.exceptions.AstroidSyntaxError as e: |
| 73 | + e = e.__cause__ |
| 74 | + traceback.print_exception(type(e), e, e.__traceback__) |
| 75 | + print() |
| 76 | + |
| 77 | +print(f"{ok} ok out of {total}") |
| 78 | + |
| 79 | +if ok != total: |
| 80 | + sys.exit(total - ok) |
0 commit comments