Django

Code

Show
Ignore:
Timestamp:
08/08/08 12:54:35 (5 months ago)
Author:
mtredinnick
Message:

Made auto-reloading for the dev server a little more friendly in the Jython
case. Patch from Leo Soto. Fixed #8147.

In passing, also corrected a typo when reloading .pyo files. Fixed #8157.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/autoreload.py

    r4265 r8235  
    4646RUN_RELOADER = True 
    4747 
     48_mtimes = {} 
     49_win = (sys.platform == "win32") 
     50 
     51def code_changed(): 
     52    global _mtimes, _win 
     53    for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): 
     54        if filename.endswith(".pyc") or filename.endswith(".pyo"): 
     55            filename = filename[:-1] 
     56        if not os.path.exists(filename): 
     57            continue # File might be in an egg, so it can't be reloaded. 
     58        stat = os.stat(filename) 
     59        mtime = stat.st_mtime 
     60        if _win: 
     61            mtime -= stat.st_ctime 
     62        if filename not in _mtimes: 
     63            _mtimes[filename] = mtime 
     64            continue 
     65        if mtime != _mtimes[filename]: 
     66            _mtimes = {} 
     67            return True 
     68    return False 
     69 
    4870def reloader_thread(): 
    49     mtimes = {} 
    50     win = (sys.platform == "win32") 
    5171    while RUN_RELOADER: 
    52         for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): 
    53             if filename.endswith(".pyc") or filename.endswith("*.pyo"): 
    54                 filename = filename[:-1] 
    55             if not os.path.exists(filename): 
    56                 continue # File might be in an egg, so it can't be reloaded. 
    57             stat = os.stat(filename) 
    58             mtime = stat.st_mtime 
    59             if win: 
    60                 mtime -= stat.st_ctime 
    61             if filename not in mtimes: 
    62                 mtimes[filename] = mtime 
    63                 continue 
    64             if mtime != mtimes[filename]: 
    65                 sys.exit(3) # force reload 
     72        if code_changed(): 
     73            sys.exit(3) # force reload 
    6674        time.sleep(1) 
    6775 
     
    7785            return exit_code 
    7886 
    79 def main(main_func, args=None, kwargs=None): 
     87def python_reloader(main_func, args, kwargs): 
    8088    if os.environ.get("RUN_MAIN") == "true": 
    81         if args is None: 
    82             args = () 
    83         if kwargs is None: 
    84             kwargs = {} 
    8589        thread.start_new_thread(main_func, args, kwargs) 
    8690        try: 
     
    9397        except KeyboardInterrupt: 
    9498            pass 
     99 
     100def jython_reloader(main_func, args, kwargs): 
     101    from _systemrestart import SystemRestart 
     102    thread.start_new_thread(main_func, args) 
     103    while True: 
     104        if code_changed(): 
     105            raise SystemRestart 
     106        time.sleep(1) 
     107 
     108 
     109def main(main_func, args=None, kwargs=None): 
     110    if args is None: 
     111        args = () 
     112    if kwargs is None: 
     113        kwargs = {} 
     114    if sys.platform.startswith('java'): 
     115        reloader = jython_reloader 
     116    else: 
     117        reloader = python_reloader 
     118    reloader(main_func, args, kwargs) 
     119