From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id EC881158015 for ; Fri, 22 Dec 2023 13:28:35 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3FDF22BC078; Fri, 22 Dec 2023 13:28:35 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 201672BC078 for ; Fri, 22 Dec 2023 13:28:35 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 2DB1733FE49 for ; Fri, 22 Dec 2023 13:28:34 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id C59AA125F for ; Fri, 22 Dec 2023 13:28:32 +0000 (UTC) From: "Arthur Zamarin" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Arthur Zamarin" Message-ID: <1703251636.272b9fabe7bfba705c6bfbbdfff901535f1d305b.arthurzam@gentoo> Subject: [gentoo-commits] proj/pkgcore/pkgdev:main commit in: src/pkgdev/scripts/ X-VCS-Repository: proj/pkgcore/pkgdev X-VCS-Files: src/pkgdev/scripts/argparsers.py src/pkgdev/scripts/pkgdev_bugs.py src/pkgdev/scripts/pkgdev_tatt.py X-VCS-Directories: src/pkgdev/scripts/ X-VCS-Committer: arthurzam X-VCS-Committer-Name: Arthur Zamarin X-VCS-Revision: 272b9fabe7bfba705c6bfbbdfff901535f1d305b X-VCS-Branch: main Date: Fri, 22 Dec 2023 13:28:32 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 76345b59-cbc0-4727-a8d4-e72725000f81 X-Archives-Hash: d3dcba8bd6c19ffe1c4eb7ac07a485f1 commit: 272b9fabe7bfba705c6bfbbdfff901535f1d305b Author: Arthur Zamarin gentoo org> AuthorDate: Fri Dec 22 13:27:16 2023 +0000 Commit: Arthur Zamarin gentoo org> CommitDate: Fri Dec 22 13:27:16 2023 +0000 URL: https://gitweb.gentoo.org/proj/pkgcore/pkgdev.git/commit/?id=272b9fab bugs: support ~/.bugzrc fir api-key extraction Resolves: https://github.com/pkgcore/pkgdev/issues/162 Signed-off-by: Arthur Zamarin gentoo.org> src/pkgdev/scripts/argparsers.py | 39 +++++++++++++++++++++++++++++++++++++++ src/pkgdev/scripts/pkgdev_bugs.py | 18 ++---------------- src/pkgdev/scripts/pkgdev_tatt.py | 12 ++---------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/pkgdev/scripts/argparsers.py b/src/pkgdev/scripts/argparsers.py index ac1a758..76479ff 100644 --- a/src/pkgdev/scripts/argparsers.py +++ b/src/pkgdev/scripts/argparsers.py @@ -1,5 +1,7 @@ import os import subprocess +from configparser import ConfigParser +from pathlib import Path from pkgcore.repository import errors as repo_errors from snakeoil.cli.arghparse import ArgumentParser @@ -41,3 +43,40 @@ def _determine_git_repo(parser, namespace): pass namespace.git_repo = path + + +class BugzillaApiKey: + @classmethod + def mangle_argparser(cls, parser): + parser.add_argument( + "--api-key", + metavar="KEY", + help="Bugzilla API key", + docs=""" + The Bugzilla API key to use for authentication. WARNING: using this + option will expose your API key to other users of the same system. + Consider instead saving your API key in a file named ``~/.bugzrc`` + in an INI format like so:: + + [default] + key = + + ANother supported option is to save your API key in a file named + ``~/.bugz_token``. + """, + ) + + parser.bind_delayed_default(1000, "api_key")(cls._default_api_key) + + @staticmethod + def _default_api_key(namespace, attr): + """Use all known arches by default.""" + if (bugz_rc_file := Path.home() / ".bugzrc").is_file(): + try: + config = ConfigParser(default_section="default") + config.read(bugz_rc_file) + setattr(namespace, attr, config.get("default", "key")) + except Exception as e: + raise ValueError(f"failed parsing {bugz_rc_file}: {e}") + elif (bugz_token_file := Path.home() / ".bugz_token").is_file(): + setattr(namespace, attr, bugz_token_file.read_text().strip()) diff --git a/src/pkgdev/scripts/pkgdev_bugs.py b/src/pkgdev/scripts/pkgdev_bugs.py index 5d3672c..7c11d31 100644 --- a/src/pkgdev/scripts/pkgdev_bugs.py +++ b/src/pkgdev/scripts/pkgdev_bugs.py @@ -30,7 +30,7 @@ from snakeoil.cli.input import userquery from snakeoil.formatters import Formatter from ..cli import ArgumentParser -from .argparsers import _determine_cwd_repo, cwd_repo_argparser +from .argparsers import _determine_cwd_repo, cwd_repo_argparser, BugzillaApiKey bugs = ArgumentParser( prog="pkgdev bugs", @@ -39,16 +39,7 @@ bugs = ArgumentParser( quiet=False, parents=(cwd_repo_argparser,), ) -bugs.add_argument( - "--api-key", - metavar="KEY", - help="Bugzilla API key", - docs=""" - The Bugzilla API key to use for authentication. WARNING: using this - option will expose your API key to other users of the same system. - Consider instead saving your API key in a file named ~/.bugz_token. - """, -) +BugzillaApiKey.mangle_argparser(bugs) bugs.add_argument( "targets", metavar="target", @@ -572,11 +563,6 @@ def main(options, out: Formatter, err: Formatter): for node in d.nodes: node.cleanup_keywords(search_repo) - if options.api_key is None: - bugz_token_file = Path.home() / ".bugz_token" - if bugz_token_file.is_file: - options.api_key = bugz_token_file.read_text().strip() - if not d.nodes: out.write(out.fg("red"), "Nothing to do, exiting", out.reset) return 1 diff --git a/src/pkgdev/scripts/pkgdev_tatt.py b/src/pkgdev/scripts/pkgdev_tatt.py index 37454fc..79624cd 100644 --- a/src/pkgdev/scripts/pkgdev_tatt.py +++ b/src/pkgdev/scripts/pkgdev_tatt.py @@ -15,18 +15,10 @@ from pkgcore.util import packages as pkgutils from snakeoil.cli import arghparse from ..cli import ArgumentParser +from .argparsers import BugzillaApiKey tatt = ArgumentParser(prog="pkgdev tatt", description=__doc__, verbose=False, quiet=False) -tatt.add_argument( - "--api-key", - metavar="KEY", - help="Bugzilla API key", - docs=""" - The Bugzilla API key to use for authentication. Used mainly to overcome - rate limiting done by bugzilla server. This tool doesn't perform any - bug editing, just fetching info for the bug. - """, -) +BugzillaApiKey.mangle_argparser(tatt) tatt.add_argument( "-j", "--job-name",