summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Brewer <fumanchu@aminus.org>2006-12-28 19:03:35 +0000
committerRobert Brewer <fumanchu@aminus.org>2006-12-28 19:03:35 +0000
commita13184e0ba909c7baeca4dceca730887b4b10b31 (patch)
tree620bc8190cfa71a640e285ca0d7c468a39aaa419
parent17d452db04b83ec51e57a3941df9a27a301487ef (diff)
downloadcherrypy-a13184e0ba909c7baeca4dceca730887b4b10b31.tar.gz
2.x backport of [1220] (Fix for #438 (autoreload.py: Server fails to start if a .pyc is imported with no corresponding .py)) and [1337] (New engine.autoreload.match attribute for filtering autoreload to a single package.)
-rw-r--r--cherrypy/lib/autoreload.py36
1 files changed, 28 insertions, 8 deletions
diff --git a/cherrypy/lib/autoreload.py b/cherrypy/lib/autoreload.py
index 75414192..b16d42f7 100644
--- a/cherrypy/lib/autoreload.py
+++ b/cherrypy/lib/autoreload.py
@@ -1,7 +1,9 @@
# autoreloading launcher
# stolen a lot from Ian Bicking's WSGIKit (www.wsgikit.org)
+import errno
import os
+import re
import sys
import time
import thread
@@ -9,28 +11,46 @@ import thread
RUN_RELOADER = True
reloadFiles = []
ignoreFiles = ['<string>']
+match = ".*"
+
def reloader_thread(freq):
mtimes = {}
- def fileattr(m):
- if hasattr(m, "__loader__"):
- if hasattr(m.__loader__, "archive"):
- return m.__loader__.archive
- return getattr(m, "__file__", None)
-
while RUN_RELOADER:
- for filename in map(fileattr, sys.modules.values()) + reloadFiles:
+ sysfiles = []
+ for k, m in sys.modules.items():
+ if re.match(match, k):
+ if hasattr(m, "__loader__"):
+ if hasattr(m.__loader__, "archive"):
+ k = m.__loader__.archive
+ k = getattr(m, "__file__", None)
+ sysfiles.append(k)
+
+ for filename in sysfiles + reloadFiles:
if filename and filename not in ignoreFiles:
+
+ orig = filename
if filename.endswith(".pyc"):
filename = filename[:-1]
+
+ # Get the last-modified time of the source file.
try:
mtime = os.stat(filename).st_mtime
- except OSError:
+ except OSError, e:
+ if orig.endswith('.pyc') and e[0] == errno.ENOENT:
+ # This prevents us from endlessly restarting if
+ # there is an old .pyc lying around after a .py
+ # file has been deleted. Note that TG's solution
+ # actually deletes the .pyc, but we just ignore it.
+ # See http://www.cherrypy.org/ticket/438.
+ continue
sys.exit(3) # force reload
+
if filename not in mtimes:
mtimes[filename] = mtime
continue
+
if mtime > mtimes[filename]:
sys.exit(3) # force reload
time.sleep(freq)