db-write 2.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python

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(
        strict=True
    )
    if not repodir.exists():
        raise RuntimeError(f"No repo named {args.repo}")

    with tarfile.open(f"{args.repo}.db.tar.gz", mode="w:gz") as tf:
        for basefile in repodir.glob("*.json"):
            base = basefile.stem

            with basefile.open() 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
                tf.addfile(entry)

                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()
exit(main(args))