Skip to content

Instantly share code, notes, and snippets.

@melinath
Created June 1, 2011 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melinath/1003261 to your computer and use it in GitHub Desktop.
Save melinath/1003261 to your computer and use it in GitHub Desktop.
Class-based reloading
#!/usr/bin/env python
import subprocess
import sys
from reloader.reloaders import FileChangeReloader
def auto_compile(path, filetypes, cmd):
filetypes = filetypes.split(',')
reloader = FileChangeReloader(path, filetypes=filetypes)
reloader(lambda: subprocess.call(cmd, cwd=path))
if __name__ == '__main__':
if len(sys.argv) < 4:
print >> sys.stderr, "Command line error: missing argument(s)"
sys.exit(1)
path = sys.argv[1]
ftypes = sys.argv[2]
cmd = sys.argv[3:]
auto_compile(path, ftypes, cmd)
# Autoreloading launcher.
# Modified from django's setup.py file (http://djangoproject.com)
# Borrowed from Peter Hunt and the CherryPy project (http://www.cherrypy.org).
# Some taken from Ian Bicking's Paste (http://pythonpaste.org/).
#
# Portions copyright (c) 2004, CherryPy Team (team@cherrypy.org)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the CherryPy Team nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# 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
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os, sys, time
try:
import thread
except ImportError:
import dummy_thread as thread
# This import does nothing, but it's necessary to avoid some race conditions
# in the threading module. See http://code.djangoproject.com/ticket/2330 .
try:
import threading
except ImportError:
pass
try:
import termios
except ImportError:
termios = None
RUN_RELOADER = True
WIN = (sys.platform == "win32")
def ensure_echo_on():
if termios:
fd = sys.stdin
if fd.isatty():
attr_list = termios.tcgetattr(fd)
if not attr_list[3] & termios.ECHO:
attr_list[3] |= termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, attr_list)
def restart_with_reloader():
while True:
args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv
if sys.platform == "win32":
args = ['"%s"' % arg for arg in args]
new_environ = os.environ.copy()
new_environ["RUN_MAIN"] = 'true'
exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
if exit_code != 3:
return exit_code
class BaseChangeReloader(object):
"""A base class for reloaders based on changes."""
sleep = 1
def __init__(self, sleep=None):
if sleep is not None:
self.sleep = sleep
def changed(self):
raise NotImplementedError
def reloader_thread(self):
ensure_echo_on()
while RUN_RELOADER:
if self.changed():
sys.exit(3) # force reload
time.sleep(self.sleep)
def python_reloader(self, main_func, args, kwargs):
if os.environ.get("RUN_MAIN") == "true":
thread.start_new_thread(main_func, args, kwargs)
try:
self.reloader_thread()
except KeyboardInterrupt:
pass
else:
try:
sys.exit(restart_with_reloader())
except KeyboardInterrupt:
pass
def jython_reloader(self, main_func, args, kwargs):
from _systemrestart import SystemRestart
thread.start_new_thread(main_func, args)
while True:
if self.changed():
raise SystemRestart
time.sleep(self.sleep)
def __call__(self, main_func, args=None, kwargs=None):
if args is None:
args = ()
if kwargs is None:
kwargs = {}
if sys.platform.startswith('java'):
reloader = self.jython_reloader
else:
reloader = self.python_reloader
reloader(main_func, args, kwargs)
class PythonChangeReloader(BaseChangeReloader):
def __init__(self, sleep=None):
self._mtimes = {}
BaseChangeReloader.__init__(self, sleep=sleep)
def file_changed(self, filename):
stat = os.stat(filename)
mtime = stat.st_mtime
if WIN:
mtime -= stat.st_ctime
if filename not in self._mtimes:
self._mtimes[filename] = mtime
return
if mtime != self._mtimes[filename]:
self._mtimes = {}
return True
def changed(self):
for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
if filename.endswith(".pyc") or filename.endswith(".pyo"):
filename = filename[:-1]
if not os.path.exists(filename):
continue # File might be in an egg, so it can't be reloaded.
if self.file_changed(filename):
return True
return False
class FileChangeReloader(PythonChangeReloader):
ignore_dirname = lambda s, d: d.startswith('.') or d.startswith('_')
def __init__(self, dirname, filetypes=None, ignore_dirname=None, sleep=None):
self.dirname = dirname
self.filetypes = filetypes
if ignore_dirname is not None:
self.ignore_dirname = ignore_dirname
PythonChangeReloader.__init__(self, sleep=sleep)
def changed(self):
for dirpath, dirnames, filenames in os.walk(self.dirname):
# Ignore dirnames that start with '.' or '_'
for i, dirname in enumerate(dirnames):
if self.ignore_dirname(dirname):
del dirnames[i]
if filenames:
for f in filenames:
f = os.path.join(dirpath, f)
if self.filetypes is not None:
for filetype in self.filetypes:
if f.endswith(filetype):
if self.file_changed(f):
return True
break
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment