Skip to content

Instantly share code, notes, and snippets.

@airtonix
Created August 11, 2013 07:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airtonix/6203834 to your computer and use it in GitHub Desktop.
Save airtonix/6203834 to your computer and use it in GitHub Desktop.
Management command that overrides the default runserver to provide `compass watch`
"""
License
=======
Do whatever you like.
Usage
=====
1. put it in your project
manage.py
base/
management/
commands/
__init__.py
runserver.py
2. point to your compass root in the project settings
base/
settings/
main.py
local.py
someotherapp/
yetanotherapp/
omgstopwiththeapps/
# local.py
from .main import ROOT_DIR
COMPASS_PROJECT_PATH = os.path.abspath(os.path.join(ROOT_DIR, "base", "static"))
"""
import os
import subprocess
import atexit
import signal
from optparse import make_option
from django.conf import settings
# from django.core.management.base import BaseCommand
from django.core.management.commands.runserver import Command as RunserverCommand
MESSAGES = {
"NotFound": ">>> Could not find a config.rb at {path}",
"StartingFor": ">>> Starting the compass watch command for path: {path}",
"StartingPid": ">>> Compass watch process on pid: {pid}",
"Closing": ">>> Closing Compass watch process for pid: {pid}"
}
class Command(RunserverCommand):
option_list = RunserverCommand.option_list + (
make_option('--compass', dest='compass_project_path', default=None,
help='Specifies the project directory for a compass project'),
)
def run(self, *args, **options):
"""Runs the server and the compass watch process"""
if settings.DEBUG and os.environ.get("RUN_MAIN") != "true":
"""RUN_MAIN Environment variable is set to None the first time the
runserver command is start, on every reload after a code change if the
option 'use_reloader' is set (by default it's) RUN_MAIN is set on 'true'.
"""
project_path = options.get('compass_project_path')
if not project_path:
project_path = getattr(settings, 'COMPASS_PROJECT_PATH', None)
if project_path and not os.path.exists(os.path.join(project_path, 'config.rb')):
self.stdout.write(MESSAGES.get("NotFound").format(path=project_path))
else:
self.start_compass_watch(project_path)
super(Command, self).run(*args, **options)
def start_compass_watch(self, path=None):
self.compass_process = subprocess.Popen(
['compass watch %s' % path],
shell=True,
stdin=subprocess.PIPE,
stdout=self.stdout,
stderr=self.stderr,
)
def kill_compass_project(pid):
self.stdout.write(self.style.NOTICE(MESSAGES.get("Closing").format(pid=self.compass_process.pid)) + "\n")
os.kill(pid, signal.SIGTERM)
atexit.register(kill_compass_project, self.compass_process.pid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment