django收集静态文件
A little while ago I wrote a blog post talking about Git hooks. As an example in that post I wrote a post-commit hook that would minify and upload my static files to S3.
不久前,我写了一篇博客文章谈论Git钩子 。 作为该帖子中的一个示例,我编写了一个提交后钩子,该钩子将最小化我的静态文件并将其上传到S3。
This has spiralled a little bit out of control since then, and I finally drew the line at maintaining that script. It was time to create something more powerful. To this end I give you: collectr.
从那以后,这种情况逐渐失去控制,我终于在维护该脚本时划清界限。 是时候创建更强大的功能了。 为此,我给你: 收集器 。
关于收藏家 (About collectr)
collectr has one goal: to control the management of static files. In its current form it is a wrapper around the boto library. It targets Amazon’s S3 as the back end data store. Its sole purpose in life is to prepare and upload static files to S3 according to the user’s wishes.
收集器的目标是:控制静态文件的管理。 目前,它是boto库的包装器。 它针对亚马逊的S3作为后端数据存储。 其唯一的目的是根据用户的意愿准备静态文件并将其上传到S3。
In the basic case, where all you want to do is upload some files in a directory to S3, you can keep things simple:
在基本情况下,您要做的就是将目录中的一些文件上传到S3,您可以使事情简单:
import import collectr
collectr
collectrcollectr .. updateupdate (( 'path/to/dir''path/to/dir' , , 'bucket_name''bucket_name' ))
But collectr allows much more than that. In its current early state, collectr supports a few other behaviours: it can minify files, it can ignore files in the directory, and it can apply S3 metadata. A very simple API controls this functionality. For instance, you can apply metadata:
但是收集器的功能远不止于此。 在当前的早期状态下,收集器还支持其他一些行为:它可以缩小文件,可以忽略目录中的文件以及可以应用S3元数据。 一个非常简单的API可以控制此功能。 例如,您可以应用元数据:
You can ignore files:
您可以忽略文件:
import import collectr
collectr
directory directory = = collectrcollectr .. StaticDirStaticDir (( 'path/to/dir''path/to/dir' )
)
directorydirectory .. ignore ignore = = [[ '.*'.* .json'.json' ]]
And you can minify.
您可以缩小规模。
缩小 (Minification)
Minification has the most functionality at the moment. To minify files, you want to provide a dictionary. The keys of this dictionary should correspond to the file extensions, and the values correspond to the command-line you want to run to minify the files. This allows collectr to run using whatever minification tool is your favourite (or alternatively, whatever you have installed). For example:
缩小功能目前具有最多的功能。 要缩小文件,您想提供一个字典。 该字典的键应对应于文件扩展名,而值对应于要运行以缩小文件的命令行。 这样,收集器就可以使用您喜欢的任何缩小工具(或选择安装的任何工具)运行。 例如:
Each of the files in the directory is checked, and if it has a matching extension, the command-line is executed. It executes in its own shell, so globbing and redirection should work fine. All you need is to remember to put in both an in_name
and out_name
field in the format string. The out_name
is the same as the name of the file, with ‘-min’ appended to it. This will likely become user-configurable in the future.
将检查目录中的每个文件,如果它们具有匹配的扩展名,则将执行命令行。 它在自己的外壳中执行,因此,globbing和重定向应该可以正常工作。 您需要记住的是在格式字符串中同时输入in_name
和out_name
字段。 out_name
与文件名相同,并在文件名后附加“ -min”。 将来可能会变为用户可配置的。
You might want to keep your pre-minified files in a separate directory. You can do that: call the directory that contains the pre-minified files the input directory like this:
您可能希望将缩小的文件保留在单独的目录中。 您可以这样做:将包含预缩小文件的目录称为输入目录,如下所示:
import import collectr
collectr
directory directory = = collectrcollectr .. StaticDirStaticDir (( 'path/to/dir''path/to/dir' )
)
directorydirectory .. input_directory input_directory = = 'other/dir'
'other/dir'
directorydirectory .. minifier minifier = = 'yuicompressor -o {out_name} {in_name}''yuicompressor -o {out_name} {in_name}'
The out_file
format string variable will move from the first directory to the second, so if you use this feature don’t forget to use out_file
!
out_file
格式的字符串变量将从第一个目录移动到第二个目录,因此,如果您使用此功能,请不要忘记使用out_file
!
The above also shows the special-case where the minifier property is a string. If you do this, collectr treats the string as a command-line to be applied to both Javascript and CSS files. This is ideal for using with something like yuicompressor.
上面还显示了minifier属性是字符串的特殊情况。 如果执行此操作,收集器会将字符串视为要应用于Javascript和CSS文件的命令行。 这是与yuicompressor之类的东西一起使用的理想选择。
You might have noticed that there’s nothing here that specifies minification. This is correct: in fact, you can do any form of pre-processing you like using this method. Enjoy the freedom!
您可能已经注意到,这里没有指定缩小的内容。 这是正确的:实际上,您可以使用此方法进行任何形式的预处理。 享受自由!
命令行工具 (Command-line tools)
As a freebie, just to show a simple example of how you can replace your bash scripts with something nicer, collectr comes with a simple script entitled collect_static
(hi there Django!). This script is intended to be as close a match as possible to the script I wrote for my post-commit hook. It’s roughly the same length, but the vast majority of it is code for argparse and some if __name__ == '__main__'
stuff. In fact, there is only one line of code to handle all of the rest. collectr is really that easy.
作为免费赠品,只是为了展示如何用更好的东西替换bash脚本的简单示例,collectr附带了一个名为collect_static
的简单脚本(嗨, Django !)。 该脚本旨在与我为提交后挂钩编写的脚本尽可能接近。 它的长度大致相同,但是其中绝大多数是argparse的代码,还有一些if __name__ == '__main__'
东西。 实际上,只有一行代码可以处理其余所有代码。 收藏家真的很容易。
未来 (The Future)
翻译自: https://www.pybloggers.com/2013/03/collectr-static-file-management-for-all-of-us/
django收集静态文件