Skip to content

Commit 1d12caf

Browse files
committed
bugs: support piping package list from stdin
When calling the tool without packages (for example just `pkgdev bugs`), and you pipe (from a file or another command) a list of packages, it will read the package list and use it. The input file can have empty lines, and comments. Everything after the first # is considered a comment. Whitespaces are stripped. Resolves: #136 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent a691a6c commit 1d12caf

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/pkgdev/scripts/pkgdev_bugs.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Automatic bugs filer"""
22

33
import json
4+
import sys
45
import urllib.request as urllib
56
from collections import defaultdict
67
from functools import partial
@@ -19,7 +20,7 @@
1920
from pkgcore.repository import multiplex
2021
from pkgcore.restrictions import boolean, packages, values
2122
from pkgcore.test.misc import FakePkg
22-
from pkgcore.util import commandline
23+
from pkgcore.util import commandline, parserestrict
2324
from snakeoil.cli import arghparse
2425
from snakeoil.cli.input import userquery
2526
from snakeoil.formatters import Formatter
@@ -47,7 +48,7 @@
4748
bugs.add_argument(
4849
"targets",
4950
metavar="target",
50-
nargs="+",
51+
nargs="*",
5152
action=commandline.StoreTarget,
5253
help="extended atom matching of packages",
5354
)
@@ -285,10 +286,8 @@ def _find_dependencies(self, pkg: package, keywords: set[str]):
285286
match = self.find_best_match(deps, pkgset)
286287
except (ValueError, IndexError):
287288
deps_str = " , ".join(map(str, deps))
288-
self.err.write(
289-
self.err.fg("red"),
289+
self.err.error(
290290
f"unable to find match for restrictions: {deps_str}",
291-
self.err.reset,
292291
)
293292
raise
294293
results[match].add(keyword)
@@ -461,6 +460,18 @@ def observe(node: GraphNode):
461460
node.file_bug(api_key, auto_cc_arches, observe)
462461

463462

463+
def _load_from_stdin(out: Formatter, err: Formatter):
464+
if not sys.stdin.isatty():
465+
out.warn("No packages were specified, reading from stdin...")
466+
for line in sys.stdin.readlines():
467+
if line := line.split("#", 1)[0].strip():
468+
yield line, parserestrict.parse_match(line)
469+
# reassign stdin to allow interactivity (currently only works for unix)
470+
sys.stdin = open("/dev/tty")
471+
else:
472+
raise arghparse.ArgumentError(None, "reading from stdin is only valid when piping data in")
473+
474+
464475
def _parse_targets(search_repo, targets):
465476
for _, target in targets:
466477
try:
@@ -472,6 +483,7 @@ def _parse_targets(search_repo, targets):
472483
@bugs.bind_main_func
473484
def main(options, out: Formatter, err: Formatter):
474485
search_repo = options.search_repo
486+
options.targets = options.targets or list(_load_from_stdin(out, err))
475487
targets = list(_parse_targets(search_repo, options.targets))
476488
d = DependencyGraph(out, err, options)
477489
d.build_full_graph(targets)

0 commit comments

Comments
 (0)