0
0

More than 1 year has passed since last update.

python-argcomplete python3で使うときに引っかかった話(Ubuntu 22.04)

Posted at

python-argcomplete が使えない

久しぶりにansibleを使ってみようかなと思いまして、公式サイトのインストール手順を見直していたのですが。
最初の方で推奨されているコマンド補完用のpython-argcompleteがどうにも動きません。
で、ちょっとコード読んで問題箇所見っけたのでメモります。

環境

Ubuntu 22.04。
以下を導入済み。

$ sudo apt install python3-argcomplete
$ sudo activate-global-python-argcomplete3

Ansibleのサイトでは"3"をつけない手順が紹介されています。
https://docs.ansible.com/ansible/2.9/installation_guide/intro_installation.html
(ちなみにこの手順で落とすと、今だと2.13.5が落ちてくるので、2.9用手順となっっているのがちょっと気持ち悪くはありますが)

現状ubuntu 22.04では無印は配布していないようですので、"3"付きにしないといけません。

で、、補完が効きません。

原因

activate-global-python-argcomplete3を実行すると以下が生成されるのですが、その中で呼び出されているpython-argcomplete-check-easy-install-scriptが無印なのが原因です。

$ cat /etc/bash_completion.d/python-argcomplete.sh # Copyright 2012-2013, Andrey Kislyuk and argcomplete contributors.
# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.

# Copy of __expand_tilde_by_ref from bash-completion
__python_argcomplete_expand_tilde_by_ref () {
    if [ "${!1:0:1}" = "~" ]; then
        if [ "${!1}" != "${!1//\/}" ]; then
            eval $1="${!1/%\/*}"/'${!1#*/}';
        else
            eval $1="${!1}";
        fi;
    fi
}

_python_argcomplete_global() {
    local executable=$1
    __python_argcomplete_expand_tilde_by_ref executable

    local ARGCOMPLETE=0
    if [[ "$executable" == python* ]] || [[ "$executable" == 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
        else
            return
        fi
    elif which "$executable" >/dev/null 2>&1; then
        local SCRIPT_NAME=$(which "$executable")
        if (type -t pyenv && [[ "$SCRIPT_NAME" = $(pyenv root)/shims/* ]]) >/dev/null 2>&1; then
            local SCRIPT_NAME=$(pyenv which "$executable")
        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" \
            COMP_TYPE="$COMP_TYPE" \
            _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" \
            _ARGCOMPLETE=$ARGCOMPLETE \
            _ARGCOMPLETE_SUPPRESS_SPACE=1 \
            "$executable" "${COMP_WORDS[@]:1:ARGCOMPLETE-1}" 8>&1 9>&2 1>/dev/null 2>&1) )
        if [[ $? != 0 ]]; then
            unset COMPREPLY
        elif [[ "$COMPREPLY" =~ [=/:]$ ]]; then
            compopt -o nospace
        fi
    else
        type -t _completion_loader | grep -q 'function' && _completion_loader "$@"
    fi
}

python-argcomplete-check-easy-install-scriptをpython-argcomplete-check-easy-install-script3に書き換えれば補完が動くようになります。

最初以下の方法でしのいでましたが、これするとコマンドごとに追加しないといけないのでめんどくさいです。

$ sudo register-python-argcomplete3 ansible >> /etc/bash_completion.d/python-argcomplete.sh

まとめ

そんだけ。
そのうち修正されるでしょうけれど、python3化の影響ってまだ残っってるのだなぁ、、と再認識した今日この頃でした。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0