Verified Commit 5d842eb0 authored by Jan Alexander Steffens (heftig)'s avatar Jan Alexander Steffens (heftig)
Browse files

db-write: Code that transforms JSON DB into Pacman DB

parent 208ad198
import io
import json
import tarfile
from argparse import ArgumentParser
from pathlib import Path
from sys import argv, exit
def writefield(out: io.StringIO, name: str, field):
if isinstance(field, list):
print(f"%{name}%", *field, sep="\n", end="\n\n", file=out)
elif field is not None:
print(f"%{name}%", field, sep="\n", end="\n\n", file=out)
def descfile(base: str, version: str, data) -> bytes:
out = io.StringIO()
writefield(out, "FILENAME", data["filename"])
writefield(out, "NAME", data["name"])
writefield(out, "BASE", base)
writefield(out, "VERSION", version)
writefield(out, "DESC", data.get("desc"))
writefield(out, "GROUPS", data.get("groups"))
writefield(out, "CSIZE", data.get("csize"))
writefield(out, "ISIZE", data.get("isize"))
# add checksums
writefield(out, "MD5SUM", data.get("md5sum"))
writefield(out, "SHA256SUM", data.get("sha256sum"))
# add PGP sig
writefield(out, "PGPSIG", data.get("pgpsig"))
writefield(out, "URL", data.get("url"))
writefield(out, "LICENSE", data.get("license"))
writefield(out, "ARCH", data.get("arch"))
writefield(out, "BUILDDATE", data.get("builddate"))
writefield(out, "PACKAGER", data.get("packager"))
writefield(out, "REPLACES", data.get("replaces"))
writefield(out, "CONFLICTS", data.get("conflicts"))
writefield(out, "PROVIDES", data.get("provides"))
writefield(out, "DEPENDS", data.get("depends"))
writefield(out, "OPTDEPENDS", data.get("optdepends"))
writefield(out, "MAKEDEPENDS", data.get("makedepends"))
writefield(out, "CHECKDEPENDS", data.get("checkdepends"))
return out.getvalue().encode()
def main(args) -> int:
repodir = (Path(argv[0]).parent / f"workdirs/x86_64/{args.repo}").resolve(
if not repodir.exists():
raise RuntimeError(f"No repo named {args.repo}")
with"{args.repo}.db.tar.gz", mode="w:gz") as tf:
for basefile in repodir.glob("*.json"):
base = basefile.stem
with as f:
data = json.load(f)
version = data["version"]
for package in data["packages"]:
name = package["name"]
dirname = f"{name}-{version}"
entry = tarfile.TarInfo(dirname)
entry.type = tarfile.DIRTYPE
dfile = descfile(base, version, package)
entry = tarfile.TarInfo(f"{dirname}/desc")
entry.size = len(dfile)
tf.addfile(entry, io.BytesIO(dfile))
return 0
parser = ArgumentParser(description="db-write mockup.")
parser.add_argument("repo", help="Repository to write DB for")
args = parser.parse_args()
"version": "1-1",
"packages": [
"filename": "foo-1-1-x86_64.pkg.tar.xz",
"name": "foo",
"version": "1-1",
"desc": "Foo demo package",
"csize": 1234,
"isize": 12345,
"md5sum": "123456",
"sha256sum": "1234567",
"pgpsig": "some base64 data",
"url": "",
"licenses": ["foo", "bar"],
"arch": "any",
"builddate": 0,
"packager": "Bruce \"Batman\" Wayne",
"depends": ["bbq", "qux"],
"makedepends": ["something-else"]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment