From fa34a605e8bf1eb37a688709b77acbb531ef2401 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 8 Sep 2013 11:35:38 -0300 Subject: [PATCH] Support for pigz or other external gzip commands (Fix #351) --- CHANGES.txt | 8 ++++++++ nikola/conf.py.in | 3 +++ nikola/nikola.py | 1 + nikola/plugins/task/gzip.py | 15 ++++++++++----- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7bc682fc85..eeae4c358d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,11 @@ +New in master +============= + +Features +-------- + +* Support for external gzip commands (Issue #351) + New in 6.0.0 ============ diff --git a/nikola/conf.py.in b/nikola/conf.py.in index e645d6e81f..cda7d2c3bb 100644 --- a/nikola/conf.py.in +++ b/nikola/conf.py.in @@ -203,6 +203,9 @@ COMPILERS = ${COMPILERS} # GZIP_FILES = False # File extensions that will be compressed # GZIP_EXTENSIONS = ('.txt', '.htm', '.html', '.css', '.js', '.json') +# Use an external gzip command? None means no. +# Example: GZIP_COMMAND = "pigz -k {filename}" +# GZIP_COMMAND = None # ############################################################################# # Image Gallery Options diff --git a/nikola/nikola.py b/nikola/nikola.py index f68c016441..695a2dd6fc 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -140,6 +140,7 @@ def __init__(self, **config): 'FILES_FOLDERS': {'files': ''}, 'FILTERS': {}, 'GALLERY_PATH': 'galleries', + 'GZIP_COMMAND': None, 'GZIP_FILES': False, 'GZIP_EXTENSIONS': ('.txt', '.htm', '.html', '.css', '.js', '.json'), 'HIDE_SOURCELINK': False, diff --git a/nikola/plugins/task/gzip.py b/nikola/plugins/task/gzip.py index 7f3b0db545..738d52c813 100644 --- a/nikola/plugins/task/gzip.py +++ b/nikola/plugins/task/gzip.py @@ -28,6 +28,8 @@ import gzip import os +import shlex +import subprocess from nikola.plugin_categories import TaskMultiplier @@ -61,13 +63,16 @@ def process(self, task, prefix): gzipped = target + '.gz' gzip_task['file_dep'].append(target) gzip_task['targets'].append(gzipped) - gzip_task['actions'].append((create_gzipped_copy, (target, 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): - with gzip.GzipFile(out_path, 'wb+') as outf: - with open(in_path, 'rb') as inf: - outf.write(inf.read()) +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())