Skip to content

Instantly share code, notes, and snippets.

@tritium21
Created January 22, 2020 13:01
Show Gist options
  • Save tritium21/78e00939a8d8c936d3994a6b2bfd717e to your computer and use it in GitHub Desktop.
Save tritium21/78e00939a8d8c936d3994a6b2bfd717e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.8
import hashlib
import io
import json
import pathlib
import tarfile
def make_tarinfo(path, data):
fob = io.BytesIO(data.encode())
info = tarfile.TarInfo(name=path)
info.size = fob.getbuffer().nbytes
return info, fob
def make_tar(dist, ident, datamap):
dist.mkdir(parents=True, exist_ok=True)
file_out = io.BytesIO()
with tarfile.open(mode='w:gz', fileobj=file_out) as tar:
for path, data in datamap.items():
tar.addfile(*make_tarinfo(f"{ident}/{path}", data))
file_out.seek(0)
(dist / f'{ident}.tar.gz').write_bytes(file_out.getvalue())
def apply_vars(text, data=None):
data = {} if data is None else dict(data)
for key, value in data.items():
text = text.replace(key, json.dumps(value))
return text
def bump_version(filename):
filepath = pathlib.Path(filename).resolve()
version = filepath.read_text()
s = version.split('.')
s[-1] = str(int(s[-1]) + 1)
version = '.'.join(s)
filepath.write_text(version)
return version
def main():
path = pathlib.Path(__file__).resolve().parent
templates = path / 'templates'
dist = path / 'dist'
css_b = (path / 'reactor.css').read_bytes()
css = css_b.decode()
bundle = (templates / 'custom_css_bundle.js').read_text()
plugin = (templates / 'plugin.json').read_text()
version = bump_version(path / 'version')
ident = f"custom-css-{''.join(version.split('.'))}"
bundle = apply_vars(bundle, {"$id$": ident, "$CSS$": css})
bundle_name = f"custom_css_{hashlib.sha1(css_b).hexdigest()}.js"
plugin = apply_vars(plugin, {"$id$": ident, "$VERSION$": version, "$BUNDLE_PATH$": bundle_name})
make_tar(dist, ident, {bundle_name: bundle, 'plugin.json': plugin,})
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment