1 | #!/usr/bin/env python
|
---|
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 |
|
---|
7 | __author__ = "Guido van Rossum <guido@python.org>"
|
---|
8 |
|
---|
9 | # Support imports (need to be imported first)
|
---|
10 | from . import support
|
---|
11 |
|
---|
12 | # Python imports
|
---|
13 | import os
|
---|
14 | import sys
|
---|
15 | import logging
|
---|
16 |
|
---|
17 | # Local imports
|
---|
18 | from .. import pytree
|
---|
19 | import pgen2
|
---|
20 | from pgen2 import driver
|
---|
21 |
|
---|
22 | logging.basicConfig()
|
---|
23 |
|
---|
24 | def 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):
|
---|
31 | print "No diffs."
|
---|
32 | 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
|
---|
47 | print >>sys.stderr, "Parsing", fn
|
---|
48 | 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
|
---|
58 | print >>sys.stderr, "Scanning", dir, "..."
|
---|
59 | for name in names:
|
---|
60 | if not name.endswith(".py"):
|
---|
61 | continue
|
---|
62 | print >>sys.stderr, "Parsing", name
|
---|
63 | fn = os.path.join(dir, name)
|
---|
64 | try:
|
---|
65 | tree = dr.parse_file(fn, debug=True)
|
---|
66 | except pgen2.parse.ParseError, err:
|
---|
67 | print "ParseError:", err
|
---|
68 | else:
|
---|
69 | if diff(fn, tree):
|
---|
70 | problems.append(fn)
|
---|
71 |
|
---|
72 | # Show summary of problem files
|
---|
73 | if not problems:
|
---|
74 | print "No problems. Congratulations!"
|
---|
75 | else:
|
---|
76 | print "Problems in following files:"
|
---|
77 | for fn in problems:
|
---|
78 | print "***", fn
|
---|
79 |
|
---|
80 | def 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 |
|
---|
91 | if __name__ == "__main__":
|
---|
92 | main()
|
---|