Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion whitenoise/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from io import BytesIO
from django.conf import settings

try:
import brotli
Expand Down Expand Up @@ -39,13 +40,14 @@ class Compressor(object):
)

def __init__(
self, extensions=None, use_gzip=True, use_brotli=True, log=print, quiet=False
self, extensions=None, use_gzip=True, use_brotli=True, skip_existing=False, log=print, quiet=False
):
if extensions is None:
extensions = self.SKIP_COMPRESS_EXTENSIONS
self.extension_re = self.get_extension_re(extensions)
self.use_gzip = use_gzip
self.use_brotli = use_brotli and brotli_installed
self.skip_existing = skip_existing
if not quiet:
self.log = log

Expand All @@ -65,6 +67,10 @@ def log(self, message):
pass

def compress(self, path):
skip_existing = getattr(settings, "WHITENOISE_SKIP_EXISTING", False)
if (self.skip_existing or skip_existing) and os.path.isfile(path + ".br") and os.path.isfile(path + ".gz"):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t the file checks be dependent on self.use_brotli / self.use_gzip?

return

with open(path, "rb") as f:
stat_result = os.fstat(f.fileno())
data = f.read()
Expand Down Expand Up @@ -167,5 +173,11 @@ def main(root, **kwargs):
"(default: {})".format(", ".join(Compressor.SKIP_COMPRESS_EXTENSIONS)),
default=Compressor.SKIP_COMPRESS_EXTENSIONS,
)
parser.add_argument(
"--skip-existing",
help="Don't compress, if the compressed file already exists",
action="store_true",
dest="skip_existing",
)
args = parser.parse_args()
main(**vars(args))