-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreloader.py
169 lines (140 loc) · 6 KB
/
reloader.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
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
# Python Module Reloader
#
# Copyright (c) 2009, 2010, 2011 Jon Parise <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Python Module Reloader"""
try:
import builtins
except ImportError:
import __builtin__ as builtins
import imp
import sys
__author__ = 'Jon Parise <[email protected]>'
__version__ = '0.3'
__all__ = ('enable', 'disable', 'get_dependencies', 'reload')
_baseimport = builtins.__import__
_blacklist = None
_dependencies = dict()
_parent = None
# Jython doesn't have imp.reload().
if not hasattr(imp, 'reload'):
imp.reload = reload
def enable(blacklist=None):
"""Enable global module dependency tracking.
A blacklist can be specified to exclude specific modules (and their import
hierachies) from the reloading process. The blacklist can be any iterable
listing the fully-qualified names of modules that should be ignored. Note
that blacklisted modules will still appear in the dependency graph; they
will just not be reloaded.
"""
global _blacklist
builtins.__import__ = _import
if blacklist is not None:
_blacklist = frozenset(blacklist)
def disable():
"""Disable global module dependency tracking."""
global _blacklist, _parent
builtins.__import__ = _baseimport
_blacklist = None
_dependencies.clear()
_parent = None
def get_dependencies(m):
"""Get the dependency list for the given imported module."""
return _dependencies.get(m.__name__, None)
def _deepcopy_module_dict(m):
"""Make a deep copy of a module's dictionary."""
import copy
# We can't deepcopy() everything in the module's dictionary because some
# items, such as '__builtins__', aren't deepcopy()-able. To work around
# that, we start by making a shallow copy of the dictionary, giving us a
# way to remove keys before performing the deep copy.
d = vars(m).copy()
del d['__builtins__']
return copy.deepcopy(d)
def _reload(m, visited):
"""Internal module reloading routine."""
name = m.__name__
# If this module's name appears in our blacklist, skip its entire
# dependency hierarchy.
if _blacklist and name in _blacklist:
return
# Start by adding this module to our set of visited modules. We use this
# set to avoid running into infinite recursion while walking the module
# dependency graph.
visited.add(m)
# Start by reloading all of our dependencies in reverse order. Note that
# we recursively call ourself to perform the nested reloads.
deps = _dependencies.get(name, None)
if deps is not None:
for dep in reversed(deps):
if dep not in visited:
_reload(dep, visited)
# Clear this module's list of dependencies. Some import statements may
# have been removed. We'll rebuild the dependency list as part of the
# reload operation below.
try:
del _dependencies[name]
except KeyError:
pass
# Because we're triggering a reload and not an import, the module itself
# won't run through our _import hook below. In order for this module's
# dependencies (which will pass through the _import hook) to be associated
# with this module, we need to set our parent pointer beforehand.
global _parent
_parent = name
# If the module has a __reload__(d) function, we'll call it with a copy of
# the original module's dictionary after it's been reloaded.
callback = getattr(m, '__reload__', None)
if callback is not None:
d = _deepcopy_module_dict(m)
imp.reload(m)
callback(d)
else:
imp.reload(m)
# Reset our parent pointer now that the reloading operation is complete.
_parent = None
def reload(m):
"""Reload an existing module.
Any known dependencies of the module will also be reloaded.
If a module has a __reload__(d) function, it will be called with a copy of
the original module's dictionary after the module is reloaded."""
_reload(m, set())
def _import(name, globals=None, locals=None, fromlist=None, level=-1):
"""__import__() replacement function that tracks module dependencies."""
# Track our current parent module. This is used to find our current place
# in the dependency graph.
global _parent
parent = _parent
_parent = name
# Perform the actual import using the base import function.
base = _baseimport(name, globals, locals, fromlist, level)
# If this is a nested import for a reloadable (source-based) module, we
# append ourself to our parent's dependency list.
if parent is not None:
# We get the module directly from sys.modules because the import
# function only returns the top-level module reference for a nested
# import statement (e.g. 'package' for `import package.module`).
m = sys.modules.get(name, None)
if m is not None and hasattr(m, '__file__'):
l = _dependencies.setdefault(parent, [])
l.append(m)
# Lastly, we always restore our global _parent pointer.
_parent = parent
return base