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

lsrepo: Clean up, avoid warnings

parent 2c7db4b8
Pipeline #15756 passed with stage
in 43 seconds
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging
from argparse import ArgumentParser from argparse import ArgumentParser
from asyncio import gather, get_event_loop, create_subprocess_exec from asyncio import create_subprocess_exec, gather, run
from asyncio.subprocess import DEVNULL, PIPE from asyncio.subprocess import DEVNULL, PIPE
from pathlib import Path from pathlib import Path
from shlex import quote from shlex import quote
from warnings import warn from warnings import warn
import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -17,7 +16,9 @@ class DBParser: ...@@ -17,7 +16,9 @@ class DBParser:
self._packages = {} self._packages = {}
def _assert_state(self, *wanted): def _assert_state(self, *wanted):
assert self._state in wanted, "state is {!r}, not in {!r}".format(self._state, wanted) assert self._state in wanted, "state is {!r}, not in {!r}".format(
self._state, wanted
)
def _add_package(self): def _add_package(self):
name, base, version = self._name, self._base, self._version name, base, version = self._name, self._base, self._version
...@@ -31,8 +32,11 @@ class DBParser: ...@@ -31,8 +32,11 @@ class DBParser:
pkg = self._packages.setdefault(base, {}) pkg = self._packages.setdefault(base, {})
pkg.setdefault("names", set()).add(name) pkg.setdefault("names", set()).add(name)
if pkg.setdefault("version", version) != version: if pkg.setdefault("version", version) != version:
warn("Conflicting versions for pkgbase {!r}: {!r} {!r}".format( warn(
base, pkg["version"], version)) "Conflicting versions for pkgbase {!r}: {!r} {!r}".format(
base, pkg["version"], version
)
)
self._name = self._base = self._version = None self._name = self._base = self._version = None
...@@ -86,21 +90,18 @@ async def open_db(dbfile): ...@@ -86,21 +90,18 @@ async def open_db(dbfile):
args = "/usr/bin/bsdtar", "-xOf", str(dbfile), "*/desc" args = "/usr/bin/bsdtar", "-xOf", str(dbfile), "*/desc"
cmdline = " ".join(quote(a) for a in args) cmdline = " ".join(quote(a) for a in args)
log.debug("Running %s", cmdline) log.debug("Running %s", cmdline)
process = await create_subprocess_exec( process = await create_subprocess_exec(*args, stdout=PIPE, stderr=DEVNULL, env={})
*args, stdout=PIPE, stderr=DEVNULL, env={} process.cmdline = cmdline # type: ignore
)
process.cmdline = cmdline
return process return process
async def read_db(dbfile): async def read_db(dbfile):
state = name = base = version = None
packages = {} packages = {}
process = await open_db(dbfile) process = await open_db(dbfile)
with DBParser() as parser: with DBParser() as parser:
while True: while True:
line = await process.stdout.readline() line = await process.stdout.readline() # type: ignore
if not line: if not line:
break break
parser.parse_line(line.decode()) parser.parse_line(line.decode())
...@@ -118,11 +119,11 @@ async def read_repo(name, archs): ...@@ -118,11 +119,11 @@ async def read_repo(name, archs):
path = Path("/srv/ftp") / name / "os" / arch / (name + ".db") path = Path("/srv/ftp") / name / "os" / arch / (name + ".db")
db = {} db = {}
if path.exists(): if path.exists():
try: try:
db = await read_db(path) db = await read_db(path)
log.debug("Loaded repo name=%r arch=%r pkgs=%d", name, arch, len(db)) log.debug("Loaded repo name=%r arch=%r pkgs=%d", name, arch, len(db))
except Exception as e: except Exception as e:
log.warning("Failed to read repo name=%r arch=%r: %s", name, arch, e) log.warning("Failed to read repo name=%r arch=%r: %s", name, arch, e)
return frozenset([arch]), db return frozenset([arch]), db
repo = {} repo = {}
...@@ -153,12 +154,30 @@ def packages(repos): ...@@ -153,12 +154,30 @@ def packages(repos):
def parse_args(): def parse_args():
parser = ArgumentParser(description="List packages in FTP repositories") parser = ArgumentParser(description="List packages in FTP repositories")
mode = parser.add_mutually_exclusive_group() mode = parser.add_mutually_exclusive_group()
mode.add_argument("-b", "--bases", dest="mode", action="store_const", const="bases", mode.add_argument(
help="Only list pkgbases") "-b",
mode.add_argument("-n", "--names", dest="mode", action="store_const", const="names", "--bases",
help="Only list pkgnames") dest="mode",
parser.add_argument("-a", "--arch", metavar="ARCH", dest="archs", action="append", action="store_const",
help="Arch to read") const="bases",
help="Only list pkgbases",
)
mode.add_argument(
"-n",
"--names",
dest="mode",
action="store_const",
const="names",
help="Only list pkgnames",
)
parser.add_argument(
"-a",
"--arch",
metavar="ARCH",
dest="archs",
action="append",
help="Arch to read",
)
parser.add_argument("--debug", action="store_true", help="Enable debug output") parser.add_argument("--debug", action="store_true", help="Enable debug output")
parser.add_argument("repos", metavar="REPO", nargs="+", help="Repository to read") parser.add_argument("repos", metavar="REPO", nargs="+", help="Repository to read")
return parser.parse_args() return parser.parse_args()
...@@ -171,12 +190,7 @@ if args.debug: ...@@ -171,12 +190,7 @@ if args.debug:
logging.root.setLevel(logging.DEBUG) logging.root.setLevel(logging.DEBUG)
archs = frozenset(args.archs or ["x86_64"]) archs = frozenset(args.archs or ["x86_64"])
loop = get_event_loop() repos = run(read_repos(args.repos, archs))
try:
repos = loop.run_until_complete(read_repos(args.repos, archs))
finally:
loop.close()
if args.mode == "bases": if args.mode == "bases":
for base in set(bases(repos)): for base in set(bases(repos)):
...@@ -185,6 +199,7 @@ elif args.mode == "names": ...@@ -185,6 +199,7 @@ elif args.mode == "names":
for name in {n for p in packages(repos) for n in p["names"]}: for name in {n for p in packages(repos) for n in p["names"]}:
print(name) print(name)
else: else:
longestbase = longestarch = longestver = 0
if any(repos.values()): if any(repos.values()):
longestbase = max(len(b) for b in bases(repos)) longestbase = max(len(b) for b in bases(repos))
longestarch = len(" ".join(archs)) longestarch = len(" ".join(archs))
...@@ -203,9 +218,13 @@ else: ...@@ -203,9 +218,13 @@ else:
" \033[{}m{:^{}s}\033[39m" " \033[{}m{:^{}s}\033[39m"
" \033[36m{:<{}s}\033[39m" " \033[36m{:<{}s}\033[39m"
" {}".format( " {}".format(
base, longestbase, base,
34 if arch == archs else 31, " ".join(sorted(arch)), longestarch, longestbase,
pkg["version"], longestver, 34 if arch == archs else 31,
" ".join(sorted(pkg["names"])) " ".join(sorted(arch)),
longestarch,
pkg["version"],
longestver,
" ".join(sorted(pkg["names"])),
) )
) )
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