forked from semios/ports
85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
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 add_configure(self, path: str, name: str = None):
|
|
if name is None:
|
|
name = os.path.basename(path)
|
|
configuredir = self.workdir + "/configure/"
|
|
dstpath = configuredir + name
|
|
os.makedirs(configuredir, exist_ok=True)
|
|
shutil.copy2(path, dstpath)
|
|
os.chmod(dstpath, 0o755)
|
|
|
|
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}")
|
|
|
|
def add(self, src: str, dst: str | None = None):
|
|
if dst is None:
|
|
dst = src
|
|
if os.path.islink(src) or os.path.isfile(src):
|
|
self.addfile(src, dst)
|
|
else:
|
|
self.add_dir(src, dst, True)
|