Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.1/cherrypy/lib/autoreload.py

Revision 711 (checked in by fumanchu, 3 years ago)

autoreload now calls server.stop() on interrupt. No test for it though. :(

Line 
1 # autoreloading launcher
2 # stolen a lot from Ian Bicking's WSGIKit (www.wsgikit.org)
3
4 import os
5 import sys
6 import time
7 import thread
8
9 RUN_RELOADER = True
10 reloadFiles = []
11    
12 def reloader_thread():
13     mtimes = {}
14    
15     def fileattr(m):
16         return getattr(m, "__file__", None)
17    
18     while RUN_RELOADER:
19         for filename in map(fileattr, sys.modules.values()) + reloadFiles:
20             if filename:
21                 if filename.endswith(".pyc"):
22                     filename = filename[:-1]
23                 try:
24                     mtime = os.stat(filename).st_mtime
25                 except OSError:
26                     sys.exit(3) # force reload
27                 if filename not in mtimes:
28                     mtimes[filename] = mtime
29                     continue
30                 if mtime > mtimes[filename]:
31                     sys.exit(3) # force reload
32         time.sleep(1)
33
34 def restart_with_reloader():
35     while True:
36         args = [sys.executable] + sys.argv
37         if sys.platform == "win32":
38             args = ['"%s"' % arg for arg in args]
39         new_environ = os.environ.copy()
40         new_environ["RUN_MAIN"] = 'true'
41         exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
42         if exit_code != 3:
43             return exit_code
44
45 def main(main_func, args=None, kwargs=None):
46     if os.environ.get("RUN_MAIN") == "true":
47        
48         if args is None:
49             args = ()
50         if kwargs is None:
51             kwargs = {}
52         thread.start_new_thread(main_func, args, kwargs)
53        
54         # If KeyboardInterrupt is raised within reloader_thread,
55         # let it propagate out to the caller.
56         reloader_thread()
57     else:
58         # If KeyboardInterrupt is raised within restart_with_reloader,
59         # let it propagate out to the caller.
60         sys.exit(restart_with_reloader())
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets