From 1a61297755aee21e34be358b2f4d84d52f5abd32 Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Oct 07 2021 11:56:11 +0000 Subject: Enable Python argcomplete Argcomplete library supports custom compleso there is no need to maintain bash script for bash-completion. This commit introduces argcomplete to fedpkg. JIRA: RHELCMP-89 Signed-off-by: Dominik Rumian --- diff --git a/conf/bash-completion/fedpkg.bash b/conf/bash-completion/fedpkg.bash index a7a71f9..d949e28 100644 --- a/conf/bash-completion/fedpkg.bash +++ b/conf/bash-completion/fedpkg.bash @@ -388,4 +388,4 @@ _fedpkg_namespaces() # sh-indent-comment: t # indent-tabs-mode: nil # End: -# ex: ts=4 sw=4 et filetype=sh +# ex: ts=4 sw=4 et filetype=sh \ No newline at end of file diff --git a/fedpkg/__main__.py b/fedpkg/__main__.py index 09e55cb..c440f08 100644 --- a/fedpkg/__main__.py +++ b/fedpkg/__main__.py @@ -9,6 +9,8 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license. +# PYTHON_ARGCOMPLETE_OK + import logging import os import sys diff --git a/fedpkg/cli.py b/fedpkg/cli.py index df9c5c4..175da1c 100644 --- a/fedpkg/cli.py +++ b/fedpkg/cli.py @@ -29,6 +29,8 @@ from six.moves.configparser import NoOptionError, NoSectionError, ConfigParser from six.moves.urllib_parse import urlparse from fedpkg.bugzilla import BugzillaClient +from fedpkg.completers import (build_arches, list_targets, + fedpkg_packages, distgit_branches) from fedpkg.utils import (assert_new_tests_repo, assert_valid_epel_package, config_get_safely, do_add_remote, do_fork, expand_release, get_dist_git_url, @@ -97,6 +99,8 @@ def check_bodhi_version(): class fedpkgClient(cliClient): def __init__(self, config, name=None): self.DEFAULT_CLI_NAME = 'fedpkg' + super(fedpkgClient, self).setup_completers() + self.setup_completers() super(fedpkgClient, self).__init__(config, name) self.setup_fed_subparsers() @@ -125,6 +129,18 @@ class fedpkgClient(cliClient): self.register_set_distgit_token() self.register_set_pagure_token() + def setup_completers(self): + """ + Set specific argument completers for fedpkg. Structure, where + are these assignments (name -> method) stored, is in the parent + class and have to be filled before __init__ (containing argument + parser definitions) is called there. + """ + cliClient.set_completer("build_arches", build_arches) + cliClient.set_completer("list_targets", list_targets) + cliClient.set_completer("packages", fedpkg_packages) + cliClient.set_completer("branches", distgit_branches) + # Target registry goes here def register_update(self): description = textwrap.dedent(''' diff --git a/fedpkg/completers.py b/fedpkg/completers.py new file mode 100644 index 0000000..83427b7 --- /dev/null +++ b/fedpkg/completers.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# completers.py - custom argument completers module for fedpkg +# +# Copyright (C) 2019 Red Hat Inc. +# Author(s): Ondrej Nosek , +# Dominik Rumian +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. See http://www.gnu.org/copyleft/gpl.html for +# the full text of the license. + +import subprocess +import sys + +from argcomplete.completers import ChoicesCompleter + + +def distgit_branches(**kwargs): + format = "--format=\"%(refname:short)\"" + # TODO: Somehow get $path from argument-string + remotes = subprocess.check_output( + "git for-each-ref %s 'refs/remotes' | sed 's,.*/,,'" % format, + shell=True).decode(sys.stdout.encoding).split() + heads = subprocess.check_output( + " git for-each-ref %s 'refs/heads'" % format, + shell=True).decode(sys.stdout.encoding).split() + return remotes + heads + + +def fedpkg_packages(prefix, **kwargs): + if len(prefix): + return subprocess.check_output( + "repoquery -C --qf=%{{sourcerpm}} \"{}*\" 2>/dev/null | sed -r " + "'s/(-[^-]*){{2}}\\.src\\.rpm$//'" + .format(prefix), shell=True).decode(sys.stdout.encoding).split() + else: + return [] + + +def list_targets(**kwargs): + return tuple(subprocess.check_output("koji list-targets --quiet 2>/dev/null | cut -d\" \" -f1", + shell=True).decode(sys.stdout.encoding).split()) + + +build_arches = ChoicesCompleter(("i386", "i686", "x86_64", "armv5tel", + "armv7hl", "armv7hnl", "ppc", "ppc64", + "ppc64le", "ppc64p7", "s390", "s390x")) diff --git a/requirements.txt b/requirements.txt index 9e4f07a..7e52a2c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +argcomplete bodhi-client openidc-client python-bugzilla diff --git a/setup.py b/setup.py index 0c5b56e..5af64de 100755 --- a/setup.py +++ b/setup.py @@ -10,12 +10,6 @@ except ImportError: from commands import getstatusoutput -def bash_completion_dir(): - (sts, output) = getstatusoutput( - 'pkg-config --variable=completionsdir bash-completion') - return output if not sts and output else '/etc/bash_completion.d' - - project_dir = os.path.dirname(os.path.realpath(__file__)) requirements = os.path.join(project_dir, 'requirements.txt') tests_requirements = os.path.join(project_dir, 'tests-requirements.txt') @@ -40,8 +34,7 @@ setup( license="GPLv2+", url="https://pagure.io/fedpkg", packages=find_packages(), - data_files=[(bash_completion_dir(), ['conf/bash-completion/fedpkg.bash']), - ('/etc/rpkg', ['conf/etc/rpkg/fedpkg.conf', + data_files=[('/etc/rpkg', ['conf/etc/rpkg/fedpkg.conf', 'conf/etc/rpkg/fedpkg-stage.conf']), ('/usr/share/zsh/site-functions', ['conf/zsh-completion/_fedpkg']), ],