add doit task for compressing static files

This commit is contained in:
codl 2017-08-11 20:03:18 +02:00
parent ed69bdfb12
commit e60897a3f8
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
1 changed files with 25 additions and 0 deletions

25
dodo.py
View File

@ -48,6 +48,31 @@ def task_minify_css():
'clean': True, 'clean': True,
} }
def task_compress_static():
import brotli
import gzip
deps = ('static/styles.css', 'static/icon.png', 'static/logotype.png') + tuple((f'static/logotype-{width}.png' for width in (200, 400, 600, 800)))
targets = tuple((f'{file}.br' for file in deps)) + tuple((f'{file}.gz' for file in deps))
def compress_brotli(dependencies):
for filename in dependencies:
with open(filename, 'rb') as in_:
with open(filename + '.br', 'wb') as out:
out.write(brotli.compress(in_.read()))
def compress_gzip(dependencies):
for filename in dependencies:
with open(filename, 'rb') as in_:
with gzip.open(filename + '.gz', 'wb') as out:
out.write(in_.read())
return dict(
file_dep=deps,
targets=targets,
actions=[compress_brotli, compress_gzip],
clean=True,
)
if __name__ == '__main__': if __name__ == '__main__':
import doit import doit
doit.run(globals()) doit.run(globals())