django management command + yui compressor
Can I call yui compressor: java -jar yuicompressor-x.y.z.jar [options] [input file] from a django management command and if so how do I go about doing it?
I develop locally on Window and host on Linux,开发者_运维知识库 so this seem like a solution that will work on both.
To expand on Van Gale's answer, it's most certainly possible. Here are the ingredients:
- An app that is in INSTALLED APPS
- The proper directory structure
- A python file to act as the command which inherits off of a Django management command class
Here's generally how this works...
When manage.py runs, if it does not find the command say "manage.py yui_compress" it searches through the installed apps. It looks in each app to see if app.management.commands exists, and then checks if there is a file "yui_compress.py" in that module. If so, it will initiate the class in that python file and use that.
So, it ends up looking like this...
app
\management
\commands
yui_compress.py
Where yui_compress.py contains...
from django.core.management.base import NoArgsCommand
class Command(NoArgsCommand):
help = "Does my special action."
requires_model_validation = False
def handle_noargs(self, **options):
# Execute whatever code you want here
pass
Of course 'app' needs to be in thE INSTALLED APPS inside of settings.py.
But then, Van does make a good suggestion to find a tool which already does what you want. :)
Yes, but you have to write the command part yourself. The best way to go about this would be to see how the stock commands are implemented or look at a project like django-command-extensions
However, an even better solution (i.e. less work) would be to use a project like django-compress that already defines a management command synccompress
that will call yui compressor.
I recently added a YUI Compressor processor to django-mediasync.
If you'd like to use django-mediasync itself, here's the project page: https://github.com/sunlightlabs/django-mediasync
If you'd like to see the YUI Compressor command as a reference, here's a copy/paste from it (in case the paths change in the future)...
from django.conf import settings
import os
from subprocess import Popen, PIPE
def _yui_path(settings):
if not hasattr(settings, 'MEDIASYNC'):
return None
path = settings.MEDIASYNC.get('YUI_COMPRESSOR_PATH', None)
if path:
path = os.path.realpath(os.path.expanduser(path))
return path
def css_minifier(filedata, content_type, remote_path, is_active):
is_css = (content_type == 'text/css' or remote_path.lower().endswith('.css'))
yui_path = _yui_path(settings)
if is_css and yui_path and is_active:
proc = Popen(['java', '-jar', yui_path, '--type', 'css'], stdout=PIPE,
stderr=PIPE, stdin=PIPE)
stdout, stderr = proc.communicate(input=filedata)
return str(stdout)
def js_minifier(filedata, content_type, remote_path, is_active):
is_js = (content_type == 'text/javascript' or remote_path.lower().endswith('.js'))
yui_path = _yui_path(settings)
if is_js and yui_path and is_active:
proc = Popen(['java', '-jar', yui_path, '--type', 'js'], stdout=PIPE,
stderr=PIPE, stdin=PIPE)
stdout, stderr = proc.communicate(input=filedata)
return str(stdout)
精彩评论