| 1 | n/a | #!/usr/bin/env python3 |
|---|
| 2 | n/a | # Copyright 2006 Google, Inc. All Rights Reserved. |
|---|
| 3 | n/a | # Licensed to PSF under a Contributor Agreement. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | """Main program for testing the infrastructure.""" |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from __future__ import print_function |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | __author__ = "Guido van Rossum <guido@python.org>" |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | # Support imports (need to be imported first) |
|---|
| 12 | n/a | from . import support |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # Python imports |
|---|
| 15 | n/a | import os |
|---|
| 16 | n/a | import sys |
|---|
| 17 | n/a | import logging |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | # Local imports |
|---|
| 20 | n/a | from .. import pytree |
|---|
| 21 | n/a | from .. import pgen2 |
|---|
| 22 | n/a | from ..pgen2 import driver |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | logging.basicConfig() |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def main(): |
|---|
| 27 | n/a | gr = driver.load_grammar("Grammar.txt") |
|---|
| 28 | n/a | dr = driver.Driver(gr, convert=pytree.convert) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | fn = "example.py" |
|---|
| 31 | n/a | tree = dr.parse_file(fn, debug=True) |
|---|
| 32 | n/a | if not diff(fn, tree): |
|---|
| 33 | n/a | print("No diffs.") |
|---|
| 34 | n/a | if not sys.argv[1:]: |
|---|
| 35 | n/a | return # Pass a dummy argument to run the complete test suite below |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | problems = [] |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | # Process every imported module |
|---|
| 40 | n/a | for name in sys.modules: |
|---|
| 41 | n/a | mod = sys.modules[name] |
|---|
| 42 | n/a | if mod is None or not hasattr(mod, "__file__"): |
|---|
| 43 | n/a | continue |
|---|
| 44 | n/a | fn = mod.__file__ |
|---|
| 45 | n/a | if fn.endswith(".pyc"): |
|---|
| 46 | n/a | fn = fn[:-1] |
|---|
| 47 | n/a | if not fn.endswith(".py"): |
|---|
| 48 | n/a | continue |
|---|
| 49 | n/a | print("Parsing", fn, file=sys.stderr) |
|---|
| 50 | n/a | tree = dr.parse_file(fn, debug=True) |
|---|
| 51 | n/a | if diff(fn, tree): |
|---|
| 52 | n/a | problems.append(fn) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | # Process every single module on sys.path (but not in packages) |
|---|
| 55 | n/a | for dir in sys.path: |
|---|
| 56 | n/a | try: |
|---|
| 57 | n/a | names = os.listdir(dir) |
|---|
| 58 | n/a | except OSError: |
|---|
| 59 | n/a | continue |
|---|
| 60 | n/a | print("Scanning", dir, "...", file=sys.stderr) |
|---|
| 61 | n/a | for name in names: |
|---|
| 62 | n/a | if not name.endswith(".py"): |
|---|
| 63 | n/a | continue |
|---|
| 64 | n/a | print("Parsing", name, file=sys.stderr) |
|---|
| 65 | n/a | fn = os.path.join(dir, name) |
|---|
| 66 | n/a | try: |
|---|
| 67 | n/a | tree = dr.parse_file(fn, debug=True) |
|---|
| 68 | n/a | except pgen2.parse.ParseError as err: |
|---|
| 69 | n/a | print("ParseError:", err) |
|---|
| 70 | n/a | else: |
|---|
| 71 | n/a | if diff(fn, tree): |
|---|
| 72 | n/a | problems.append(fn) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | # Show summary of problem files |
|---|
| 75 | n/a | if not problems: |
|---|
| 76 | n/a | print("No problems. Congratulations!") |
|---|
| 77 | n/a | else: |
|---|
| 78 | n/a | print("Problems in following files:") |
|---|
| 79 | n/a | for fn in problems: |
|---|
| 80 | n/a | print("***", fn) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def diff(fn, tree): |
|---|
| 83 | n/a | f = open("@", "w") |
|---|
| 84 | n/a | try: |
|---|
| 85 | n/a | f.write(str(tree)) |
|---|
| 86 | n/a | finally: |
|---|
| 87 | n/a | f.close() |
|---|
| 88 | n/a | try: |
|---|
| 89 | n/a | return os.system("diff -u %s @" % fn) |
|---|
| 90 | n/a | finally: |
|---|
| 91 | n/a | os.remove("@") |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | if __name__ == "__main__": |
|---|
| 94 | n/a | main() |
|---|