1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
'''
This module provides functions to retrieve file paths for
autocompletion scripts for Bash, Fish, and Zsh.
'''
import subprocess
from . import utils
# pylint: disable=broad-exception-caught
# pylint: disable=broad-exception-raised
def _pkg_config(args):
'''
Call the `pkg-config` program with `args` and check the return code.
If everything is fine, return the output as string.
'''
command = ['pkg-config'] + args
try:
result = subprocess.run(
command,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
check=False)
except FileNotFoundError as exc:
raise Exception('Program `pkg-config` not found') from exc
if result.returncode == 0:
return result.stdout.strip()
raise Exception('Command `%s` failed: %s' % (' '.join(command), result.stderr.strip()))
def get_bash_completion_file(program_name):
'''Get the path for a Bash completion file.'''
directory = '/usr/share/bash-completion/completions'
try:
directory = _pkg_config(['--variable=completionsdir', 'bash-completion'])
except Exception as e:
utils.warn(e)
return f'{directory}/{program_name}'
def get_fish_completion_file(program_name):
'''Get the path for a Fish completion file.'''
directory = '/usr/share/fish/vendor_completions.d'
try:
directory = _pkg_config(['--variable=completionsdir', 'fish'])
except Exception as e:
utils.warn(e)
return f'{directory}/{program_name}.fish'
def get_zsh_completion_file(program_name):
'''Get the path for a Zsh completion file.'''
# There is '/usr/share/zsh/vendor-completions', but that directory is not
# in the default $fpath of zsh.
directory = '/usr/share/zsh/site-functions'
return f'{directory}/_{program_name}'
|