78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
from urllib.parse import urlparse
|
|
import os
|
|
import subprocess
|
|
from . import sources
|
|
import glob
|
|
|
|
|
|
DEFAULT_REPO="https://mirror2.semilabs.org/semios/srcpkgs"
|
|
|
|
|
|
def postprocess_rust():
|
|
subprocess.run(["cargo", "vendor"], check=True)
|
|
os.makedirs(".cargo", exist_ok=True)
|
|
with open(".cargo/config.toml", "a+") as fd:
|
|
fd.write(
|
|
"[source.crates-io]\nreplace-with = 'vendored-sources'\n[source.vendored-sources]\ndirectory = 'vendor'\n",
|
|
)
|
|
|
|
def postprocess_autotools():
|
|
subprocess.run(["sh", "autogen.sh"], check=True)
|
|
subprocess.run(["./configure"], check=False)
|
|
subprocess.run(["make", "dist"], check=True)
|
|
tarballs = glob.glob("*.tar.gz")
|
|
if len(tarballs) != 1:
|
|
raise RuntimeError("No dist tarballs found.")
|
|
subprocess.run(["tar", "xf", tarballs[0]], check=True)
|
|
os.chdir(tarballs[0].replace(".tar.gz", ""))
|
|
|
|
|
|
def get_origin_url(s):
|
|
url = urlparse(s)
|
|
if url.scheme == "github":
|
|
return f"https://github.com/{url.hostname}{url.path}/archive/refs/tags/{url.fragment}.tar.gz"
|
|
elif url.scheme == "gitea":
|
|
return f"https://{url.hostname}{url.path}/archive/{url.fragment}.tar.gz"
|
|
else:
|
|
raise RuntimeError(f"Unsupported scheme \"{url.scheme}\"")
|
|
|
|
def get_updates(s):
|
|
url = urlparse(s)
|
|
current_version = url.fragment
|
|
|
|
def get_repo_filename(s):
|
|
url = urlparse(s)
|
|
if url.scheme == "github":
|
|
ghname = f"{url.hostname}-{url.path.replace('/', '')}"
|
|
return f"github-{ghname}-{url.fragment}.tar.zst"
|
|
elif url.scheme == "gitea":
|
|
gitea_name = url.path.replace('/', '')
|
|
gitea_host = url.hostname.split('.')[-2]
|
|
return f"{gitea_host}-{gitea_name}-{url.fragment}.tar.zst"
|
|
else:
|
|
raise RuntimeError(f"Unsupported scheme \"{url.scheme}\"")
|
|
|
|
def get_repo_url(s):
|
|
url = urlparse(s)
|
|
repo_url = os.environ.get("SEMIOS_SOURCE_REPO", DEFAULT_REPO)
|
|
return f"{repo_url}/{get_repo_filename(s)}"
|
|
|
|
def create_srcpkg(s, postprocess="") -> str:
|
|
origin_url = get_origin_url(s)
|
|
origin_dir = sources.download_decompress(origin_url)
|
|
os.chdir(origin_dir)
|
|
for program in postprocess.split(','):
|
|
program = program.strip()
|
|
if program == "":
|
|
continue
|
|
elif program == "rust":
|
|
postprocess_rust()
|
|
elif program == "autotools":
|
|
postprocess_autotools()
|
|
else:
|
|
raise RuntimeError(f"Unknown postprocessor \"{program}\"")
|
|
subprocess.run(["tar", "cpf", "../srcpkg.tar", "--numeric-owner", "."], check=True)
|
|
os.chdir("../")
|
|
subprocess.run(["zstd", "-q", "-f", "-o", get_repo_filename(s), "srcpkg.tar"], check=True)
|
|
return os.getcwd() + "/" + get_repo_filename(s)
|