Source

AutoRebuild / README.rst

Full commit

AutoRebuild

AutoRebuild is a really simple CherryPy plugin that provides a hook point that is triggered when certain files change. It is similar to the Autoreloader plugin with the primary difference that it does not restart the engine and instead calls a function.

Usage

Here is a simple example provided in the code:

import cherrypy
from autorebuild import AutoRebuild
from subprocess import call

from myapp import MyApp

def build_css():
    call(['make', 'css'])

# Create our plugin and attach it to the engine object so we can
# configure it with the cherrypy.config
cherrypy.engine.autorebuild = AutoRebuild(cherrypy.engine)
cherrypy.engine.autorebuild.subscribe()

# Update our config with a glob expression to find some sass css
# files and call our build command defined in a make file to rebuild
# them
cherrypy.config.update({
        'engine.autorebuild.on': True,
        'engine.autorebuild.taskmap': {'*.scss': build_css}})

# Start things up
cherrypy.tree.mount(MyApp(), '/')
cherrypy.engine.start()
cherrypy.engine.block()

Pretty simple.

History

A developer asked me if there was a good way to force CherryPy to restart and rebuild some sass files when the files changed. Knowing that the Autoreloader plugin already existed it seemed trivial to implement. The immediate solution was to simply use the sass command line watcher script in a foreman procfile, but thinking about it, it seemed pretty silly when you could do it in CherryPy relatively easily and avoid having to depend on something like foreman all together.

I then realized there were tools like CodeKit people were actually selling to do this for you and it seemed ridiculous that this isn't built in.