Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec2f74f

Browse files
author
Fredrik Lundh
committed
module list utility
1 parent 3e86595 commit ec2f74f

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Doc/tools/listmodules.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# $Id$
2+
#
3+
# Locate all standard modules available in this build.
4+
#
5+
# This script is designed to run on Python 1.5.2 and newer.
6+
#
7+
# Written by Fredrik Lundh, January 2005
8+
#
9+
10+
import imp, sys, os, re, time
11+
12+
identifier = "python-%s-%s" % (sys.version[:3], sys.platform)
13+
timestamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(time.time()))
14+
15+
# known test packages
16+
TEST_PACKAGES = "test.", "bsddb.test.", "distutils.tests."
17+
18+
try:
19+
import platform
20+
platform = platform.platform()
21+
except:
22+
platform = None # unknown
23+
24+
suffixes = imp.get_suffixes()
25+
26+
def get_suffix(file):
27+
for suffix in suffixes:
28+
if file[-len(suffix[0]):] == suffix[0]:
29+
return suffix
30+
return None
31+
32+
def main():
33+
34+
path = getpath()
35+
36+
modules = {}
37+
for m in sys.builtin_module_names:
38+
modules[m] = None
39+
40+
for p in path:
41+
modules.update(getmodules(p))
42+
43+
keys = modules.keys()
44+
keys.sort()
45+
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)
53+
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
62+
63+
if not outfile:
64+
out = sys.stdout
65+
else:
66+
out = open(outfile, "w")
67+
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")
76+
77+
for k in keys:
78+
out.write(k + "\n")
79+
80+
if out is not sys.stdout:
81+
out.close()
82+
print out.name, "ok (%d modules)" % len(modules)
83+
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
109+
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
124+
125+
if __name__ == "__main__":
126+
main()

0 commit comments

Comments
 (0)