Commit f3fa489d by Kálmán Viktor

circle: create a watch command

usage: ./manage.py watch
parent 8d8e851e
import subprocess import subprocess
import os import os
import pyinotify import pyinotify
from django.core.management.base import BaseCommand
from django.conf import settings from django.conf import settings
STATIC_FILES = u'--include-path={}'.format(':'.join(settings.STATICFILES_DIRS)) STATIC_FILES = u'--include-path={}'.format(':'.join(settings.STATICFILES_DIRS))
...@@ -26,29 +28,30 @@ class LessUtils(object): ...@@ -26,29 +28,30 @@ class LessUtils(object):
print("Successfully compiled:\n%s\n->\n%s" % ( print("Successfully compiled:\n%s\n->\n%s" % (
less_pathname, css_pathname)) less_pathname, css_pathname))
@staticmethod
if __name__ == "__main__": def initial_compile():
# for first run compile everything """ Walks through the project looking for LESS files
print("Initial LESS compiles") and compiles them into CSS.
current_dir = os.path.dirname(os.path.realpath(__file__)) """
for root, dirs, files in os.walk(settings.SITE_ROOT):
for root, dirs, files in os.walk("/home/cloud/circle/circle"):
for f in files: for f in files:
if not f.endswith(".less"): if not f.endswith(".less"):
continue continue
relpath = os.path.relpath(root, current_dir) relpath = os.path.relpath(root, settings.SITE_ROOT)
if relpath.startswith(("static_collected", "bower_components")): if relpath.startswith(("static_collected",
"bower_components")):
continue continue
less_pathname = "%s/%s" % (root, f) less_pathname = "%s/%s" % (root, f)
css_pathname = LessUtils.less_path_to_css_path(less_pathname) css_pathname = LessUtils.less_path_to_css_path(less_pathname)
LessUtils.compile_less(less_pathname, css_pathname) LessUtils.compile_less(less_pathname, css_pathname)
print("\n%s\n" % ("=" * 30)) @staticmethod
print("End of initial LESS compiles\n") def start_watch():
""" Watches for changes in LESS files recursively from the
# after first run watch less files project's root and compiles the files
"""
wm = pyinotify.WatchManager() wm = pyinotify.WatchManager()
class EventHandler(pyinotify.ProcessEvent): class EventHandler(pyinotify.ProcessEvent):
...@@ -61,5 +64,19 @@ if __name__ == "__main__": ...@@ -61,5 +64,19 @@ if __name__ == "__main__":
handler = EventHandler() handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler) notifier = pyinotify.Notifier(wm, handler)
wm.add_watch(current_dir, pyinotify.IN_MODIFY, rec=True) wm.add_watch(settings.SITE_ROOT, pyinotify.IN_MODIFY, rec=True)
notifier.loop() notifier.loop()
class Command(BaseCommand):
help = "Compiles all LESS files then watches for changes."
def handle(self, *args, **kwargs):
# for first run compile everything
print("Initial LESS compiles")
LessUtils.initial_compile()
print("\n%s\n" % ("=" * 30))
print("End of initial LESS compiles\n")
# after first run watch less files
LessUtils.start_watch()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment