blob: a02bbfe201815c85e3db8feeb732555f32f9d017 [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
7__author__ = "Guido van Rossum <guido@python.org>"
8
9# Support imports (need to be imported first)
Martin v. Löwisa675ef12008-03-24 00:50:58 +000010from . import support
Martin v. Löwisef04c442008-03-19 05:04:44 +000011
12# Python imports
13import os
14import sys
15import logging
16
17# Local imports
18from .. import pytree
19import pgen2
20from pgen2 import driver
21
22logging.basicConfig()
23
24def main():
25 gr = driver.load_grammar("Grammar.txt")
26 dr = driver.Driver(gr, convert=pytree.convert)
27
28 fn = "example.py"
29 tree = dr.parse_file(fn, debug=True)
30 if not diff(fn, tree):
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000031 print("No diffs.")
Martin v. Löwisef04c442008-03-19 05:04:44 +000032 if not sys.argv[1:]:
33 return # Pass a dummy argument to run the complete test suite below
34
35 problems = []
36
37 # Process every imported module
38 for name in sys.modules:
39 mod = sys.modules[name]
40 if mod is None or not hasattr(mod, "__file__"):
41 continue
42 fn = mod.__file__
43 if fn.endswith(".pyc"):
44 fn = fn[:-1]
45 if not fn.endswith(".py"):
46 continue
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000047 print("Parsing", fn, file=sys.stderr)
Martin v. Löwisef04c442008-03-19 05:04:44 +000048 tree = dr.parse_file(fn, debug=True)
49 if diff(fn, tree):
50 problems.append(fn)
51
52 # Process every single module on sys.path (but not in packages)
53 for dir in sys.path:
54 try:
55 names = os.listdir(dir)
56 except os.error:
57 continue
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000058 print("Scanning", dir, "...", file=sys.stderr)
Martin v. Löwisef04c442008-03-19 05:04:44 +000059 for name in names:
60 if not name.endswith(".py"):
61 continue
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000062 print("Parsing", name, file=sys.stderr)
Martin v. Löwisef04c442008-03-19 05:04:44 +000063 fn = os.path.join(dir, name)
64 try:
65 tree = dr.parse_file(fn, debug=True)
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000066 except pgen2.parse.ParseError as err:
67 print("ParseError:", err)
Martin v. Löwisef04c442008-03-19 05:04:44 +000068 else:
69 if diff(fn, tree):
70 problems.append(fn)
71
72 # Show summary of problem files
73 if not problems:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000074 print("No problems. Congratulations!")
Martin v. Löwisef04c442008-03-19 05:04:44 +000075 else:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000076 print("Problems in following files:")
Martin v. Löwisef04c442008-03-19 05:04:44 +000077 for fn in problems:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000078 print("***", fn)
Martin v. Löwisef04c442008-03-19 05:04:44 +000079
80def diff(fn, tree):
81 f = open("@", "w")
82 try:
83 f.write(str(tree))
84 finally:
85 f.close()
86 try:
87 return os.system("diff -u %s @" % fn)
88 finally:
89 os.remove("@")
90
91if __name__ == "__main__":
92 main()