Minor fix for currentframe (SF #1652788).
[python.git] / Doc / tools / listmodules.py
blob5f3ea026a32e2b689d746ed62148f3898f504bf7
1 # $Id$
3 # Locate all standard modules available in this build.
5 # This script is designed to run on Python 1.5.2 and newer.
7 # Written by Fredrik Lundh, January 2005
10 import imp, sys, os, re, time
12 identifier = "python-%s-%s" % (sys.version[:3], sys.platform)
13 timestamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(time.time()))
15 # known test packages
16 TEST_PACKAGES = "test.", "bsddb.test.", "distutils.tests."
18 try:
19 import platform
20 platform = platform.platform()
21 except:
22 platform = None # unknown
24 suffixes = imp.get_suffixes()
26 def get_suffix(file):
27 for suffix in suffixes:
28 if file[-len(suffix[0]):] == suffix[0]:
29 return suffix
30 return None
32 def main():
34 path = getpath()
36 modules = {}
37 for m in sys.builtin_module_names:
38 modules[m] = None
40 for p in path:
41 modules.update(getmodules(p))
43 keys = modules.keys()
44 keys.sort()
46 # filter out known test packages
47 def cb(m):
48 for d in TEST_PACKAGES:
49 if m[:len(d)] == d:
50 return 0
51 return 1
52 keys = filter(cb, keys)
54 try:
55 outfile = sys.argv[1]
56 if outfile == "-":
57 outfile = None
58 elif outfile == "-f":
59 outfile = "modules-" + identifier + ".txt"
60 except IndexError:
61 outfile = None
63 if not outfile:
64 out = sys.stdout
65 else:
66 out = open(outfile, "w")
68 out.write("# module list (generated by listmodules.py)\n")
69 out.write("#\n")
70 out.write("# timestamp=%s\n" % repr(timestamp))
71 out.write("# sys.version=%s\n" % repr(sys.version))
72 out.write("# sys.platform=%s\n" % repr(sys.platform))
73 if platform:
74 out.write("# platform=%s\n" % repr(platform))
75 out.write("#\n")
77 for k in keys:
78 out.write(k + "\n")
80 if out is not sys.stdout:
81 out.close()
82 print out.name, "ok (%d modules)" % len(modules)
84 def getmodules(p):
85 # get modules in a given directory
86 modules = {}
87 for f in os.listdir(p):
88 f = os.path.join(p, f)
89 if os.path.isfile(f):
90 m, e = os.path.splitext(f)
91 suffix = get_suffix(f)
92 if not suffix:
93 continue
94 m = os.path.basename(m)
95 if re.compile("(?i)[a-z_]\w*$").match(m):
96 if suffix[2] == imp.C_EXTENSION:
97 # check that this extension can be imported
98 try:
99 __import__(m)
100 except ImportError:
101 continue
102 modules[m] = f
103 elif os.path.isdir(f):
104 m = os.path.basename(f)
105 if os.path.isfile(os.path.join(f, "__init__.py")):
106 for mm, f in getmodules(f).items():
107 modules[m + "." + mm] = f
108 return modules
110 def getpath():
111 path = map(os.path.normcase, map(os.path.abspath, sys.path[:]))
112 # get rid of site packages
113 for p in path:
114 if p[-13:] == "site-packages":
115 def cb(p, site_package_path=os.path.abspath(p)):
116 return p[:len(site_package_path)] != site_package_path
117 path = filter(cb, path)
118 break
119 # get rid of non-existent directories and the current directory
120 def cb(p, cwd=os.path.normcase(os.getcwd())):
121 return os.path.isdir(p) and p != cwd
122 path = filter(cb, path)
123 return path
125 if __name__ == "__main__":
126 main()