forked from semios/ports
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
class PkgComposer:
|
||||
def __init__(self, pkgfile: str):
|
||||
self.pkgfile = os.path.abspath(pkgfile)
|
||||
|
||||
def setworkdir(self, workdir: str):
|
||||
self.workdir = workdir
|
||||
shutil.rmtree(workdir, ignore_errors=True)
|
||||
os.mkdir(workdir)
|
||||
os.mkdir(f"{workdir}/bundle")
|
||||
|
||||
def write_manifest(self, manifest: dict):
|
||||
with open(self.workdir + "/PkgManifest.json", "w") as f:
|
||||
json.dump(manifest, f)
|
||||
|
||||
def write_links(self, links: list[dict]):
|
||||
with open(self.workdir + "/_links", "w") as f:
|
||||
for link in links:
|
||||
json.dump(link, f)
|
||||
f.write("\n")
|
||||
|
||||
def compose(self):
|
||||
subprocess.run(
|
||||
[
|
||||
"tar", "cpf", f"{self.pkgfile}.tar",
|
||||
"-C", self.workdir,
|
||||
"--numeric-owner",
|
||||
".",
|
||||
],
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(["zstd", "--rm", "-f", f"{self.pkgfile}.tar", "-o", self.pkgfile], check=True)
|
||||
|
||||
def makedir(self, name: str):
|
||||
os.mkdir(f"{self.workdir}/bundle/{name}")
|
||||
|
||||
def makedirs(self, name: str):
|
||||
os.makedirs(f"{self.workdir}/bundle/{name}", exist_ok=True)
|
||||
|
||||
def addfile(self, src: str, dst: str | None = None):
|
||||
if dst is None:
|
||||
dst = src
|
||||
shutil.copy2(src, f"{self.workdir}/bundle/{dst}", follow_symlinks=False)
|
||||
|
||||
def add_dir(self, src: str, dst: str | None = None, merge: bool = False):
|
||||
if dst is None:
|
||||
dst = src
|
||||
target = f"{self.workdir}/bundle/{dst}"
|
||||
if not os.path.exists(src):
|
||||
raise FileNotFoundError(f"Source directory not found: {src}")
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
if merge and os.path.exists(target) and os.path.isdir(target):
|
||||
cmd = ["cp", "-a", f"{src}/.", target]
|
||||
else:
|
||||
cmd = ["cp", "-a", src, target]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"cp command failed: {result.stderr}")
|
||||
|
||||
def link_external(self, src: str, dst: str):
|
||||
os.symlink(src, f"{self.workdir}/bundle/{dst}")
|
||||
Reference in New Issue
Block a user