db-remove 1.4 KB
Newer Older
Maxim Baz's avatar
Maxim Baz committed
1
2
3
4
5
6
7
8
#!/usr/bin/python

import asyncio
import json
from argparse import ArgumentParser
from fcntl import LOCK_EX, flock
from pathlib import Path
from sys import argv, exit
9
from lib.dbwrite import generate_dbs
Maxim Baz's avatar
Maxim Baz committed
10
11
12
13
14
15
16
17


async def main(repo, pkgbases) -> int:
    metadir = (Path(argv[0]).parent / "meta").resolve(strict=True)

    lockfile = (metadir / "dbscripts.lock").open(mode="w")
    flock(lockfile, LOCK_EX)

18
19
20
21
    # check that user has access to this repo
    if not (metadir / repo).is_dir():
        raise RuntimeError(f"repo '{repo}' is not allowed")

Maxim Baz's avatar
Maxim Baz committed
22
23
24
    # load existing repo metadata
    meta = {
        repo: {
25
            p.stem: json.load((metadir / repo / p).open())
Maxim Baz's avatar
Maxim Baz committed
26
27
28
29
30
31
32
33
34
35
36
37
            for p in (metadir / repo).glob("*.json")
        }
    }

    # remove packages from meta and corresponding json files
    for pkgbase in pkgbases:
        if pkgbase not in meta[repo]:
            raise RuntimeError(f"pkgbase '{pkgbase}' is not present in '{repo}' repo")
        del meta[repo][pkgbase]
        (metadir / repo / f"{pkgbase}.json").unlink()

    # rebuild DB file using `meta` object
38
    generate_dbs(meta)
Maxim Baz's avatar
Maxim Baz committed
39
40
41
42
43

    return 0


if __name__ == "__main__":
44
    parser = ArgumentParser(description="Remove pkgbase(s) from a repo")
Maxim Baz's avatar
Maxim Baz committed
45
    parser.add_argument("repo", help="repository name")
46
    parser.add_argument("pkgbase", help="pkgbase(s) to remove", nargs="+")
Maxim Baz's avatar
Maxim Baz committed
47
48
    args = parser.parse_args()
    exit(asyncio.run(main(args.repo, args.pkgbase)))