Skip to content

Commit

Permalink
Fixed #8413: Better autoreload
Browse files Browse the repository at this point in the history
  • Loading branch information
buriy committed Jun 6, 2010
1 parent 5e44225 commit 7c2c45e
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions django/utils/autoreload.py
Expand Up @@ -27,9 +27,7 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os, sys, time, imp
import os, sys, time

try: try:
import thread import thread
except ImportError: except ImportError:
Expand All @@ -42,15 +40,52 @@
except ImportError: except ImportError:
pass pass


try:
set
except NameError:
from sets import Set as set # For Python 2.3


RUN_RELOADER = True RUN_RELOADER = True


_mtimes = {} _mtimes = {}
_win = (sys.platform == "win32") _win = (sys.platform == "win32")


def get_loaded_modules():
return filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values()))

class ModuleLoaderHook:
__caught = set()
def __init__(self):
before = get_loaded_modules()
ModuleLoaderHook.__caught = set(before)

def tracked(self):
return list(self.__caught)

def find_module(self, name, path=None):
try:
if path:
name = name.split('.')[-1]
file, filename, _info = imp.find_module(name, path)
self.__caught.add(filename)
if file:
file.close()
except:
pass
return None #as it was not found

loader = None
def hook_imports():
global loader
loader = ModuleLoaderHook()
sys.meta_path.insert(0, loader)

def code_changed(): def code_changed():
global _mtimes, _win global _mtimes, _win
for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): files = get_loaded_modules()
if loader:
files = loader.tracked()
for filename in files:
if filename.endswith(".pyc") or filename.endswith(".pyo"): if filename.endswith(".pyc") or filename.endswith(".pyo"):
filename = filename[:-1] filename = filename[:-1]
if not os.path.exists(filename): if not os.path.exists(filename):
Expand Down Expand Up @@ -107,6 +142,7 @@ def jython_reloader(main_func, args, kwargs):




def main(main_func, args=None, kwargs=None): def main(main_func, args=None, kwargs=None):
hook_imports()
if args is None: if args is None:
args = () args = ()
if kwargs is None: if kwargs is None:
Expand Down

0 comments on commit 7c2c45e

Please sign in to comment.