2019-09-24 11:15:39 +00:00
|
|
|
import os
|
|
|
|
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"]}'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
class DebianBuilder:
|
|
|
|
template = "PKGBUILD.debian.j2"
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-09-24 14:18:31 +00:00
|
|
|
class PythonVirtualenvBuilder:
|
|
|
|
template = "PKGBUILD.python-virtualenv.j2"
|
2019-09-24 11:15:39 +00:00
|
|
|
|
2019-09-24 14:18:31 +00:00
|
|
|
def update_package_definition(self, package):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
BUILDERS = {"debian": DebianBuilder, "python-virtualenv": PythonVirtualenvBuilder}
|
|
|
|
|
|
|
|
print(huepy.run(f"Using {package['builder']['type']} builder"))
|
|
|
|
builder = BUILDERS[package["builder"]["type"]]()
|
2019-09-24 11:15:39 +00:00
|
|
|
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}".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)
|
|
|
|
|
|
|
|
print(huepy.good("Build finished"))
|