feat: pkg.py is now usable as production builder

This commit is contained in:
Didier Slof 2023-08-08 00:31:14 +02:00
parent 3e6b0e2070
commit a9c9b3c704
Signed by: didier
GPG key ID: 01E71F18AA4398E5
5 changed files with 77 additions and 46 deletions

View file

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

77
pkg.py Normal file
View file

@ -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 <package_toml_path> <output_path> [<directories_to_include> ...]")
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}")

View file

@ -1,3 +0,0 @@
#!/bin/sh
printf ""

View file

@ -1,9 +0,0 @@
#!/bin/sh
fetch_latest_version() {
printf ""
}
download_file() {
printf ""
}

View file

@ -1,3 +0,0 @@
#!/bin/sh
printf ""