#!/usr/bin/env python3 import sys as _sys import os as _os import json as _json import stat as _stat import argparse as _argparse def file_read(path): handle = open(path, "r") content = handle.read() handle.close() return content def file_write(path, content): handle = open(path, "w") handle.write(content) handle.close() def string_coin(template, arguments): result = template for (key, value, ) in arguments.items(): result = result.replace("{{%s}}" % key, value) return result def python_data_encode(data): return _json.dumps(data, indent = "\t") def main(): ## consts dir_source = "source" dir_build = "build" sources_common = [ _os.path.join(dir_source, "logic", "packages.py"), _os.path.join(dir_source, "logic", "lib.py"), _os.path.join(dir_source, "logic", "localization.py"), _os.path.join(dir_source, "logic", "condition.py"), _os.path.join(dir_source, "logic", "conf.py"), _os.path.join(dir_source, "logic", "checks", "_interface.py"), _os.path.join(dir_source, "logic", "checks", "script.py"), _os.path.join(dir_source, "logic", "checks", "file_state.py"), _os.path.join(dir_source, "logic", "checks", "http_request.py"), _os.path.join(dir_source, "logic", "checks", "generic_remote.py"), _os.path.join(dir_source, "logic", "channels", "_interface.py"), _os.path.join(dir_source, "logic", "channels", "console.py"), _os.path.join(dir_source, "logic", "channels", "email.py"), _os.path.join(dir_source, "logic", "channels", "libnotify.py"), ] targets = { "app": { "sources": ( sources_common + [ _os.path.join(dir_source, "logic", "main.py"), ] ), "build": _os.path.join(dir_build, "heimdall"), }, "test": { "sources": ( sources_common + [ _os.path.join(dir_source, "test", "test.py"), ] ), "build": _os.path.join(dir_build, "heimdall-test"), }, } ## args argument_parser = _argparse.ArgumentParser( ) argument_parser.add_argument( "-t", "--target", type = str, choices = ["app", "test"], default = "app", dest = "target_name", help = "which target to build", ) args = argument_parser.parse_args() ## exec if (not _os.path.exists(dir_build)): _os.mkdir(dir_build) compilation = "" compilation += "#!/usr/bin/env python3\n\n" ### localization if True: localization_data = dict( map( lambda entry: ( entry.name.split(".")[0], _json.loads(file_read(entry.path)), ), filter( lambda entry: (entry.is_file() and entry.name.endswith(".json")), _os.scandir(_os.path.join(dir_source, "localization")) ) ) ) compilation += string_coin( "localization_data = {{data}}\n\n", { "data": python_data_encode(localization_data), } ) ### logic for path in targets[args.target_name]["sources"]: compilation += (file_read(path) + "\n") ### write to file if _os.path.exists(targets[args.target_name]["build"]): _os.remove(targets[args.target_name]["build"]) file_write(targets[args.target_name]["build"], compilation) ### postproess _os.chmod( targets[args.target_name]["build"], (_stat.S_IRWXU | _stat.S_IXGRP | _stat.S_IXOTH) ) main()