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