Jakub Kuszneruk
Jakub Kuszneruk

Reputation: 1240

Django running bash script when less file is modified

I wrote bash script converting *.less files into *.css using lessc. Unfortunately run function from BaseRunserverCommand restarts server only, when *.py files was modified. How can I solve this?

Upvotes: 1

Views: 292

Answers (2)

Jakub Kuszneruk
Jakub Kuszneruk

Reputation: 1240

Correct overriding code_changed() function seemed a bit hard,so I decided to write my own script converting *.less files which was modified or had no *.css file. This script is running in reloader_thread():

import sys, time
from subprocess import call

from django.conf import settings
from django.utils import autoreload

from django.core.management.commands.runserver import Command

def convert_less():
    status = call([settings.CONVERT_LESS, settings.LESS_DIR, settings.CSS_DIR])
    if status != 0:
        exit(1)

def reloader_thread():
    autoreload.ensure_echo_on()
    while autoreload.RUN_RELOADER:
        convert_less()
        if autoreload.code_changed():
            sys.exit(3) # force reload
        time.sleep(1)

autoreload.reloader_thread = reloader_thread

Tadeck, thank you for clue :)

I suppose that this problem could be also solved by editing BaseCommand.option_list

Upvotes: 1

Tadeck
Tadeck

Reputation: 137420

You can write your own command inheriting from django.core.management.commands.runserver.Command, overriding run() method with your own, which would use your own autoreload:

def run(self, *args, **options):
    """
    Runs the server, using the autoreloader if needed
    """
    use_reloader = options.get('use_reloader')

    if use_reloader:
        autoreload.main(self.inner_run, args, options)
    else:
        self.inner_run(*args, **options)

Your own instance of autoreload, however, would need to override only code_changed() function to take also *.less and *.css (or more) files into account.

Upvotes: 2

Related Questions