2022-12-03 16:36:44 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import sys as _sys
|
|
|
|
|
import os as _os
|
|
|
|
|
import json as _json
|
|
|
|
|
import stat as _stat
|
2023-03-04 15:10:08 +01:00
|
|
|
import argparse as _argparse
|
2022-12-03 16:36:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
2023-03-04 15:36:05 +01:00
|
|
|
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"),
|
2023-03-22 14:22:54 +01:00
|
|
|
_os.path.join(dir_source, "logic", "checks", "tls_certificate.py"),
|
2023-03-04 15:36:05 +01:00
|
|
|
_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"),
|
|
|
|
|
]
|
2023-03-04 15:10:08 +01:00
|
|
|
targets = {
|
|
|
|
|
"app": {
|
2023-03-04 15:36:05 +01:00
|
|
|
"sources": (
|
|
|
|
|
sources_common
|
|
|
|
|
+
|
|
|
|
|
[
|
|
|
|
|
_os.path.join(dir_source, "logic", "main.py"),
|
|
|
|
|
]
|
|
|
|
|
),
|
2023-03-04 15:10:08 +01:00
|
|
|
"build": _os.path.join(dir_build, "heimdall"),
|
|
|
|
|
},
|
|
|
|
|
"test": {
|
2023-03-04 15:36:05 +01:00
|
|
|
"sources": (
|
|
|
|
|
sources_common
|
|
|
|
|
+
|
|
|
|
|
[
|
|
|
|
|
_os.path.join(dir_source, "test", "test.py"),
|
|
|
|
|
]
|
|
|
|
|
),
|
2023-03-04 15:10:08 +01:00
|
|
|
"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()
|
2022-12-03 16:36:44 +01:00
|
|
|
|
|
|
|
|
## 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
|
2023-03-04 15:10:08 +01:00
|
|
|
for path in targets[args.target_name]["sources"]:
|
2022-12-03 16:36:44 +01:00
|
|
|
compilation += (file_read(path) + "\n")
|
|
|
|
|
|
|
|
|
|
### write to file
|
2023-03-04 15:10:08 +01:00
|
|
|
if _os.path.exists(targets[args.target_name]["build"]):
|
|
|
|
|
_os.remove(targets[args.target_name]["build"])
|
|
|
|
|
file_write(targets[args.target_name]["build"], compilation)
|
2022-12-03 16:36:44 +01:00
|
|
|
|
|
|
|
|
### postproess
|
|
|
|
|
_os.chmod(
|
2023-03-04 15:10:08 +01:00
|
|
|
targets[args.target_name]["build"],
|
2022-12-03 16:36:44 +01:00
|
|
|
(_stat.S_IRWXU | _stat.S_IXGRP | _stat.S_IXOTH)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main()
|