How to use the whitenoise.gzip.GzipFile function in whitenoise

To help you get started, we’ve selected a few whitenoise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github evansd / whitenoise / whitenoise / gzip.py View on Github external
def compress(path, log=null_log):
    gzip_path = path + '.gz'
    with open(path, 'rb') as in_file:
        # Explicitly set mtime to 0 so gzip content is fully determined
        # by file content (0 = "no timestamp" according to gzip spec)
        with gzip.GzipFile(gzip_path, 'wb', compresslevel=9, mtime=0) as out_file:
            for chunk in iter(lambda: in_file.read(CHUNK_SIZE), b''):
                out_file.write(chunk)
    # If gzipped file isn't actually any smaller then get rid of it
    orig_size = os.path.getsize(path)
    gzip_size = os.path.getsize(gzip_path)
    if not is_worth_gzipping(orig_size, gzip_size):
        log('Skipping {0} (Gzip not effective)'.format(path))
        os.unlink(gzip_path)
    else:
        log('Gzipping {0} ({1}K -> {2}K)'.format(
            path, orig_size // 1024, gzip_size // 1024))