Skip to content

Commit

Permalink
Client will install bash completion file.
Browse files Browse the repository at this point in the history
JIRA: PDC-1161
  • Loading branch information
zkl94 committed Nov 16, 2015
1 parent 05280ef commit c013040
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
6 changes: 6 additions & 0 deletions docs/source/pdc_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ requests manually.
This is much more user friendly user interface. A single invocation can perform
multiple requests depending on what subcommand you used.

Although auto-completion is inherently enabled for pdc, it will be disabled if
`python-argcomplete` is not installed. To enable it, install `python-argcomplete`
and activate by typing `activate-global-python-argcomplete` in command line.
Please note that by doing this only global completion is enabled which is
currently not compatible with zsh.


Python API
----------
Expand Down
1 change: 1 addition & 0 deletions pdc_client/bin/pdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#! /usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
Expand Down
4 changes: 4 additions & 0 deletions pdc_client/pdc-client.spec
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ install -m 0644 -D -p bin/pdc %{buildroot}%{_bindir}/pdc
install -m 0644 -D -p docs/pdc_client.1 %{buildroot}%{_mandir}/man1/pdc_client.1
gzip %{buildroot}%{_mandir}/man1/pdc_client.1

# Install bash completion file
install -m 0644 -D -p python-argcomplete.sh %{buildroot}%{_sysconfdir}/bash_completion.d/python-argcomplete.sh


%files
%{python_sitelib}/pdc_client
%attr(755, root, root) %{_bindir}/pdc_client
%attr(755, root, root) %{_bindir}/pdc
%{_mandir}/man1/pdc_client.1.gz
%{_sysconfdir}/bash_completion.d/python-argcomplete.sh


%changelog
Expand Down
39 changes: 39 additions & 0 deletions pdc_client/python-argcomplete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2012-2013, Andrey Kislyuk and argcomplete contributors.
# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.

_python_argcomplete_global() {
local ARGCOMPLETE=0
if [[ "$1" == python* ]] || [[ "$1" == pypy* ]]; then
if [[ -f "${COMP_WORDS[1]}" ]] && (head -c 1024 "${COMP_WORDS[1]}" | grep --quiet "PYTHON_ARGCOMPLETE_OK") >/dev/null 2>&1; then
local ARGCOMPLETE=2
set -- "${COMP_WORDS[1]}"
fi
elif which "$1" >/dev/null 2>&1; then
local SCRIPT_NAME=$(which "$1")
if (type -t pyenv && [[ "$SCRIPT_NAME" = $(pyenv root)/shims/* ]]) >/dev/null 2>&1; then
local SCRIPT_NAME=$(pyenv which "$1")
fi
if (head -c 1024 "$SCRIPT_NAME" | grep --quiet "PYTHON_ARGCOMPLETE_OK") >/dev/null 2>&1; then
local ARGCOMPLETE=1
elif (head -c 1024 "$SCRIPT_NAME" | egrep --quiet "(PBR Generated)|(EASY-INSTALL-(SCRIPT|ENTRY-SCRIPT|DEV-SCRIPT))" \
&& python-argcomplete-check-easy-install-script "$SCRIPT_NAME") >/dev/null 2>&1; then
local ARGCOMPLETE=1
fi
fi

if [[ $ARGCOMPLETE == 1 ]] || [[ $ARGCOMPLETE == 2 ]]; then
local IFS=$(echo -e '\v')
COMPREPLY=( $(_ARGCOMPLETE_IFS="$IFS" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
_ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" \
_ARGCOMPLETE=$ARGCOMPLETE \
"$1" 8>&1 9>&2 1>/dev/null 2>&1) )
if [[ $? != 0 ]]; then
unset COMPREPLY
fi
else
type -t _completion_loader | grep -q 'function' && _completion_loader "$@"
fi
}
complete -o nospace -o default -o bashdefault -D -F _python_argcomplete_global
9 changes: 1 addition & 8 deletions pdc_client/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
import logging
import imp

# The client supports Bash completion if argcomplete Python package is
# installed. To enable it, run this in your terminal (assuming pdc is somewhere
# on path).
#
# eval "$(register-python-argcomplete pdc)"
#
# This is only a temporary solution, when the client is packaged, a completion
# file should be shipped with it and installed to /etc/bash_completion.d/.
# The client supports Bash completion if argcomplete Python package is installed.
try:
import argcomplete
except ImportError:
Expand Down

0 comments on commit c013040

Please sign in to comment.