blob: c6359bf18a1b34df183954eef962f1b5c3473df0 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Martin v. Löwisef04c442008-03-19 05:04:44 +00002# Copyright 2006 Google, Inc. All Rights Reserved.
3# Licensed to PSF under a Contributor Agreement.
4
5"""Main program for testing the infrastructure."""
6
Zachary Ware2acbae82014-10-29 12:24:59 -05007from __future__ import print_function
8
Martin v. Löwisef04c442008-03-19 05:04:44 +00009__author__ = "Guido van Rossum <guido@python.org>"
10
11# Support imports (need to be imported first)
Martin v. Löwisa675ef12008-03-24 00:50:58 +000012from . import support
Martin v. Löwisef04c442008-03-19 05:04:44 +000013
14# Python imports
15import os
16import sys
17import logging
18
19# Local imports
20from .. import pytree
21import 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):
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000033 print("No diffs.")
Martin v. Löwisef04c442008-03-19 05:04:44 +000034 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
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000049 print("Parsing", fn, file=sys.stderr)
Martin v. Löwisef04c442008-03-19 05:04:44 +000050 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)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +020058 except OSError:
Martin v. Löwisef04c442008-03-19 05:04:44 +000059 continue
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000060 print("Scanning", dir, "...", file=sys.stderr)
Martin v. Löwisef04c442008-03-19 05:04:44 +000061 for name in names:
62 if not name.endswith(".py"):
63 continue
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000064 print("Parsing", name, file=sys.stderr)
Martin v. Löwisef04c442008-03-19 05:04:44 +000065 fn = os.path.join(dir, name)
66 try:
67 tree = dr.parse_file(fn, debug=True)
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000068 except pgen2.parse.ParseError as err:
69 print("ParseError:", err)
Martin v. Löwisef04c442008-03-19 05:04:44 +000070 else:
71 if diff(fn, tree):
72 problems.append(fn)
73
74 # Show summary of problem files
75 if not problems:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000076 print("No problems. Congratulations!")
Martin v. Löwisef04c442008-03-19 05:04:44 +000077 else:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000078 print("Problems in following files:")
Martin v. Löwisef04c442008-03-19 05:04:44 +000079 for fn in problems:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000080 print("***", fn)
Martin v. Löwisef04c442008-03-19 05:04:44 +000081
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()