keypadOS/x.py
2024-08-25 22:35:33 +03:00

94 lines
2.1 KiB
Python
Executable File

#!/usr/bin/python
OUTPUT="rtmc.lua";
FILES= [
"main.lua",
"log.lua",
"json.lua",
"updater.lua"
]
VERSION="1.0.0d"
MINIMISE=True
def read_file(p: str) -> str:
buf = "";
with open("src/"+p, "r", encoding="utf-8") as f:
buf += f"rawset(__BUNDLER_FILES, \"{p.replace(".lua", "")}\", function ()\n";
for line in f.readlines():
buf += " " + line;
buf += "\nend)";
if not MINIMISE:
buf += f"-- FILE END: {p} --\n";
else:
buf += "\n";
return buf;
def minimise(buf: str) -> str:
newbuf = "";
for line in buf.splitlines():
if line.strip().startswith("--"):
continue;
if line.strip() == "":
continue
newbuf += line + "\n";
return newbuf;
# def get_hash(buf: str) -> str:
# hasher = hashlib.sha1();
#
# hasher.update(bytes(buf, "utf-8"));
# return hasher.hexdigest();
REQ_HEADER=f"""local __BUNDLER_FILES = {{}}
local __VERSION = "{VERSION}"
local __DEFAULT_IMPORT = require
local require = function(path)
if __BUNDLER_FILES[path] then
return __BUNDLER_FILES[path]()
elseif __BUNDLER_FILES[path .. ".lua"] then
return __BUNDLER_FILES[path .. ".lua"]()
else
return __DEFAULT_IMPORT(path)
end
end
"""
REQ_FOOTER="""
if pcall(debug.getlocal, 4, 1) then
local exports = {};
for k, v in pairs(__BUNDLER_FILES) do
if k ~= "main" then
exports[k] = v();
end
end
return exports;
else
require("main").main()
end
"""
def main():
buf = ""
buf += REQ_HEADER;
# buf += "local __UPDATE_HASH = __BUNDLER_REPLACE_HASH__\n";
for file in FILES:
print(f"=== FILE: {file}");
buf += read_file(file);
buf += REQ_FOOTER;
if MINIMISE:
buf = minimise(buf);
#update_hash = get_hash(buf);
# buf = buf.replace("__BUNDLER_REPLACE_HASH__", f"\"{update_hash}\"");
#print(f"=== UPDATE HASH: {update_hash}")
with open(OUTPUT, "w", encoding="utf-8") as f:
f.write(buf);
print("DONE");
if __name__ == "__main__":
main();