summaryrefslogtreecommitdiff
path: root/build.py3
blob: 8adc03941691a66411145077de5a6eae509a19f3 (plain)
#!/usr/bin/env python3

import gzip
import os
import shutil

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':
			os.system(f"kuht {src} src/base.kuht -o {target}/{base}.html")


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()