import os import sys import hashlib import subprocess from pathlib import Path import toml import huepy import requests from jinja2 import Environment, FileSystemLoader PACKAGE_NAME = os.environ["PACKAGE_NAME"] BUILDS_PATH = Path(f"/tmp/{PACKAGE_NAME}/builds") PACKAGE_FILE = f"/tmp/{PACKAGE_NAME}/package.toml" package = toml.load(PACKAGE_FILE) print( huepy.run( f'Building {PACKAGE_NAME} v{package["version"]}_{package["build_number"]}-{package["aur_build"]}' ) ) if os.path.isdir(BUILDS_PATH / Path(f"{package['version']}-{package['build_number']}")): print( huepy.bad( f"{PACKAGE_NAME} {package['version']}-{package['build_number']} is already present." ) ) exit(1) def md5sum(url): response = requests.get(url, stream=True) hash_md5 = hashlib.md5() for chunk in response.iter_content(chunk_size=512): hash_md5.update(chunk) return hash_md5.hexdigest() def check_result(result, exit_on_error=True): if result.returncode != 0: print(huepy.bad("Error")) print(huepy.bad(result.stderr)) if exit_on_error: sys.exit(1) class Builder: def update_package_definition(self, package): if "i686" in package["arch"]: source_i686 = package["sources"]["i686"]["url"].format(**package) package["sources"]["i686"]["url"] = source_i686 package["sources"]["i686"]["md5sum"] = md5sum(source_i686) if "x86_64" in package["arch"]: source_x86_64 = package["sources"]["x86_64"]["url"].format(**package) package["sources"]["x86_64"]["url"] = source_x86_64 package["sources"]["x86_64"]["md5sum"] = md5sum(source_x86_64) class DebianBuilder(Builder): template = "PKGBUILD.debian.j2" class PythonVirtualenvBuilder(Builder): template = "PKGBUILD.python-virtualenv.j2" def update_package_definition(self, package): # We don't need this. pass BUILDERS = { "debian": DebianBuilder, "python-virtualenv": PythonVirtualenvBuilder, } print(huepy.run(f"Using {package['builder']['type']} builder")) builder = BUILDERS[package["builder"]["type"]]() builder.update_package_definition(package) print(huepy.run("Generating PKGBUILD")) env = Environment( loader=FileSystemLoader(["templates", f"/tmp/{PACKAGE_NAME}"]), trim_blocks=True ) if not os.path.isfile(f"/tmp/{PACKAGE_NAME}/PKGBUILD"): pkgbuild_template = env.get_template(builder.template) else: print(huepy.info("Using package-specific template")) pkgbuild_template = env.get_template("PKGBUILD") pkgbuild = pkgbuild_template.render(**package) build_path = BUILDS_PATH / Path("{version}_{build_number}-{aur_build}".format(**package)) os.makedirs(build_path, exist_ok=True) print(huepy.info(f"Writing PKGBUILD file...")) with open("%s/PKGBUILD" % build_path, "w") as handler: handler.write(pkgbuild) print(huepy.info("Wiring .SRCINFO file...")) result = subprocess.run( "makepkg --printsrcinfo > .SRCINFO", shell=True, cwd=build_path, capture_output=True ) if result.returncode != 0: print(huepy.bad("Error writing .SRCINFO file")) print(result.stderr) print(result.stdout) else: print(huepy.good("Build finished")) subprocess.run("sudo pacman -Syy", shell=True, capture_output=False) print(huepy.info("Testing: makepkg")) check_result( subprocess.run("makepkg --noconfirm -Acs", shell=True, cwd=build_path, capture_output=False) ) print(huepy.info("Testing: pacman -S --noconfirm")) check_result( subprocess.run( "sudo pacman -U --noconfirm *.pkg.tar.zst", shell=True, cwd=build_path, capture_output=False, ) ) if "test" in package: subprocess.run("ls /usr/local/bin", shell=True) print(huepy.info(f"Testing: {package['test']['command']}")) check_result( subprocess.run( f"{package['test']['command']}", shell=True, cwd=build_path, capture_output=False, ) ) print(huepy.good("Test successful"))