我可以调用YUI压缩机:Java的罐子的YUICompressor-x.y.z.jar [选项]从Django的管理命令,如果让我怎么去这样做[输入文件]?Django的管理命令+锐压缩机
我在当地发展的窗口和主机在Linux上,因此这似乎是一个解决方案,将在两个工作。
我可以调用YUI压缩机:Java的罐子的YUICompressor-x.y.z.jar [选项]从Django的管理命令,如果让我怎么去这样做[输入文件]?Django的管理命令+锐压缩机
我在当地发展的窗口和主机在Linux上,因此这似乎是一个解决方案,将在两个工作。
是的,但你必须自己写命令的一部分。去这个问题的最好办法是怎么看股票命令执行或看一个项目像django-command-extensions
然而,一个更好的解决方案(即较少的工作)是使用一个项目像django-compress已经定义一个管理命令synccompress
,它将调用yui压缩器。
为了扩大对范大风的答案,这是肯定可以的。下面是成分:
这里一般是如何工作的...
当manage.py运行时,如果它找不到命令说“manage.py yui_compress”它搜索通过安装的应用程序。它会在每个应用程序中查看是否存在app.management.commands,然后检查该模块中是否存在文件“yui_compress.py”。如果是这样,它将启动该python文件中的类并使用它。
因此,它结束了看起来像这样...
app
\management
\commands
yui_compress.py
凡yui_compress.py包含...
当然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
中 '应用' 需要在已安装应用内settings.py。
但随后,范确实让一个很好的建议,以找到一个工具,它已经做了你想要的东西。 :)
我最近增加了一个YUI压缩机处理器Django的mediasync。
如果您想使用Django的mediasync本身,这里的项目页面: https://github.com/sunlightlabs/django-mediasync
如果你想看到的YUI压缩机命令作为参考,这里是从它复制/粘贴(以防路径在未来发生变化)...
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)
+1不错的答案,我想勾画出如何做一个命令,但没有足够的时间。 – 2009-11-12 23:16:34
感谢您的回答。我知道如何设置管理命令,但想知道:NoArgsCommand以及如何放置在:def handle_noargs方法中? – Joe 2009-11-13 00:39:17
这里有什么代码是你想运行命令时运行的任何代码。这里有一个来自南方的例子 - http://bitbucket.org/andrewgodwin/south/src/tip/south/management/commands/convert_to_south。py因此,例如,您可以递归搜索MEDIA_ROOT文件夹,查找以CSS结尾的所有文件,通过YUI压缩器运行它们,然后将它们保存为[[originalname]] _ compressed.css。 – 2009-11-13 02:09:43