forget-cancellare-vecchi-toot/dodo.py

165 lines
4.8 KiB
Python
Raw Normal View History

from doit import create_after
2017-08-29 14:46:32 +02:00
def reltouch(source_filename, dest_filename):
from os import stat, utime
stat_res = stat(source_filename)
utime(dest_filename, ns=(stat_res.st_atime_ns, stat_res.st_mtime_ns))
2017-08-29 14:46:32 +02:00
2017-08-24 15:39:11 +02:00
def resize_image(basename, width, format):
2017-08-11 14:57:32 +02:00
from PIL import Image
2017-08-24 15:39:11 +02:00
with Image.open('assets/{}.png'.format(basename)) as im:
if 'A' in im.getbands() and format != 'jpeg':
im = im.convert('RGBA')
else:
2017-08-20 20:17:38 +02:00
im = im.convert('RGB')
2017-08-24 15:39:11 +02:00
height = im.height * width // im.width
2017-08-29 14:46:32 +02:00
new = im.resize((width, height), resample=Image.LANCZOS)
2017-08-24 15:39:11 +02:00
if format == 'jpeg':
kwargs = dict(
2017-08-29 14:46:32 +02:00
optimize=True,
progressive=True,
quality=80,
2017-08-24 15:39:11 +02:00
)
elif format == 'webp':
kwargs = dict(
2017-08-29 14:46:32 +02:00
quality=79,
2017-08-24 15:39:11 +02:00
)
elif format == 'png':
kwargs = dict(
2017-08-29 14:46:32 +02:00
optimize=True,
2017-08-24 15:39:11 +02:00
)
new.save('static/{}-{}.{}'.format(basename, width, format), **kwargs)
2017-08-29 14:46:32 +02:00
reltouch('assets/{}.png'.format(basename),
'static/{}-{}.{}'.format(basename, width, format))
2017-08-11 21:26:17 +02:00
def task_logotype():
"""resize and convert logotype"""
2017-08-11 21:26:17 +02:00
widths = (200, 400, 600, 800)
2017-08-20 20:17:38 +02:00
formats = ('jpeg', 'webp')
2017-08-11 21:26:17 +02:00
for width in widths:
2017-08-20 20:17:38 +02:00
for format in formats:
yield dict(
name='{}.{}'.format(width, format),
2017-08-24 15:39:11 +02:00
actions=[(resize_image, ('logotype', width, format))],
2017-08-20 20:17:38 +02:00
targets=[f'static/logotype-{width}.{format}'],
file_dep=['assets/logotype.png'],
clean=True,
)
2017-08-11 14:57:32 +02:00
2017-08-29 14:46:32 +02:00
def task_service_icon():
"""resize and convert service icons"""
2017-08-29 14:46:32 +02:00
widths = (20, 40, 80)
2017-08-24 15:39:11 +02:00
formats = ('webp', 'png')
for width in widths:
for format in formats:
for basename in ('twitter', 'mastodon'):
yield dict(
name='{}-{}.{}'.format(basename, width, format),
actions=[(resize_image, (basename, width, format))],
2017-08-29 14:46:32 +02:00
targets=[
'static/{}-{}.{}'.format(basename, width, format)],
2017-08-24 15:39:11 +02:00
file_dep=['assets/{}.png'.format(basename)],
clean=True,
)
2017-08-11 14:57:32 +02:00
2017-08-29 14:46:32 +02:00
def task_copy():
"copy assets verbatim"
2017-08-24 15:39:11 +02:00
assets = ('icon.png', 'logotype.png', 'settings.js')
def do_the_thing(src, dst):
from shutil import copy
copy(src, dst)
reltouch(src, dst)
2017-08-11 21:26:17 +02:00
for asset in assets:
src = 'assets/{}'.format(asset)
dst = 'static/{}'.format(asset)
2017-08-11 21:26:17 +02:00
yield dict(
name=asset,
actions=[(do_the_thing, (src, dst))],
2017-08-24 18:53:24 +02:00
targets=[dst],
file_dep=[src],
2017-08-11 21:26:17 +02:00
clean=True,
)
2017-08-11 16:41:31 +02:00
2017-08-29 14:46:32 +02:00
2017-08-11 16:41:31 +02:00
def task_minify_css():
"""minify css file with csscompressor"""
2017-08-11 16:41:31 +02:00
from csscompressor import compress
def minify():
with open('assets/styles.css') as in_:
with open('static/styles.css', 'w') as out:
out.write(compress(in_.read()))
reltouch('assets/styles.css', 'static/styles.css')
2017-08-11 16:41:31 +02:00
return dict(
actions=[minify],
targets=['static/styles.css'],
file_dep=['assets/styles.css'],
clean=True,
)
2017-08-11 16:41:31 +02:00
2017-08-29 14:46:32 +02:00
@create_after('logotype')
@create_after('service_icon')
@create_after('copy')
@create_after('minify_css')
def task_compress():
2017-08-29 14:46:32 +02:00
"""
make gzip and brotli compressed versions of each
static file for the server to lazily serve
"""
from glob import glob
from itertools import chain
2017-08-20 20:17:38 +02:00
files = chain(
glob('static/*.css'),
glob('static/*.js'),
glob('static/*.jpeg'),
glob('static/*.png'),
glob('static/*.webp'),
)
def compress_brotli(filename):
import brotli
with open(filename, 'rb') as in_:
with open(filename + '.br', 'wb') as out:
out.write(brotli.compress(in_.read()))
reltouch(filename, filename+'.br')
2017-08-24 15:39:11 +02:00
def compress_gzip(filename):
import gzip
with open(filename, 'rb') as in_:
with gzip.open(filename + '.gz', 'wb') as out:
out.write(in_.read())
reltouch(filename, filename+'.gz')
for filename in files:
yield dict(
file_dep=(filename,),
targets=(filename+'.br',),
name=filename+'.br',
actions=[(compress_brotli, (filename,))],
clean=True,
)
yield dict(
file_dep=(filename,),
targets=(filename+'.gz',),
name=filename+'.gz',
actions=[(compress_gzip, (filename,))],
clean=True,
)
2017-08-29 14:46:32 +02:00
2017-08-11 14:57:32 +02:00
if __name__ == '__main__':
import doit
doit.run(globals())