forked from pib/enstaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
108 lines (88 loc) · 2.92 KB
/
hooks.py
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
import os
import sys
import imp
from os.path import isfile, join, splitext
EXT_INFO_MAP = {
'.py': ('.py', 'U', imp.PY_SOURCE),
'.pyw': ('.pyw', 'U', imp.PY_SOURCE),
'.pyc': ('.pyc', 'rb', imp.PY_COMPILED),
'.pyo': ('.pyo', 'rb', imp.PY_COMPILED),
'.pyd': ('.pyd', 'rb', imp.C_EXTENSION),
'.so': ('.so', 'rb', imp.C_EXTENSION),
'': ('', '', imp.PKG_DIRECTORY),
}
class PackageRegistry(object):
def __init__(self, registry={}):
self.registry = registry
self._path = None
def find_module(self, fullname, path=None):
"""
Find fullname in the registry if it is recorded directly or if any
of the partial names are recorded.
That is, if there is a registry entry for scipy.stats then
scipy.stats.distributions will be found underneath the path tree
where it is located.
"""
# sys.stderr.write("# registry find_module: %s (path=%s)\n" %
# (fullname, path))
try:
self._path = self.registry[fullname]
return self
except KeyError:
return None
def load_module(self, fullname):
"""
uses imp.load_module
"""
# print "Called load_module", fullname
mod = sys.modules.get(fullname)
if mod:
return mod
if sys.flags.verbose:
sys.stderr.write("# registry load_module: %s\n" % fullname)
assert self._path, "_path=%r" % self._path
info = EXT_INFO_MAP[splitext(self._path)[-1]]
if info[1]:
f = open(self._path, info[1])
else:
f = None
try:
mod = imp.load_module(fullname, f, self._path, info)
finally:
if f is not None:
f.close()
return mod
def update(path):
"""
updates the registry and sys.path given the path to a registry file
The registry file should have one entry per package and additional
entries for any individual files that don't fit under any package
(essentially) anything that is top level.
In addition, it may contain "-pth-" entries, which are simply appended
to sys.path.
"""
registry = {}
for line in open(path):
line = line.strip()
if not line or line.startswith('#'):
continue
k, v = line.split(None, 1)
if k == '-pth-':
if v not in sys.path:
sys.path.insert(0, v)
else:
registry[k] = v
sys.meta_path.insert(0, PackageRegistry(registry))
def main():
"""
called from main() in site.py
"""
path = os.environ.get('EPDREGISTRY', join(sys.prefix, 'registry.txt'))
if sys.flags.verbose:
sys.stderr.write("# registry file: %s\n" % path)
if isfile(path):
update(path)
elif sys.flags.verbose:
sys.stderr.write("# WARNING: registry file does not exist\n")
if __name__ == '__main__':
main()