annotate Tools/scripts/pysource.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 1d84bf5f9478
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
1 #!/usr/bin/env python
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
2
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
3 """\
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
4 List python source files.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
5
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
6 There are three functions to check whether a file is a Python source, listed
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
7 here with increasing complexity:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
8
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
9 - has_python_ext() checks whether a file name ends in '.py[w]'.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
10 - look_like_python() checks whether the file is not binary and either has
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
11 the '.py[w]' extension or the first line contains the word 'python'.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
12 - can_be_compiled() checks whether the file can be compiled by compile().
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
13
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
14 The file also must be of appropriate size - not bigger than a megabyte.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
15
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
16 walk_python_files() recursively lists all Python files under the given directories.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
17 """
40032
5aea8b1fd262 Fix name.
Georg Brandl <georg@python.org>
parents: 35465
diff changeset
18 __author__ = "Oleg Broytmann, Georg Brandl"
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
19
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
20 __all__ = ["has_python_ext", "looks_like_python", "can_be_compiled", "walk_python_files"]
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
21
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
22
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 40032
diff changeset
23 import os, re
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
24
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
25 binary_re = re.compile('[\x00-\x08\x0E-\x1F\x7F]')
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
26
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
27 debug = False
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
28
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
29 def print_debug(msg):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
30 if debug: print msg
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
31
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
32
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
33 def _open(fullpath):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
34 try:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
35 size = os.stat(fullpath).st_size
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
36 except OSError, err: # Permission denied - ignore the file
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
37 print_debug("%s: permission denied: %s" % (fullpath, err))
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
38 return None
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
39
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
40 if size > 1024*1024: # too big
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
41 print_debug("%s: the file is too big: %d bytes" % (fullpath, size))
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
42 return None
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
43
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
44 try:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
45 return open(fullpath, 'rU')
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
46 except IOError, err: # Access denied, or a special file - ignore it
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
47 print_debug("%s: access denied: %s" % (fullpath, err))
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
48 return None
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
49
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
50 def has_python_ext(fullpath):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
51 return fullpath.endswith(".py") or fullpath.endswith(".pyw")
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
52
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
53 def looks_like_python(fullpath):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
54 infile = _open(fullpath)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
55 if infile is None:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
56 return False
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
57
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
58 line = infile.readline()
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
59 infile.close()
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35424
diff changeset
60
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
61 if binary_re.search(line):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
62 # file appears to be binary
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
63 print_debug("%s: appears to be binary" % fullpath)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
64 return False
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35424
diff changeset
65
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
66 if fullpath.endswith(".py") or fullpath.endswith(".pyw"):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
67 return True
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
68 elif "python" in line:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
69 # disguised Python script (e.g. CGI)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
70 return True
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
71
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
72 return False
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
73
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
74 def can_be_compiled(fullpath):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
75 infile = _open(fullpath)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
76 if infile is None:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
77 return False
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
78
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
79 code = infile.read()
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
80 infile.close()
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
81
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
82 try:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
83 compile(code, fullpath, "exec")
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
84 except Exception, err:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
85 print_debug("%s: cannot compile: %s" % (fullpath, err))
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
86 return False
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
87
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
88 return True
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
89
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
90
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
91 def walk_python_files(paths, is_python=looks_like_python, exclude_dirs=None):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
92 """\
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
93 Recursively yield all Python source files below the given paths.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
94
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
95 paths: a list of files and/or directories to be checked.
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
96 is_python: a function that takes a file name and checks whether it is a
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
97 Python source file
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35424
diff changeset
98 exclude_dirs: a list of directory base names that should be excluded in
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
99 the search
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
100 """
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
101 if exclude_dirs is None:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
102 exclude_dirs=[]
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35424
diff changeset
103
35424
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
104 for path in paths:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
105 print_debug("testing: %s" % path)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
106 if os.path.isfile(path):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
107 if is_python(path):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
108 yield path
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
109 elif os.path.isdir(path):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
110 print_debug(" it is a directory")
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
111 for dirpath, dirnames, filenames in os.walk(path):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
112 for exclude in exclude_dirs:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
113 if exclude in dirnames:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
114 dirnames.remove(exclude)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
115 for filename in filenames:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
116 fullpath = os.path.join(dirpath, filename)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
117 print_debug("testing: %s" % fullpath)
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
118 if is_python(fullpath):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
119 yield fullpath
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
120 else:
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
121 print_debug(" unknown type")
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
122
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
123
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
124 if __name__ == "__main__":
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
125 # Two simple examples/tests
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
126 for fullpath in walk_python_files(['.']):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
127 print fullpath
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
128 print "----------"
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
129 for fullpath in walk_python_files(['.'], is_python=can_be_compiled):
7caabc749975 Patch [ 784089 ] A program to scan python files and list those require coding
Georg Brandl <georg@python.org>
parents:
diff changeset
130 print fullpath