4
# Modified to only exclude module imports from a given module.
6
# Copyright 2004 Toby Dickenson
8
# Permission is hereby granted, free of charge, to any person obtaining
9
# a copy of this software and associated documentation files (the
10
# "Software"), to deal in the Software without restriction, including
11
# without limitation the rights to use, copy, modify, merge, publish,
12
# distribute, sublicense, and/or sell copies of the Software, and to
13
# permit persons to whom the Software is furnished to do so, subject
14
# to the following conditions:
16
# The above copyright notice and this permission notice shall be included
17
# in all copies or substantial portions of the Software.
19
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
import os, sys, pprint
31
class mymf(modulefinder.ModuleFinder):
32
def __init__(self,*args,**kwargs):
35
self._last_caller = None
36
modulefinder.ModuleFinder.__init__(self, *args, **kwargs)
38
def import_hook(self, name, caller=None, fromlist=None, level=-1):
39
old_last_caller = self._last_caller
41
self._last_caller = caller
42
return modulefinder.ModuleFinder.import_hook(self, name, caller,
45
self._last_caller = old_last_caller
47
def import_module(self, partnam, fqname, parent):
48
m = modulefinder.ModuleFinder.import_module(self,
49
partnam, fqname, parent)
50
if m is not None and self._last_caller:
51
caller = self._last_caller.__name__
53
caller = caller[:caller.index('.')]
56
callee = callee[:callee.index('.')]
57
#print "XXX last_caller", caller, "MOD", callee
58
#self._depgraph.setdefault(self._last_caller.__name__,{})[r.__name__] = 1
59
#if caller in ('pdb', 'doctest') or callee in ('pdb', 'doctest'):
60
# print caller, "-->", callee
62
self._depgraph.setdefault(caller,{})[callee] = 1
65
def find_module(self, name, path, parent=None):
66
if parent is not None:
67
# assert path is not None
68
fullname = parent.__name__+'.'+name
69
elif name == "__init__":
70
fullname = os.path.basename(path[0])
74
caller = self._last_caller.__name__
75
if fullname in excluded_imports.get(caller, []):
76
#self.msgout(3, "find_module -> Excluded", fullname)
77
raise ImportError(name)
79
if fullname in self.excludes:
80
#self.msgout(3, "find_module -> Excluded", fullname)
81
raise ImportError(name)
84
if name in sys.builtin_module_names:
85
return (None, None, ("", "", imp.C_BUILTIN))
88
return imp.find_module(name, path)
90
def load_module(self, fqname, fp, pathname, file_info):
91
suffix, mode, type = file_info
92
m = modulefinder.ModuleFinder.load_module(self, fqname,
93
fp, pathname, file_info)
95
self._types[m.__name__] = type
98
def load_package(self, fqname, pathname):
99
m = modulefinder.ModuleFinder.load_package(self, fqname,pathname)
101
self._types[m.__name__] = imp.PKG_DIRECTORY
104
def reduce_depgraph(dg):
107
# guarded imports, which don't need to be included in python-minimal
109
'argparse': set(('gettext',)),
110
'codecs': set(('encodings',)),
111
'collections': set(('cPickle', 'pickle', 'doctest')),
112
'copy': set(('reprlib',)),
113
'functools': set(('_dummy_thread',)),
114
'hashlib': set(('logging',)),
115
#'hashlib': set(('_hashlib', '_md5', '_sha', '_sha256','_sha512',)),
116
'heapq': set(('doctest',)),
117
'io': set(('_dummy_thread',)),
118
'logging': set(('multiprocessing',)),
119
'os': set(('nt', 'ntpath', 'os2', 'os2emxpath', 'mac', 'macpath',
120
'riscos', 'riscospath', 'riscosenviron')),
121
'optparse': set(('gettext',)),
122
'pickle': set(('argparse', 'doctest', 'pprint')),
123
'platform': set(('plistlib', 'tempfile')),
124
#'socket': set(('_ssl',)),
125
'subprocess': set(('threading',)),
126
'sysconfig': set(('pprint',)),
133
opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
134
except getopt.error as msg:
149
addpath = addpath + a.split(os.pathsep)
156
path = addpath + path
159
print("version:", sys.version)
162
print(" ", repr(item))
164
#exclude = ['__builtin__', 'sys', 'os']
166
mf = mymf(path, debug, exclude)
170
depgraph = reduce_depgraph(mf._depgraph)
172
pprint.pprint({'depgraph':mf._depgraph, 'types':mf._types})
174
if __name__=='__main__':