Skip to content

Instantly share code, notes, and snippets.

@lubomir
Created July 24, 2024 10:34
Show Gist options
  • Save lubomir/9c9dd031203c07db92b9f88ea5c60333 to your computer and use it in GitHub Desktop.
Save lubomir/9c9dd031203c07db92b9f88ea5c60333 to your computer and use it in GitHub Desktop.
import argparse
import sys
import koji
import yaml
parser = argparse.ArgumentParser()
parser.add_argument("TAG")
parser.add_argument("--koji-profile", default="brew")
parser.add_argument("--arch", action="append")
parser.add_argument("--sigkey", default="")
args = parser.parse_args()
m = koji.get_profile_module(args.koji_profile)
client = m.ClientSession(m.config.server)
pathinfo = m.PathInfo(m.config.topurl)
arches = args.arch
if not arches:
# No arches were provided, so let's use whatever is configured for the
# first build target building into the tag.
taginfo = client.getTag(args.TAG)
targets = client.getBuildTargets(destTagID=taginfo["id"])
try:
buildtaginfo = client.getTag(targets[0]["build_tag"])
except IndexError:
print("Can not figure out arches", file=sys.stderr)
sys.exit(1)
arches = buildtaginfo["arches"]
packages_per_arch = {}
packages, builds = client.listTaggedRPMS(args.TAG, inherit=True, latest=True)
builds_by_id = {b["id"]: b for b in builds}
def add_package(arch, pkg):
epoch = f"{pkg['epoch']}:" if pkg["epoch"] else ""
checksums = client.getRPMChecksums(pkg["id"], checksum_types=["sha256"])
checksum = checksums[args.sigkey]["sha256"]
build_dir = pathinfo.build(builds_by_id[pkg["build_id"]])
if args.sigkey:
rpm_path = pathinfo.signed(pkg, args.sigkey)
else:
rpm_path = pathinfo.rpm(pkg)
packages_per_arch.setdefault(arch, []).append(
{
"url": f"{build_dir}/{rpm_path}",
"repoid": args.koji_profile,
"size": pkg["size"],
"checksum": f"sha256:{checksum}",
"name": pkg["name"],
"evr": f"{epoch}{pkg['version']}-{pkg['release']}",
}
)
for package in packages:
if package["arch"] == "noarch":
for arch in arches:
add_package(arch, package)
else:
add_package(package["arch"], package)
lockfile = {"lockfileVersion": 1, "lockfileVendor": "redhat", "arches": []}
for arch in sorted(packages_per_arch):
lockfile["arches"].append(
{
"arch": arch,
"packages": sorted(packages_per_arch[arch], key=lambda rpm: rpm["url"]),
}
)
yaml.dump(lockfile, sys.stdout, sort_keys=False, explicit_start=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment