-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathactivate_pyzshcomplete
More file actions
96 lines (73 loc) · 3.29 KB
/
activate_pyzshcomplete
File metadata and controls
96 lines (73 loc) · 3.29 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python3
import os
from os.path import join, dirname, expanduser
from os import remove, geteuid, walk
from subprocess import run, Popen, PIPE
from sys import stderr, exit
from glob import glob
def main():
try:
avoid_running_as_super_user_implicitly()
completion_script_dir = find_completion_script_dir()
copy_completion_scripts(completion_script_dir)
remove_completion_system_cache_files()
print_success_message()
except Exception as e:
stderr.write('An error occured during installation: {}\n'.
format(str(e)))
exit(-1)
def avoid_running_as_super_user_implicitly():
if geteuid() == 0:
if input('You seem to be running as a super user. If you have a user '
'account for which you would like to activate pyzshcomplete, '
'you should run this script from that user. Root priviledges '
'will be asked for when needed. \nContinue? [y/N] ').lower() \
!= 'y':
print('Aborting...')
exit(0)
print('Continuing...')
def find_completion_script_dir():
print('Locating zsh completion script directory...')
result = Popen(['echo $FPATH'], shell=True, executable='zsh', stdout=PIPE)
fpath = result.stdout.read().decode('ascii').strip()
for path in fpath.split(':'):
for root, _, files in walk(path):
if '_python' in files:
print(f'Installing in {root}')
return root
raise RuntimeError('Can\'t find zsh completion folder. If you see this '
'error, please report it on the pyzshcomplete issue '
'tracker.\n')
def copy_completion_scripts(target_dir):
print('Copying completion scripts... ')
scripts = get_completion_scripts()
perform_copy(scripts, target_dir, not os.access(target_dir, os.W_OK))
def remove_completion_system_cache_files():
print('Removing completion system cache files... ')
zsh_completion_cache_files = glob(expanduser('~/.zcompdump*'))
for cache_file in zsh_completion_cache_files:
try:
remove(cache_file)
except:
stderr.write('WARNING: Failed removing zsh cache file: {}. You can '
'try to remove it manually.'.format(cache_file))
def print_success_message():
print('pyzshcomplete was activated successfully!')
print('Restart zsh for changes to take effect')
def get_completion_scripts():
try:
import pyzshcomplete
except ImportError as e:
raise ImportError('{}: Make sure the module is installed for the user '
'running the activation script'.format(str(e)))
pyzshcomplete_root_dir = dirname(pyzshcomplete.__file__)
zsh_scripts_dir = join(pyzshcomplete_root_dir, 'zsh_scripts')
return ' '.join(glob(join(zsh_scripts_dir, '*')))
def perform_copy(source, target, as_root = False):
commandline = f'{"sudo " if as_root else "" }cp {source} {target}'
if run(commandline.split()).returncode != 0:
raise RuntimeError('Failed copying zsh completion scripts. Make sure '
'to provide root access, as these files are copied '
'to a global zsh dir owned by root\n')
if __name__ == '__main__':
main()