diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2023-12-22 15:27:16 +0200 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2023-12-22 15:27:16 +0200 |
commit | 272b9fabe7bfba705c6bfbbdfff901535f1d305b (patch) | |
tree | 05e28fb3a3c138b56b79210f30cb15f0abc8eae0 | |
parent | bugs: handle merging of top level nodes (diff) | |
download | pkgdev-272b9fabe7bfba705c6bfbbdfff901535f1d305b.tar.gz pkgdev-272b9fabe7bfba705c6bfbbdfff901535f1d305b.tar.bz2 pkgdev-272b9fabe7bfba705c6bfbbdfff901535f1d305b.zip |
bugs: support ~/.bugzrc fir api-key extraction
Resolves: https://github.com/pkgcore/pkgdev/issues/162
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r-- | src/pkgdev/scripts/argparsers.py | 39 | ||||
-rw-r--r-- | src/pkgdev/scripts/pkgdev_bugs.py | 18 | ||||
-rw-r--r-- | 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 = <your API 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", |