Unverified Commit dfdc743e authored by Maxim Baz's avatar Maxim Baz
Browse files

Implement basic db-move

parent 2db01263
import asyncio
import json
from argparse import ArgumentParser
from fcntl import LOCK_EX, flock
from pathlib import Path
from sys import argv, exit
async def main(repo_from, repo_to, pkgbases) -> int:
metadir = (Path(argv[0]).parent / "meta").resolve(strict=True)
lockfile = (metadir / "dbscripts.lock").open(mode="w")
flock(lockfile, LOCK_EX)
# check that user has access to these repos
if not (metadir / repo_from).is_dir():
raise RuntimeError(f"Source repo '{repo_from}' is not allowed")
if not (metadir / repo_to).is_dir():
raise RuntimeError(f"Target repo '{repo_to}' is not allowed")
if repo_from == repo_to:
raise RuntimeError("repo_from and repo_to cannot be the same")
# load existing repo metadata
meta = {
repo: {
p.name[:-5]: json.load((metadir / repo / p).open())
for p in (metadir / repo).glob("*.json")
for repo in [repo_from, repo_to]
# validate that the move is possible
for pkgbase in pkgbases:
if pkgbase not in meta[repo_from]:
raise RuntimeError(
f"pkgbase '{pkgbase}' is not present in the source repo '{repo_from}'"
if pkgbase in meta[repo_to]:
raise RuntimeError(
f"pkgbase '{pkgbase}' is already present in the destination repo '{repo_to}'"
# move package info within `meta` objects and update json files
Path(metadir / repo_to).mkdir(exist_ok=True)
for pkgbase in pkgbases:
meta[repo_to][pkgbase] = meta[repo_from][pkgbase]
del meta[repo_from][pkgbase]
(metadir / repo_from / f"{pkgbase}.json").rename(
metadir / repo_to / f"{pkgbase}.json"
# rebuild DB file using `meta` object
return 0
if __name__ == "__main__":
parser = ArgumentParser(description="Move pkgbase(s) between repositories")
parser.add_argument("repo_from", help="repo to move pkgbase(s) from")
parser.add_argument("repo_to", help="repo to move pkgbase(s) to")
parser.add_argument("pkgbase", help="pkgbase(s) to move", nargs="+")
args = parser.parse_args()
exit(asyncio.run(main(args.repo_from, args.repo_to, args.pkgbase)))
Supports Markdown
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