Skip to content

Commit 272b9fa

Browse files
committed
bugs: support ~/.bugzrc fir api-key extraction
Resolves: #162 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent ee5abb2 commit 272b9fa

3 files changed

Lines changed: 43 additions & 26 deletions

File tree

src/pkgdev/scripts/argparsers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22
import subprocess
3+
from configparser import ConfigParser
4+
from pathlib import Path
35

46
from pkgcore.repository import errors as repo_errors
57
from snakeoil.cli.arghparse import ArgumentParser
@@ -41,3 +43,40 @@ def _determine_git_repo(parser, namespace):
4143
pass
4244

4345
namespace.git_repo = path
46+
47+
48+
class BugzillaApiKey:
49+
@classmethod
50+
def mangle_argparser(cls, parser):
51+
parser.add_argument(
52+
"--api-key",
53+
metavar="KEY",
54+
help="Bugzilla API key",
55+
docs="""
56+
The Bugzilla API key to use for authentication. WARNING: using this
57+
option will expose your API key to other users of the same system.
58+
Consider instead saving your API key in a file named ``~/.bugzrc``
59+
in an INI format like so::
60+
61+
[default]
62+
key = <your API key>
63+
64+
ANother supported option is to save your API key in a file named
65+
``~/.bugz_token``.
66+
""",
67+
)
68+
69+
parser.bind_delayed_default(1000, "api_key")(cls._default_api_key)
70+
71+
@staticmethod
72+
def _default_api_key(namespace, attr):
73+
"""Use all known arches by default."""
74+
if (bugz_rc_file := Path.home() / ".bugzrc").is_file():
75+
try:
76+
config = ConfigParser(default_section="default")
77+
config.read(bugz_rc_file)
78+
setattr(namespace, attr, config.get("default", "key"))
79+
except Exception as e:
80+
raise ValueError(f"failed parsing {bugz_rc_file}: {e}")
81+
elif (bugz_token_file := Path.home() / ".bugz_token").is_file():
82+
setattr(namespace, attr, bugz_token_file.read_text().strip())

src/pkgdev/scripts/pkgdev_bugs.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from snakeoil.formatters import Formatter
3131

3232
from ..cli import ArgumentParser
33-
from .argparsers import _determine_cwd_repo, cwd_repo_argparser
33+
from .argparsers import _determine_cwd_repo, cwd_repo_argparser, BugzillaApiKey
3434

3535
bugs = ArgumentParser(
3636
prog="pkgdev bugs",
@@ -39,16 +39,7 @@
3939
quiet=False,
4040
parents=(cwd_repo_argparser,),
4141
)
42-
bugs.add_argument(
43-
"--api-key",
44-
metavar="KEY",
45-
help="Bugzilla API key",
46-
docs="""
47-
The Bugzilla API key to use for authentication. WARNING: using this
48-
option will expose your API key to other users of the same system.
49-
Consider instead saving your API key in a file named ~/.bugz_token.
50-
""",
51-
)
42+
BugzillaApiKey.mangle_argparser(bugs)
5243
bugs.add_argument(
5344
"targets",
5445
metavar="target",
@@ -572,11 +563,6 @@ def main(options, out: Formatter, err: Formatter):
572563
for node in d.nodes:
573564
node.cleanup_keywords(search_repo)
574565

575-
if options.api_key is None:
576-
bugz_token_file = Path.home() / ".bugz_token"
577-
if bugz_token_file.is_file:
578-
options.api_key = bugz_token_file.read_text().strip()
579-
580566
if not d.nodes:
581567
out.write(out.fg("red"), "Nothing to do, exiting", out.reset)
582568
return 1

src/pkgdev/scripts/pkgdev_tatt.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@
1515
from snakeoil.cli import arghparse
1616

1717
from ..cli import ArgumentParser
18+
from .argparsers import BugzillaApiKey
1819

1920
tatt = ArgumentParser(prog="pkgdev tatt", description=__doc__, verbose=False, quiet=False)
20-
tatt.add_argument(
21-
"--api-key",
22-
metavar="KEY",
23-
help="Bugzilla API key",
24-
docs="""
25-
The Bugzilla API key to use for authentication. Used mainly to overcome
26-
rate limiting done by bugzilla server. This tool doesn't perform any
27-
bug editing, just fetching info for the bug.
28-
""",
29-
)
21+
BugzillaApiKey.mangle_argparser(tatt)
3022
tatt.add_argument(
3123
"-j",
3224
"--job-name",

0 commit comments

Comments
 (0)