diff options
| author | 2013-11-20 16:58:50 -0300 | |
|---|---|---|
| committer | 2013-11-20 16:58:50 -0300 | |
| commit | ca94afc07df55cb7fc6fe3b4f3011877b7881195 (patch) | |
| tree | d81e1f275aa77545f33740723f307a83dde2e0b4 /nikola/plugins/task/gzip.py | |
| parent | f794eee787e9cde54e6b8f53e45d69c9ddc9936a (diff) | |
Imported Upstream version 6.2.1upstream/6.2.1
Diffstat (limited to 'nikola/plugins/task/gzip.py')
| -rw-r--r-- | nikola/plugins/task/gzip.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/nikola/plugins/task/gzip.py b/nikola/plugins/task/gzip.py new file mode 100644 index 0000000..738d52c --- /dev/null +++ b/nikola/plugins/task/gzip.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2012-2013 Roberto Alsina and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Create gzipped copies of files.""" + +import gzip +import os +import shlex +import subprocess + +from nikola.plugin_categories import TaskMultiplier + + +class GzipFiles(TaskMultiplier): + """If appropiate, create tasks to create gzipped versions of files.""" + + name = "gzip" + is_default = True + + def process(self, task, prefix): + if not self.site.config['GZIP_FILES']: + return [] + if task.get('name') is None: + return [] + gzip_task = { + 'file_dep': [], + 'targets': [], + 'actions': [], + 'basename': '{0}_gzip'.format(prefix), + 'name': task.get('name').split(":", 1)[-1] + '.gz', + 'clean': True, + } + targets = task.get('targets', []) + flag = False + for target in targets: + ext = os.path.splitext(target)[1] + if (ext.lower() in self.site.config['GZIP_EXTENSIONS'] and + target.startswith(self.site.config['OUTPUT_FOLDER'])): + flag = True + gzipped = target + '.gz' + gzip_task['file_dep'].append(target) + gzip_task['targets'].append(gzipped) + gzip_task['actions'].append((create_gzipped_copy, (target, gzipped, self.site.config['GZIP_COMMAND']))) + if not flag: + return [] + return [gzip_task] + + +def create_gzipped_copy(in_path, out_path, command=None): + if command: + subprocess.check_call(shlex.split(command.format(filename=in_path))) + else: + with gzip.GzipFile(out_path, 'wb+') as outf: + with open(in_path, 'rb') as inf: + outf.write(inf.read()) |
