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

Ticket #159: autoreload.py

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 from cherrypy import _cputil
9
10 RUN_RELOADER = True
11 reloadFiles = []
12    
13 def reloader_thread():
14     mtimes = {}
15     while RUN_RELOADER:
16         for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())) + reloadFiles:
17             if filename.endswith(".pyc"):
18                 filename = filename[:-1]
19             mtime = os.stat(filename).st_mtime
20             if filename not in mtimes:
21                 mtimes[filename] = mtime
22                 continue
23             if mtime > mtimes[filename]:
24                 sys.exit(3) # force reload
25         time.sleep(1)
26
27 def restart_with_reloader():
28     while True:
29         args = [sys.executable] + sys.argv
30         if sys.platform == "win32": args = ['"%s"' % arg for arg in args]
31         new_environ = os.environ.copy()
32         new_environ["RUN_MAIN"] = 'true'
33         exit_code = os.spawnve(os.P_WAIT, sys.executable,
34                                args, new_environ)
35         if exit_code != 3:
36             return exit_code
37        
38 def main(main_func):
39     if os.environ.get("RUN_MAIN") == "true":
40        
41         thread.start_new_thread(main_func, ())
42
43         try:
44             reloader_thread()
45         except KeyboardInterrupt:
46             pass
47     else:
48         try:
49             sys.exit(restart_with_reloader())
50         except KeyboardInterrupt:
51             _cputil.getSpecialFunction('_cpLogMessage')("<Ctrl-C> hit: shutting down", "HTTP")
52

Hosted by WebFaction

Log in as guest/cpguest to create tickets