blob: 2a9e5290b5658b736ca0681053e33fb13b5be684 (
plain)
#!/usr/bin/env python3
import gzip
import os
import shutil
import sys
import brotli
def clean_target():
if os.path.exists('target'):
shutil.rmtree('target')
os.mkdir('target')
def compile_all(path: str, target: str):
for file in os.listdir(path):
src = os.path.join(path, file)
base, ext = os.path.splitext(file)
if os.path.isdir(src):
os.mkdir(os.path.join(target, file))
compile_all(src, os.path.join(target, file))
elif ext == '.kuht':
print(f"Building {src}")
exit_code = os.system(f"kuht {src} src/base.kuht -o {target}/{base}.html")
if exit_code != 0:
sys.exit(exit_code)
def move_css(path: str):
for file in os.listdir(path):
if os.path.splitext(file)[1] == '.css':
shutil.copy(file, os.path.join('target', file))
def compress_files(path: str):
for file in os.listdir(path):
file = os.path.join(path, file)
if os.path.isdir(file):
compress_files(file)
return
with open(file, 'rb') as f_in:
data = f_in.read()
with gzip.open(f"{file}.gz", 'wb') as f_out:
f_out.write(data)
with open(f"{file}.br", 'wb') as f_out:
f_out.write(brotli.compress(data, mode=brotli.MODE_TEXT))
def main():
original_path = os.getcwd()
script_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(script_path)
clean_target()
compile_all('src', 'target')
move_css(".")
compress_files("target")
os.chdir(original_path)
if __name__ == '__main__':
main()
|