dbwrite.py 2.31 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
import io
import tarfile


def writefield(out: io.StringIO, name: str, field):
    if field is None:
        pass
    elif not isinstance(field, list):
        print(f"%{name}%", field, sep="\n", end="\n\n", file=out)
    elif field:
        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 generate_dbs(meta):
    for repo, pkgbases in meta.items():
        with tarfile.open(f"{repo}.db.tar.gz", mode="w:gz") as tf:
            for pkgbase, pkginfo in pkgbases.items():
                version = pkginfo["version"]
                for package in pkginfo["packages"]:
                    name = package["name"]
                    dirname = f"{name}-{version}"

                    entry = tarfile.TarInfo(dirname)
                    entry.type = tarfile.DIRTYPE
                    tf.addfile(entry)

                    dfile = descfile(pkgbase, version, package)
                    entry = tarfile.TarInfo(f"{dirname}/desc")
                    entry.size = len(dfile)
                    tf.addfile(entry, io.BytesIO(dfile))