/usr/share/pyshared/mx/Misc/PackageTools.py is in python-egenix-mxtools 3.2.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | """ PackageTools - A set of tools to aid working with packages.
Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2010, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
__version__ = '0.4.0'
import os,types,sys,re,imp,__builtin__
import mx.Tools.NewBuiltins
# RE to identify Python modules
suffixes = projection(imp.get_suffixes(),0)
module_name = re.compile('(.*)(' + '|'.join(suffixes) + ')$')
initmodule_name = re.compile('__init__(' + '|'.join(suffixes) + ')$')
initmodule_names = []
for suffix in suffixes:
initmodule_names.append('__init__' + suffix)
def find_packages(dir=os.curdir, files_only=0, recursive=0, ignore_modules=0,
pkgbasename='', pkgdict=None,
isdir=os.path.isdir,exists=os.path.exists,
isfile=os.path.isfile,join=os.path.join,listdir=os.listdir,
module_name=module_name,initmodule_name=initmodule_name):
""" Return a list of package names found in dir.
Packages are Python modules and subdirectories that provide an
__init__ module. The .py extension is removed from the
files. The __init__ modules are not considered being seperate
packages.
If files_only is true, only Python files are included in the
search (subdirectories are *not* taken into account). If
ignore_modules is true (default is false), modules are
ignored. If recursive is true the search recurses into package
directories.
pkgbasename and pkgdict are only used during recursion.
"""
l = listdir(dir)
if pkgdict is None:
pkgdict = {}
if files_only:
for filename in l:
m = module_name.match(filename)
if m is not None and \
m.group(1) != '__init__':
pkgdict[pkgbasename + m.group(1)] = 1
else:
for filename in l:
path = join(dir, filename)
if isdir(path):
# Check for __init__ module(s)
for name in initmodule_names:
if isfile(join(path, name)):
pkgname = pkgbasename + filename
pkgdict[pkgname] = 1
if recursive:
find_packages(path,
recursive=1,
pkgbasename=pkgname + '.',
pkgdict=pkgdict)
break
elif not ignore_modules:
m = module_name.match(filename)
if m is not None and \
m.group(1) != '__init__':
pkgdict[pkgbasename + m.group(1)] = 1
return pkgdict.keys()
def find_subpackages(package, recursive=0,
splitpath=os.path.split):
""" Assuming that package points to a loaded package module, this
function tries to identify all subpackages of that package.
Subpackages are all Python files included in the same
directory as the module plus all subdirectories having an
__init__.py file. The modules name is prepended to all
subpackage names.
The module location is found by looking at the __file__
attribute that non-builtin modules define. The function uses
the __all__ attribute from the package __init__ module if
available.
If recursive is true (default is false), then subpackages of
subpackages are recursively also included in the search.
"""
if not recursive:
# Try the __all__ attribute...
try:
subpackages = list(package.__all__)
except (ImportError, AttributeError):
# Did not work, then let's try to find the subpackages by looking
# at the directory where package lives...
subpackages = find_packages(package.__path__[0], recursive=recursive)
else:
# XXX Recursive search does not support the __all__ attribute
subpackages = find_packages(package.__path__[0], recursive=recursive)
basename = package.__name__ + '.'
for i,name in irange(subpackages):
subpackages[i] = basename + name
return subpackages
def _thismodule(upcount=1,
exc_info=sys.exc_info,trange=trange):
""" Returns the module object that the callee is calling from.
upcount can be given to indicate how far up the execution
stack the function is supposed to look (1 == direct callee, 2
== callee of callee, etc.).
"""
try:
1/0
except:
frame = exc_info()[2].tb_frame
for i in trange(upcount):
frame = frame.f_back
name = frame.f_globals['__name__']
del frame
return sys.modules[name]
def _module_loader(name, locals, globals, sysmods, errors='strict',
importer=__import__, reloader=reload, from_list=['*']):
""" Internal API for loading a module
"""
if not sysmods.has_key(name):
is_new = 1
else:
is_new = 0
try:
mod = importer(name, locals, globals, from_list)
if reload and not is_new:
mod = reloader(mod)
except KeyboardInterrupt:
# Pass through; SystemExit will be handled by the error handler
raise
except Exception, why:
if errors == 'ignore':
pass
elif errors == 'strict':
raise
elif callable(errors):
errors(name, sys.exc_info()[0], sys.exc_info()[1])
else:
raise ValueError,'unknown errors value'
else:
return mod
return None
def import_modules(modnames,module=None,errors='strict',reload=0,
thismodule=_thismodule):
""" Import all modules given in modnames into module.
module defaults to the caller's module. modnames may contain
dotted package names.
If errors is 'strict' (default), then ImportErrors and
SyntaxErrors are raised. If set to 'ignore', they are silently
ignored. If errors is a callable object, then it is called
with arguments (modname, errorclass, errorvalue). If the
handler returns, processing continues.
If reload is true (default is false), all already modules
among the list will be forced to reload.
"""
if module is None:
module = _thismodule(2)
locals = module.__dict__
sysmods = sys.modules
for name in modnames:
mod = _module_loader(name, locals, locals, sysmods, errors=errors)
if mod is not None:
locals[name] = mod
def load_modules(modnames,locals=None,globals=None,errors='strict',reload=0):
""" Imports all modules in modnames using the given namespaces and returns
list of corresponding module objects.
If errors is 'strict' (default), then ImportErrors and
SyntaxErrors are raised. If set to 'ignore', they are silently
ignored. If errors is a callable object, then it is called
with arguments (modname, errorclass, errorvalue). If the
handler returns, processing continues.
If reload is true (default is false), all already modules
among the list will be forced to reload.
"""
modules = []
append = modules.append
sysmods = sys.modules
for name in modnames:
mod = _module_loader(name, locals, globals, sysmods, errors=errors)
if mod is not None:
append(mod)
return modules
def import_subpackages(module, reload=0, recursive=0,
import_modules=import_modules,
find_subpackages=find_subpackages):
""" Does a subpackages scan using find_subpackages(module) and then
imports all submodules found into module.
The module location is found by looking at the __file__
attribute that non-builtin modules define. The function uses
the __all__ attribute from the package __init__ module if
available.
If reload is true (default is false), all already modules
among the list will be forced to reload.
"""
import_modules(find_subpackages(module, recursive=recursive),
module, reload=reload)
def load_subpackages(module, locals=None, globals=None, errors='strict', reload=0,
recursive=0,
load_modules=load_modules,
find_subpackages=find_subpackages):
""" Same as import_subpackages but with load_modules
functionality, i.e. imports the modules and also returns a list of
module objects.
If errors is 'strict' (default), then ImportErrors are
raised. If set to 'ignore', they are silently ignored.
If reload is true (default is false), all already modules
among the list will be forced to reload.
"""
return load_modules(find_subpackages(module, recursive=recursive),
locals, globals,
errors=errors, reload=reload)
def modules(names,
extract=extract):
""" Converts a list of module names into a list of module objects.
The modules must already be loaded.
"""
return extract(sys.modules, names)
def package_modules(pkgname):
""" Returns a list of all modules belonging to the package with the
given name.
The package must already be loaded. Only the currently
registered modules are included in the list.
"""
match = pkgname + '.'
match_len = len(match)
mods = [sys.modules[pkgname]]
for k,v in sys.modules.items():
if k[:match_len] == match and v is not None:
mods.append(v)
return mods
def find_classes(mods,baseclass=None,annotated=0,
ClassType=types.ClassType,issubclass=issubclass):
""" Find all subclasses of baseclass or simply all classes (if baseclass
is None) defined by the module objects in list mods.
If annotated is true the returned list will contain tuples
(module_object,name,class_object) for each class found where
module_object is the module where the class is defined.
"""
classes = []
for mod in mods:
for name,obj in mod.__dict__.items():
if type(obj) is ClassType:
if baseclass and not issubclass(obj,baseclass):
continue
if annotated:
classes.append((mod, name, obj))
else:
classes.append(obj)
return classes
def find_instances(mods,baseclass,annotated=0,
InstanceType=types.InstanceType,issubclass=issubclass):
""" Find all instances of baseclass defined by the module objects
in list mods.
If annotated is true the returned list will contain tuples
(module_object,name,instances_object) for each instances found where
module_object is the module where the instances is defined.
"""
instances = []
for mod in mods:
for name,obj in mod.__dict__.items():
if isinstance(obj,baseclass):
if annotated:
instances.append((mod,name,obj))
else:
instances.append(obj)
return instances
|