Repository navigation
Expand file tree
/
Copy pathstdmodules.py
More file actions
193 lines (149 loc) · 5.12 KB
/
Copy pathstdmodules.py
File metadata and controls
193 lines (149 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.
'''
OVERVIEW:
This script outputs a log file showing standard CPython modules which cannot be
imported from IronPython.
USAGE:
ipy stdmodules.py C:\Python25
'''
from sys import argv, path
import nt
import sys
#--GLOBALS---------------------------------------------------------------------
LOG_FILE_BUSTED = open("STD_MODULES_BROKEN_UNDER_IP.log", "w")
LOG_FILE_OK = open("STD_MODULES_OK_UNDER_IP.log", "w")
STDOUT_FAKE = open(__file__ + ".stdout.log", "w")
STDOUT_BAK = sys.stdout
FILE_LIST = [LOG_FILE_BUSTED, LOG_FILE_OK, STDOUT_FAKE]
VERBOSE = __name__=="__main__"
BROKEN_LIST = []
BLACKLIST = ['test', 'idlelib'] #Importing all tests takes too long
#--FUNCTIONS-------------------------------------------------------------------
def import_helper(mod_name):
'''
Helper function used to temporarily override stdout before importing
a module.
'''
try:
sys.stdout = STDOUT_FAKE
__import__(mod_name)
finally:
sys.stdout = STDOUT_BAK
def is_package(dir_name):
'''
Returns True if dir_name is actually a Python package in the current
working directory.
'''
#*.py, *.pyd, etc
if "." in dir_name:
return False
#Make sure it exists
try:
if not nt.stat(dir_name): return False
except:
return False
#Make sure it has an __init__.py
try:
if "__init__.py" not in nt.listdir(nt.getcwd() + "\\" + dir_name):
return False
except:
return False
return True
def log_broken(name, e):
global BROKEN_LIST
if VERBOSE:
print name, "FAILED"
print >> LOG_FILE_BUSTED, "----------------------------------------------------------------"
print >> LOG_FILE_BUSTED, "--", name
if hasattr(e, "clsException"):
print >> LOG_FILE_BUSTED, e.clsException
else:
print >> LOG_FILE_BUSTED, e
temp_name = name.replace(".", "\\")
try:
if nt.stat(CPY_LIB_DIR + "\\" + temp_name + ".py"):
BROKEN_LIST.append("/Lib/" + temp_name.replace("\\", "/") + ".py")
except:
pass
try:
if nt.stat(CPY_LIB_DIR + "\\" + temp_name):
BROKEN_LIST.append("/Lib/" + temp_name.replace("\\", "/"))
BROKEN_LIST.append("/Lib/" + temp_name.replace("\\", "/") + "/__init__.py")
except:
pass
def log_ok(name):
if VERBOSE:
print name, "PASSED"
print >> LOG_FILE_OK, name
def check_package(package_name):
'''
Checks all subpackages and modules in the package_name package.
'''
cwd = nt.getcwd()
if cwd==CPY_LIB_DIR:
root_name = package_name
else:
root_name = cwd.split(CPY_DIR + "\\Lib\\")[1].replace("\\", ".") + "." + package_name
#First check that the root package can be imported
try:
import_helper(package_name)
log_ok(root_name)
except (Exception, SystemExit), e:
log_broken(root_name, e)
# no sense continuing
return
#Next examine subpackages and modules
nt.chdir(cwd + "\\" + package_name)
for x in nt.listdir("."):
if x.endswith(".py") and x not in ("__init__.py", '__main__.py'):
x = x.split(".py", 1)[0]
mod_name = nt.getcwd().split(CPY_DIR + "\\Lib\\")[1] + "\\" + x
mod_name = mod_name.replace("\\", ".")
try:
import_helper(mod_name)
log_ok(mod_name)
except (Exception, SystemExit), e:
log_broken(mod_name, e)
elif is_package(x) and not x.startswith('test'):
check_package(x)
nt.chdir(cwd)
#--MAIN------------------------------------------------------------------------
def main(cpy_dir):
global CPY_DIR
global CPY_LIB_DIR
CPY_DIR = cpy_dir
CPY_LIB_DIR = cpy_dir + "\\Lib" #E.g., C:\Python25\Lib
path.append(CPY_LIB_DIR)
path.insert(0, ".")
print "------------------------------------------------------------------------"
print "--PACKAGES"
cwd = nt.getcwd()
nt.chdir(CPY_LIB_DIR)
for pack_name in nt.listdir(CPY_LIB_DIR):
if not is_package(pack_name):
continue
elif pack_name in BLACKLIST:
continue
check_package(pack_name)
print
nt.chdir(cwd)
print "------------------------------------------------------------------------"
print "--MODULES"
for x in nt.listdir(CPY_LIB_DIR):
if x.endswith(".py"):
mod_name = x.split(".py", 1)[0]
else:
continue
try:
import_helper(mod_name)
log_ok(mod_name)
except Exception, e:
log_broken(mod_name, e)
#Cleanup
for f in FILE_LIST:
f.close()
return BROKEN_LIST
if __name__=="__main__":
main(sys.argv[1])