#305 [WIP]: Enable Python argcomplete
Opened 2 years ago by onosek. Modified 2 years ago

@@ -1,359 +0,0 @@ 

- # fedpkg bash completion

- 

- _fedpkg()

- {

-     COMPREPLY=()

- 

-     in_array()

-     {

-         local i

-         for i in $2; do

-             [[ $i = $1 ]] && return 0

-         done

-         return 1

-     }

- 

-     _filedir_exclude_paths()

-     {

-         _filedir "$@"

-         for ((i=0; i<=${#COMPREPLY[@]}; i++)); do

-             [[ ${COMPREPLY[$i]} =~ /?\.git/? ]] && unset COMPREPLY[$i]

-         done

-     }

- 

-     local cur prev

-     _get_comp_words_by_ref cur prev

- 

-     # global options

- 

-     local options="--help -v -q"

-     local options_value="--dist --release --user --path --user-config --name --namespace"

-     local commands="build chain-build ci clean clog clone co commit compile \

-     container-build diff gimmespec giturl help gitbuildhash import install lint \

-     local mockbuild mock-config module-build module-build-cancel \

-     module-build-local module-build-info module-build-watch module-overview \

-     new new-sources patch prep pull push retire request-branch request-repo \

-     scratch-build sources srpm switch-branch tag unused-patches update upload \

-     verify-files verrel override"

- 

-     # parse main options and get command

- 

-     local command=

-     local command_first=

-     local path=

- 

-     local i w

-     for (( i = 0; i < ${#COMP_WORDS[*]} - 1; i++ )); do

-         w="${COMP_WORDS[$i]}"

-         # option

-         if [[ ${w:0:1} = - ]]; then

-             if in_array "$w" "$options_value"; then

-                 ((i++))

-                 [[ "$w" = --path ]] && path="${COMP_WORDS[$i]}"

-             fi

-         # command

-         elif in_array "$w" "$commands"; then

-             command="$w"

-             command_first=$((i+1))

-             break

-         fi

-     done

- 

-     # complete base options

- 

-     if [[ -z $command ]]; then

-         if [[ $cur == -* ]]; then

-             COMPREPLY=( $(compgen -W "$options $options_value" -- "$cur") )

-             return 0

-         fi

- 

-         case "$prev" in

-             --dist | --release | --user | -u | --config)

-                 ;;

-             --path)

-                 _filedir_exclude_paths

-                 ;;

-             --namespace)

-                 COMPREPLY=( $(compgen -W "$(_fedpkg_namespaces)" -- "$cur") )

-                 ;;

-             *)

-                 COMPREPLY=( $(compgen -W "$commands" -- "$cur") )

-                 ;;

-         esac

- 

-         return 0

-     fi

- 

-     # parse command specific options

- 

-     local options=

-     local options_target= options_arches= options_branch= options_string= options_file= options_dir= options_srpm= options_mroot= options_builder= options_namespace=

-     local options_update_type= options_update_request=

-     local after= after_more=

- 

-     case $command in

-         help|gimmespec|gitbuildhash|giturl|lint|new|push|unused-patches|verrel)

-             ;;

-         build)

-             options="--nowait --background --skip-tag --scratch --skip-remote-rules-validation"

-             options_arches="--arches"

-             options_srpm="--srpm"

-             options_target="--target"

-             ;;

-         chain-build)

-             options="--nowait --background"

-             options_target="--target"

-             after="package"

-             after_more=true

-             ;;

-         clean)

-             options="--dry-run -x"

-             ;;

-         clog)

-             options="--raw"

-             ;;

-         clone|co)

-             options="--branches --anonymous"

-             options_branch="-b"

-             after="package"

-             ;;

-         commit|ci)

-             options="--push --clog --raw --tag --with-changelog"

-             options_string="--message"

-             options_file="--file"

-             after="file"

-             after_more=true

-             ;;

-         compile|install)

-             options="--short-circuit"

-             options_arch="--arch"

-             options_dir="--builddir"

-             ;;

-         container-build)

-             options="--scratch --nowait --repo-url --skip-remote-rules-validation"

-             options_arches="--arches"

-             options_target="--target"

-             ;;

-         diff)

-             options="--cached"

-             after="file"

-             after_more=true

-             ;;

-         import)

-             options="--create"

-             options_branch="--branch"

-             after="srpm"

-             ;;

-         lint)

-             options="--info"

-             ;;

-         local)

-             options="--md5"

-             options_arch="--arch"

-             options_string="--with --without"

-             options_dir="--builddir"

-             ;;

-         mock-config)

-             options="--target"

-             options_arch="--arch"

-             ;;

-         mockbuild)

-             options="--md5 --no-clean --no-cleanup-after --no-clean-all --shell"

-             options_string="--with --without"

-             options_mroot="--root --mock-config"

-             ;;

-         module-build)

-             options="--watch"

-             options_string="--optional"

-             ;;

-         module-build-local)

-             options="--skip-tests"

-             options_string="--add-local-build"

-             ;;

-         module-overview)

-             options="--unfinished"

-             options_string="--limit"

-             ;;

-         patch)

-             options="--rediff"

-             options_string="--suffix"

-             ;;

-         prep|verify-files)

-             options_arch="--arch"

-             options_dir="--builddir"

-             ;;

-         pull)

-             options="--rebase --no-rebase"

-             ;;

-         retire)

-             after_more=true

-             ;;

-         request-branch)

-             options="--no-git-branch --all-releases --no-auto-module"

-             options_string="--sl --repo"

-             ;;

-         request-repo)

-             options="--exception --no-initial-commit"

-             options_string="--description --monitor --upstreamurl --summary"

-             options_namespace="--namespace"

-             ;;

-         scratch-build)

-             options="--nowait --background"

-             options_target="--target"

-             options_arches="--arches"

-             options_srpm="--srpm"

-             ;;

-         sources)

-             options_dir="--outdir"

-             ;;

-         srpm)

-             options="--md5"

-             ;;

-         switch-branch)

-             options="--list"

-             after="branch"

-             ;;

-         tag)

-             options="--clog --raw --force --list --delete"

-             options_string="--message"

-             options_file="--file"

-             after_more=true

-             ;;

-         upload|new-sources)

-             after="file"

-             after_more=true

-             ;;

-         update)

-             options="--not-close-bugs --suggest-reboot --disable-autokarma"

-             options_string="--notes --bugs --stable-karma --unstable-karma"

-             options_update_type="--type"

-             options_update_request="--request"

-             ;;

-     esac

- 

-     local all_options="--help $options"

-     local all_options_value="$options_target $options_arches $options_branch \

-     $options_string $options_file $options_dir $options_srpm $options_mroot \

-     $options_builder $options_namespace $options_update_type $options_update_request"

- 

-     # count non-option parameters

- 

-     local i w

-     local last_option=

-     local after_counter=0

-     for (( i = $command_first; i < ${#COMP_WORDS[*]} - 1; i++)); do

-         w="${COMP_WORDS[$i]}"

-         if [[ ${w:0:1} = - ]]; then

-             if in_array "$w" "$all_options"; then

-                 last_option="$w"

-                 continue

-             elif in_array "$w" "$all_options_value"; then

-                 last_option="$w"

-                 ((i++))

-                 continue

-             fi

-         fi

-         in_array "$last_option" "$options_arches" || ((after_counter++))

-     done

- 

-     # completion

- 

-     if [[ -n $options_target ]] && in_array "$prev" "$options_target"; then

-         COMPREPLY=( $(compgen -W "$(_fedpkg_target)" -- "$cur") )

- 

-     elif [[ -n $options_arches ]] && in_array "$last_option" "$options_arches"; then

-         COMPREPLY=( $(compgen -W "$(_fedpkg_arch) $all_options" -- "$cur") )

- 

-     elif [[ -n $options_srpm ]] && in_array "$prev" "$options_srpm"; then

-         _filedir_exclude_paths "*.src.rpm"

- 

-     elif [[ -n $options_branch ]] && in_array "$prev" "$options_branch"; then

-         COMPREPLY=( $(compgen -W "$(_fedpkg_branch "$path")" -- "$cur") )

- 

-     elif [[ -n $options_file ]] && in_array "$prev" "$options_file"; then

-         _filedir_exclude_paths

- 

-     elif [[ -n $options_dir ]] && in_array "$prev" "$options_dir"; then

-         _filedir_exclude_paths -d

- 

-     elif [[ -n $options_string ]] && in_array "$prev" "$options_string"; then

-         COMPREPLY=( )

- 

-     elif [[ -n $options_mroot ]] && in_array "$prev" "$options_mroot"; then

-         COMPREPLY=( )

-         if declare -F _mock_root &>/dev/null; then

-             _mock_root

-         elif declare -F _xfunc &>/dev/null; then

-             _xfunc mock _mock_root

-         fi

- 

-     elif [[ -n $options_namespace ]] && in_array "$prev" "$options_namespace"; then

-         COMPREPLY=( $(compgen -W "$(_fedpkg_namespaces)" -- "$cur") )

- 

-     elif [[ -n $options_update_type ]] && in_array "$prev" "$options_update_type"; then

-         COMPREPLY=( $(compgen -W "bugfix security enhancement newpackage" -- "$cur") )

- 

-     elif [[ -n $options_update_request ]] && in_array "$prev" "$options_update_request"; then

-         COMPREPLY=( $(compgen -W "testing stable" -- "$cur") )

- 

-     else

-         local after_options=

- 

-         if [[ $after_counter -eq 0 ]] || [[ $after_more = true ]]; then

-             case $after in

-                 file)    _filedir_exclude_paths ;;

-                 srpm)    _filedir_exclude_paths "*.src.rpm" ;;

-                 branch)  after_options="$(_fedpkg_branch "$path")" ;;

