| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
import shlex |
| 7 |
import argparse |
| 8 |
|
| 9 |
# ============================================================================= |
| 10 |
# Constants |
| 11 |
# ============================================================================= |
| 12 |
|
| 13 |
PROGRAM_PATHS = ['/bin', '/sbin', '/usr/bin', '/usr/sbin'] |
| 14 |
|
| 15 |
BASH_COMPLETION_PATHS = [ |
| 16 |
'/etc/bash_completion.d', |
| 17 |
'/usr/share/bash-completion/completions', |
| 18 |
] |
| 19 |
|
| 20 |
FISH_COMPLETION_PATHS = [ |
| 21 |
'/usr/share/fish/completions', |
| 22 |
'/usr/share/fish/vendor_completions.d', |
| 23 |
] |
| 24 |
|
| 25 |
ZSH_COMPLETION_PATHS = [ |
| 26 |
'/usr/share/zsh/site-functions', |
| 27 |
'/usr/share/zsh/vendor-completions', |
| 28 |
'/usr/share/zsh/functions/Completion/Linux', |
| 29 |
'/usr/share/zsh/functions/Completion/Unix', |
| 30 |
'/usr/share/zsh/functions/Completion/Base', |
| 31 |
'/usr/share/zsh/functions/Completion/Zsh', |
| 32 |
'/usr/share/zsh/functions/Completion/X', |
| 33 |
] |
| 34 |
|
| 35 |
# ============================================================================= |
| 36 |
# Functions |
| 37 |
# ============================================================================= |
| 38 |
|
| 39 |
def find_programs(path): |
| 40 |
try: |
| 41 |
return os.listdir(path) |
| 42 |
except FileNotFoundError: |
| 43 |
pass |
| 44 |
|
| 45 |
def get_all_files_in_directories(directories): |
| 46 |
r = [] |
| 47 |
|
| 48 |
for directory in directories: |
| 49 |
try: |
| 50 |
for file in os.scandir(directory): |
| 51 |
r.append(file.path) |
| 52 |
except FileNotFoundError: |
| 53 |
continue |
| 54 |
|
| 55 |
return r |
| 56 |
|
| 57 |
def process_file(filename, callback): |
| 58 |
try: |
| 59 |
with open(filename, 'r', encoding='UTF-8') as fh: |
| 60 |
content = fh.read() |
| 61 |
return callback(filename, content) |
| 62 |
except FileNotFoundError: |
| 63 |
return None |
| 64 |
except UnicodeDecodeError: |
| 65 |
return None |
| 66 |
|
| 67 |
def process_files(files, callback): |
| 68 |
r = [] |
| 69 |
|
| 70 |
for file in files: |
| 71 |
with open(file, 'r', encoding='UTF-8') as fh: |
| 72 |
content = fh.read() |
| 73 |
result = callback(file, content) |
| 74 |
|
| 75 |
if result: |
| 76 |
r.append(result) |
| 77 |
|
| 78 |
return r |
| 79 |
|
| 80 |
def parse_bash_complete_command(line): |
| 81 |
try: |
| 82 |
split = shlex.split(line) |
| 83 |
except Exception: |
| 84 |
return [] |
| 85 |
|
| 86 |
i = 1 |
| 87 |
have_F = False |
| 88 |
programs = [] |
| 89 |
|
| 90 |
while i < len(split): |
| 91 |
arg = split[i] |
| 92 |
if arg == '-F': |
| 93 |
have_F = True |
| 94 |
i += 1 |
| 95 |
elif arg == '-o': |
| 96 |
i += 1 |
| 97 |
elif arg == '-X': |
| 98 |
i += 1 |
| 99 |
elif arg.startswith('-'): |
| 100 |
pass |
| 101 |
else: |
| 102 |
programs.append(arg) |
| 103 |
i += 1 |
| 104 |
|
| 105 |
if have_F: |
| 106 |
return programs |
| 107 |
else: |
| 108 |
return [] |
| 109 |
|
| 110 |
def parse_fish_complete_command(line): |
| 111 |
try: |
| 112 |
split = shlex.split(line) |
| 113 |
except Exception: |
| 114 |
return [] |
| 115 |
|
| 116 |
i = 1 |
| 117 |
while i < len(split): |
| 118 |
arg = split[i] |
| 119 |
if arg == '-c': |
| 120 |
return [split[i + 1]] |
| 121 |
elif arg.startswith('-c'): |
| 122 |
return [arg[2:]] |
| 123 |
i += 1 |
| 124 |
|
| 125 |
return [] |
| 126 |
|
| 127 |
def parse_zsh_compdef(line): |
| 128 |
split = line.split() |
| 129 |
return split[1:] |
| 130 |
|
| 131 |
def get_bash_completions(): |
| 132 |
def callback(filename, content): |
| 133 |
r = [] |
| 134 |
content = content.replace('\\\n', '') |
| 135 |
|
| 136 |
matches = re.findall('complete [^;&|\n]+', content) |
| 137 |
for m in matches: |
| 138 |
programs = parse_bash_complete_command(m) |
| 139 |
r.extend(programs) |
| 140 |
|
| 141 |
if not r: |
| 142 |
r = [os.path.basename(filename).lstrip('_')] |
| 143 |
|
| 144 |
return r |
| 145 |
|
| 146 |
files = get_all_files_in_directories(BASH_COMPLETION_PATHS) |
| 147 |
results = process_files(files, callback) |
| 148 |
|
| 149 |
r = set() |
| 150 |
for result in results: |
| 151 |
r.update(result) |
| 152 |
return sorted(r) |
| 153 |
|
| 154 |
def get_fish_completions(): |
| 155 |
def callback(filename, content): |
| 156 |
r = set() |
| 157 |
content = content.replace('\\\n', '') |
| 158 |
|
| 159 |
matches = re.findall('complete [^\n]+', content) |
| 160 |
for m in matches: |
| 161 |
programs = parse_fish_complete_command(m) |
| 162 |
r.update(programs) |
| 163 |
|
| 164 |
return r |
| 165 |
|
| 166 |
files = get_all_files_in_directories(FISH_COMPLETION_PATHS) |
| 167 |
results = process_files(files, callback) |
| 168 |
|
| 169 |
r = set() |
| 170 |
for result in results: |
| 171 |
r.update(result) |
| 172 |
return sorted(r) |
| 173 |
|
| 174 |
def get_zsh_completions(): |
| 175 |
def callback(filename, content): |
| 176 |
content = content.replace('\\\n', '') |
| 177 |
|
| 178 |
match = re.search('#compdef [^\n]+', content) |
| 179 |
if match: |
| 180 |
programs = parse_zsh_compdef(match[0]) |
| 181 |
return programs |
| 182 |
|
| 183 |
return [] |
| 184 |
|
| 185 |
files = get_all_files_in_directories(ZSH_COMPLETION_PATHS) |
| 186 |
results = process_files(files, callback) |
| 187 |
|
| 188 |
r = set() |
| 189 |
for result in results: |
| 190 |
r.update(result) |
| 191 |
return sorted(r) |
| 192 |
|
| 193 |
# ============================================================================= |
| 194 |
# Classes |
| 195 |
# ============================================================================= |
| 196 |
|
| 197 |
class Program: |
| 198 |
def __init__(self, program, has_bash, has_fish, has_zsh): |
| 199 |
self.program = program |
| 200 |
self.has_bash = has_bash |
| 201 |
self.has_fish = has_fish |
| 202 |
self.has_zsh = has_zsh |
| 203 |
|
| 204 |
def print(self): |
| 205 |
print('%-40s %-5s %-5s %-5s' % ( |
| 206 |
self.program, |
| 207 |
self.has_bash, |
| 208 |
self.has_fish, |
| 209 |
self.has_zsh)) |
| 210 |
|
| 211 |
class Filter: |
| 212 |
def __init__(self, s): |
| 213 |
self.filters = {} |
| 214 |
|
| 215 |
for shell_filter in s.split(','): |
| 216 |
try: |
| 217 |
shell, filter_ = shell_filter.split('=') |
| 218 |
except ValueError: |
| 219 |
raise Exception('Invalid format. Format has to be SHELL=BOOL') from None |
| 220 |
|
| 221 |
if shell not in ['bash', 'fish', 'zsh']: |
| 222 |
raise Exception(f'Invalid shell: {shell}') |
| 223 |
|
| 224 |
if filter_ not in ['True', 'False']: |
| 225 |
raise Exception(f'Invalid filter: {filter_}') |
| 226 |
|
| 227 |
if shell in self.filters: |
| 228 |
raise Exception(f'Double shell key: {shell}') |
| 229 |
|
| 230 |
self.filters[shell] = {'True': True, 'False': False}[filter_] |
| 231 |
|
| 232 |
def accept(self, program): |
| 233 |
for shell, filter_ in self.filters.items(): |
| 234 |
if shell == 'bash': |
| 235 |
if program.has_bash != filter_: |
| 236 |
return False |
| 237 |
elif shell == 'fish': |
| 238 |
if program.has_fish != filter_: |
| 239 |
return False |
| 240 |
elif shell == 'zsh': |
| 241 |
if program.has_zsh != filter_: |
| 242 |
return False |
| 243 |
|
| 244 |
return True |
| 245 |
|
| 246 |
# ============================================================================= |
| 247 |
# Command line arguments |
| 248 |
# ============================================================================= |
| 249 |
|
| 250 |
p = argparse.ArgumentParser() |
| 251 |
p.add_argument('-f', '--filter', metavar='SHELL=True|False', |
| 252 |
type=Filter, action='append', |
| 253 |
help='Filter results. Example: -f bash=True,zsh=False') |
| 254 |
opts = p.parse_args() |
| 255 |
|
| 256 |
# ============================================================================= |
| 257 |
# Collect data |
| 258 |
# ============================================================================= |
| 259 |
|
| 260 |
bash_completions = get_bash_completions() |
| 261 |
fish_completions = get_fish_completions() |
| 262 |
zsh_completions = get_zsh_completions() |
| 263 |
|
| 264 |
programs = set() |
| 265 |
for path in PROGRAM_PATHS: |
| 266 |
programs.update(find_programs(path)) |
| 267 |
programs = sorted(programs) |
| 268 |
|
| 269 |
# ============================================================================= |
| 270 |
# Filter and print results |
| 271 |
# ============================================================================= |
| 272 |
|
| 273 |
for program in programs: |
| 274 |
program = Program(program, |
| 275 |
(program in bash_completions), |
| 276 |
(program in fish_completions), |
| 277 |
(program in zsh_completions)) |
| 278 |
|
| 279 |
if opts.filter: |
| 280 |
for filter_ in opts.filter: |
| 281 |
if filter_.accept(program): |
| 282 |
program.print() |
| 283 |
break |
| 284 |
else: |
| 285 |
program.print() |