From d436a49c8f8b36db0d3b6452c88538cca2a3e6e2 Mon Sep 17 00:00:00 2001 From: Raine Date: Sat, 14 Oct 2023 22:39:48 +0200 Subject: [PATCH] feat: pkg.py is now usable as production builder --- bodge-pkg.py | 31 ----------- pkg.py | 77 +++++++++++++++++++++++++++ pkg/scripts/bootstrap/dirty_install | 3 -- pkg/scripts/bootstrap/download_latest | 9 ---- pkg/scripts/check_installed | 3 -- 5 files changed, 77 insertions(+), 46 deletions(-) delete mode 100644 bodge-pkg.py create mode 100644 pkg.py delete mode 100644 pkg/scripts/bootstrap/dirty_install delete mode 100644 pkg/scripts/bootstrap/download_latest delete mode 100644 pkg/scripts/check_installed diff --git a/bodge-pkg.py b/bodge-pkg.py deleted file mode 100644 index cca3307..0000000 --- a/bodge-pkg.py +++ /dev/null @@ -1,31 +0,0 @@ -import os -import tarfile - -with open("./package.toml", mode='r') as mani: - data = mani.read() - with open("./sample.pkg", mode='wb') as pkg: - print("building header...") - pkg.write(bytes([0x01, (len(data) >> 8) & 0xFF, len(data) & 0xFF])) - print("writing manifest into pkg...") - pkg.write(data.encode("utf-8")) - with tarfile.TarFile("/tmp/pkgtar", 'w') as pkgtar: - print("tarring ./pkg...") - os.chdir("pkg") - for root, dirs, files in os.walk("."): - for file in files: - print(f"\33[2Kadd: {os.path.join(root, file)}", end="\r", flush=True) - pkgtar.add(os.path.join(root, file)) - os.chdir("..") - for root, dirs, files in os.walk("pkgr"): - for file in files: - print(f"\33[2Kadd: {os.path.join(root, file)}", end="\r", flush=True) - pkgtar.add(os.path.join(root, file)) - print("\33[2K", end="\r", flush=True) - with open("/tmp/pkgtar", 'rb') as pkgtar: - print("appending pkgtar to pkg...") - pkg.write(pkgtar.read()) - print("deleting /tmp/pkgtar...") - os.unlink("/tmp/pkgtar") - print("closing write stream") - pkg.close() - \ No newline at end of file diff --git a/pkg.py b/pkg.py new file mode 100644 index 0000000..c0a8b85 --- /dev/null +++ b/pkg.py @@ -0,0 +1,77 @@ +import os +import sys +import tarfile + + +def build_package(package_toml_path, output_path, directories_to_include): + data = read_package_toml(package_toml_path) + header = build_header(data) + pkg_path = output_path + ".pkg" + + with open(pkg_path, mode='wb') as pkg: + pkg.write(header) + write_manifest(pkg, data) + build_tarball(pkg, directories_to_include) + + return pkg_path + + +def read_package_toml(package_toml_path): + with open(package_toml_path, mode='r') as mani: + return mani.read() + + +def build_header(data): + header = bytes([0x01, (len(data) >> 8) & 0xFF, len(data) & 0xFF]) + return header + + +def write_manifest(pkg, data): + pkg.write(data.encode("utf-8")) + + +def build_tarball(pkg, directories_to_include): + with tarfile.TarFile("/tmp/pkgtar", 'w') as pkgtar: + for directory in directories_to_include: + add_files_to_tar(pkgtar, directory, True) + + append_tar_to_pkg(pkg) + cleanup_tmp_files() + + +def add_files_to_tar(pkgtar, directory, skip_target=False): + for root, dirs, files in os.walk(directory): + for file in files: + if skip_target and "target" in root: + continue + print(f"\33[2Kadd: {os.path.join(root, file)}", end="\r", flush=True) + pkgtar.add(os.path.join(root, file)) + print("\33[2K", end="\r", flush=True) + + +def append_tar_to_pkg(pkg): + with open("/tmp/pkgtar", 'rb') as pkgtar: + pkg.write(pkgtar.read()) + + +def cleanup_tmp_files(): + print("deleting /tmp/pkgtar...") + os.unlink("/tmp/pkgtar") + + +if __name__ == '__main__': + if len(sys.argv) < 3: + print("Usage: pkg.py [ ...]") + sys.exit(1) + + package_toml_path = sys.argv[1] + output_path = sys.argv[2] + directories_to_include = sys.argv[3:] if len(sys.argv) > 3 else None + + try: + pkg_path = build_package(package_toml_path, output_path, directories_to_include) + print(f"Package created: {pkg_path}") + except FileNotFoundError: + print("Error: File not found.") + except Exception as e: + print(f"Error occurred: {e}") diff --git a/pkg/scripts/bootstrap/dirty_install b/pkg/scripts/bootstrap/dirty_install deleted file mode 100644 index 41d3f5d..0000000 --- a/pkg/scripts/bootstrap/dirty_install +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -printf "" \ No newline at end of file diff --git a/pkg/scripts/bootstrap/download_latest b/pkg/scripts/bootstrap/download_latest deleted file mode 100644 index 178bd4e..0000000 --- a/pkg/scripts/bootstrap/download_latest +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -fetch_latest_version() { - printf "" -} - -download_file() { - printf "" -} \ No newline at end of file diff --git a/pkg/scripts/check_installed b/pkg/scripts/check_installed deleted file mode 100644 index 41d3f5d..0000000 --- a/pkg/scripts/check_installed +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -printf "" \ No newline at end of file