-                 package) after_options="$(_fedpkg_package "$cur")";;

-             esac

-         fi

- 

-         if [[ $cur != -* ]]; then

-             all_options=

-             all_options_value=

-         fi

- 

-         COMPREPLY+=( $(compgen -W "$all_options $all_options_value $after_options" -- "$cur" ) )

-     fi

- 

-     return 0

- } &&

- complete -F _fedpkg fedpkg

- 

- _fedpkg_target()

- {

-     koji list-targets --quiet 2>/dev/null | cut -d" " -f1

- }

- 

- _fedpkg_arch()

- {

-     echo "i386 i686 x86_64 armv5tel armv7hl armv7hnl ppc ppc64 ppc64le ppc64p7 s390 s390x"

- }

- 

- _fedpkg_branch()

- {

-     local git_options= format="--format %(refname:short)"

-     [[ -n $1 ]] && git_options="--git-dir=$1/.git"

- 

-     git $git_options for-each-ref $format 'refs/remotes' | sed 's,.*/,,'

-     git $git_options for-each-ref $format 'refs/heads'

- }

- 

- _fedpkg_package()

- {

-     repoquery -C --qf=%{sourcerpm} "$1*" 2>/dev/null | sort -u | sed -r 's/(-[^-]*){2}\.src\.rpm$//'

- }

- 

- _fedpkg_namespaces()

- {

-     grep "^distgit_namespaces =" /etc/rpkg/fedpkg.conf | cut -d'=' -f2

- }

- 

- 

- # Local variables:

- # mode: shell-script

- # sh-basic-offset: 4

- # sh-indent-comment: t

- # indent-tabs-mode: nil

- # End:

- # ex: ts=4 sw=4 et filetype=sh

file modified
+2
@@ -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

file modified
+12
@@ -31,6 +31,7 @@ 

  from six.moves.urllib_parse import urlparse

  from pyrpkg import rpkgError

  from fedpkg.bugzilla import BugzillaClient

+ from fedpkg.completers import build_arches

  from fedpkg.utils import (

      get_release_branches, sl_list_to_dict, verify_sls, new_pagure_issue,

      get_pagure_token, is_epel, assert_valid_epel_package,
@@ -92,6 +93,7 @@ 

  class fedpkgClient(cliClient):

      def __init__(self, config, name=None):

          self.DEFAULT_CLI_NAME = 'fedpkg'

+         self.setup_completers()

          super(fedpkgClient, self).__init__(config, name)

          self.setup_fed_subparsers()

  
@@ -118,6 +120,16 @@ 

          self.register_request_branch()

          self.register_override()

  

+     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)

+         # TODO: add more completer assignments

+ 

      # Target registry goes here

      def register_retire(self):

          """Register the retire target"""

file added
+20
@@ -0,0 +1,20 @@ 

+ # -*- coding: utf-8 -*-

+ # completers.py - custom argument completers module for fedpkg

+ #

+ # Copyright (C) 2019 Red Hat Inc.

+ # Author(s): Ondrej Nosek <onosek@redhat.com>

+ #

+ # 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.

+ 

+ from argcomplete.completers import ChoicesCompleter

+ 

+ 

+ build_arches = ChoicesCompleter(("i386", "i686", "x86_64", "armv5tel",

+                                  "armv7hl", "armv7hnl", "ppc", "ppc64",

+                                  "ppc64le", "ppc64p7", "s390", "s390x"))

+ 

+ # TODO: add more completers

file modified
+1
@@ -1,3 +1,4 @@ 

+ argcomplete

  openidc-client

  python-bugzilla

  rpkg

file modified
+1 -8
@@ -11,12 +11,6 @@ 

      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')
@@ -48,8 +42,7 @@ 

      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']),

                  ],