| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
from textwrap import dedent |
| 4 |
|
| 5 |
import subprocess |
| 6 |
import re |
| 7 |
|
| 8 |
import collections |
| 9 |
|
| 10 |
|
| 11 |
class AutoVivification(dict): |
| 12 |
def __getitem__(self, item): |
| 13 |
try: |
| 14 |
return dict.__getitem__(self, item) |
| 15 |
except KeyError: |
| 16 |
value = self[item] = type(self)() |
| 17 |
return value |
| 18 |
|
| 19 |
|
| 20 |
class AppHash: |
| 21 |
def __init__(self): # noqa: C901 |
| 22 |
tasks = [ |
| 23 |
'kiwi/cli.py', |
| 24 |
'kiwi/tasks/*.py' |
| 25 |
] |
| 26 |
call_parm = ['bash', '-c', 'cat %s' % ' '.join(tasks)] |
| 27 |
cmd = subprocess.Popen( |
| 28 |
call_parm, stdout=subprocess.PIPE, stderr=subprocess.STDOUT |
| 29 |
) |
| 30 |
begin = False |
| 31 |
cur_path = '' |
| 32 |
self.result = AutoVivification() |
| 33 |
for line in cmd.communicate()[0].decode().split('\n'): |
| 34 |
usage = re.search('^usage: (.*)', line) |
| 35 |
if usage: |
| 36 |
begin = True |
| 37 |
if re.match('.*--help', line): |
| 38 |
mod_line = re.sub('[\[\]\|]', '', usage.group(1)) |
| 39 |
mod_line = re.sub('-h ', '', mod_line) |
| 40 |
key_list = mod_line.split() |
| 41 |
result_keys = self.validate(key_list) |
| 42 |
cur_path = self.merge(result_keys, self.result) |
| 43 |
else: |
| 44 |
key_list = usage.group(1).split() |
| 45 |
result_keys = self.validate(key_list) |
| 46 |
cur_path = self.merge(result_keys, self.result) |
| 47 |
elif begin: |
| 48 |
if not line: |
| 49 |
begin = False |
| 50 |
else: |
| 51 |
if re.match('.*--version', line): |
| 52 |
mod_line = re.sub('[\[\]\|]', '', line) |
| 53 |
mod_line = re.sub('-v ', '', mod_line) |
| 54 |
key_list = mod_line.split() |
| 55 |
result_keys = self.validate(key_list) |
| 56 |
cur_path = self.merge(result_keys, self.result) |
| 57 |
elif re.match('.*kiwi-ng --compat', line): |
| 58 |
mod_line = re.sub('[\[\]\|]', '', line) |
| 59 |
mod_line = re.sub('<legacy_args>...', '', mod_line) |
| 60 |
key_list = mod_line.split() |
| 61 |
result_keys = self.validate(key_list) |
| 62 |
cur_path = self.merge(result_keys, self.result) |
| 63 |
elif re.match('.*kiwi-ng \[', line): |
| 64 |
line = line.replace('[', '') |
| 65 |
line = line.replace(']', '') |
| 66 |
line = line.replace('|', '') |
| 67 |
key_list = line.split() |
| 68 |
key_list.pop(0) |
| 69 |
for global_opt in key_list: |
| 70 |
result_keys = self.validate( |
| 71 |
['kiwi-ng', global_opt] |
| 72 |
) |
| 73 |
cur_path = self.merge(result_keys, self.result) |
| 74 |
elif re.match(' \[', line): |
| 75 |
line = line.replace('[', '') |
| 76 |
line = line.replace(']', '') |
| 77 |
line = line.replace('|', '') |
| 78 |
key_list = line.split() |
| 79 |
for global_opt in key_list: |
| 80 |
result_keys = self.validate( |
| 81 |
['kiwi-ng', global_opt] |
| 82 |
) |
| 83 |
cur_path = self.merge(result_keys, self.result) |
| 84 |
elif re.match(' \[', line): |
| 85 |
opt_val = re.search(' \[(--.*)', line) |
| 86 |
global_opt = opt_val.group(1) |
| 87 |
global_opt = global_opt.replace('[', '') |
| 88 |
global_opt = global_opt.replace(']', '') |
| 89 |
global_opt = global_opt.replace('|', '') |
| 90 |
result_keys = self.validate( |
| 91 |
['kiwi-ng', global_opt] |
| 92 |
) |
| 93 |
cur_path = self.merge(result_keys, self.result) |
| 94 |
elif re.match('.*kiwi', line): |
| 95 |
mandatory_options = re.search( |
| 96 |
'(.*kiwi-ng.*?) (--.*)', line |
| 97 |
) |
| 98 |
if mandatory_options: |
| 99 |
line = mandatory_options.group(1) |
| 100 |
key_list = line.split() |
| 101 |
result_keys = self.validate(key_list) |
| 102 |
cur_path = self.merge(result_keys, self.result) |
| 103 |
if mandatory_options: |
| 104 |
for option in mandatory_options.group(2).split(): |
| 105 |
mod_line = cur_path + ' ' + option |
| 106 |
key_list = mod_line.split() |
| 107 |
result_keys = self.validate(key_list) |
| 108 |
self.merge(result_keys, self.result) |
| 109 |
else: |
| 110 |
if 'kiwi-ng --' in cur_path: |
| 111 |
cur_path = '' |
| 112 |
for mod_line in line.strip().split('|'): |
| 113 |
mod_line = cur_path + ' ' + mod_line |
| 114 |
mod_line = re.sub('[\[\]]', '', mod_line) |
| 115 |
mod_line = re.sub('-h ', '', mod_line) |
| 116 |
key_list = mod_line.split() |
| 117 |
result_keys = self.validate(key_list) |
| 118 |
self.merge(result_keys, self.result) |
| 119 |
|
| 120 |
def merge(self, key_list, result): |
| 121 |
raw_key_path = " ".join(key_list) |
| 122 |
key_path = '' |
| 123 |
for key in key_list: |
| 124 |
key_path += '[\'' + key + '\']' |
| 125 |
expression = 'self.result' + key_path |
| 126 |
exec(expression) |
| 127 |
return raw_key_path |
| 128 |
|
| 129 |
def validate(self, key_list): |
| 130 |
result_keys = [] |
| 131 |
for key in key_list: |
| 132 |
option = re.search('^\[(--.*)=|^\[(.*)\]', key) |
| 133 |
mandatory = re.search('^(--.*)=', key) |
| 134 |
if option: |
| 135 |
if option.group(1): |
| 136 |
result_keys.append(option.group(1)) |
| 137 |
elif mandatory: |
| 138 |
if mandatory.group(1): |
| 139 |
result_keys.append(mandatory.group(1)) |
| 140 |
elif re.search('<servicename>|<command>|<args>...', key): |
| 141 |
pass |
| 142 |
else: |
| 143 |
key = key.replace('<', '__') |
| 144 |
key = key.replace('>', '__') |
| 145 |
result_keys.append(key) |
| 146 |
return result_keys |
| 147 |
|
| 148 |
|
| 149 |
class AppTree: |
| 150 |
def __init__(self): |
| 151 |
self.completion = AppHash() |
| 152 |
self.level_dict = {} |
| 153 |
|
| 154 |
def traverse(self, tree=None, level=0, origin=None): |
| 155 |
if not tree: |
| 156 |
tree = self.completion.result['kiwi-ng'] |
| 157 |
if not origin: |
| 158 |
origin = 'kiwi-ng' |
| 159 |
for key in tree: |
| 160 |
try: |
| 161 |
if self.level_dict[str(level)]: |
| 162 |
pass |
| 163 |
except KeyError: |
| 164 |
self.level_dict[str(level)] = {} |
| 165 |
try: |
| 166 |
if self.level_dict[str(level)][origin]: |
| 167 |
pass |
| 168 |
except KeyError: |
| 169 |
self.level_dict[str(level)][origin] = [] |
| 170 |
|
| 171 |
if key not in self.level_dict[str(level)][origin]: |
| 172 |
self.level_dict[str(level)][origin].append(key) |
| 173 |
|
| 174 |
if tree[key]: |
| 175 |
self.traverse(tree[key], level + 1, key) |
| 176 |
|
| 177 |
|
| 178 |
tree = AppTree() |
| 179 |
tree.traverse() |
| 180 |
|
| 181 |
# helpful for debugging |
| 182 |
# pp = pprint.PrettyPrinter(indent=4) |
| 183 |
# pp.pprint(tree.completion.result) |
| 184 |
|
| 185 |
sorted_levels = collections.OrderedDict( |
| 186 |
sorted(tree.level_dict.items()) |
| 187 |
) |
| 188 |
|
| 189 |
print(dedent(''' |
| 190 |
#======================================== |
| 191 |
# _kiwi |
| 192 |
#---------------------------------------- |
| 193 |
function setupCompletionLine { |
| 194 |
local comp_line=$(echo $COMP_LINE | sed -e 's@kiwi-ng@kiwi@') |
| 195 |
local result_comp_line |
| 196 |
local prev_was_option=0 |
| 197 |
for item in $comp_line; do |
| 198 |
if [ $prev_was_option = 1 ];then |
| 199 |
prev_was_option=0 |
| 200 |
continue |
| 201 |
fi |
| 202 |
if [[ $item =~ -.* ]];then |
| 203 |
prev_was_option=1 |
| 204 |
continue |
| 205 |
fi |
| 206 |
result_comp_line="$result_comp_line $item" |
| 207 |
done |
| 208 |
echo $result_comp_line |
| 209 |
} |
| 210 |
|
| 211 |
function _kiwi { |
| 212 |
local cur prev opts |
| 213 |
_get_comp_words_by_ref cur prev |
| 214 |
local cmd=$(setupCompletionLine | awk -F ' ' '{ print $NF }') |
| 215 |
''').strip()) |
| 216 |
|
| 217 |
print(' for comp in $prev $cmd;do') |
| 218 |
print(' case "$comp" in') |
| 219 |
for level in sorted_levels: |
| 220 |
if level == '0': |
| 221 |
continue |
| 222 |
for sub in sorted(sorted_levels[level]): |
| 223 |
print(' "%s")' % (sub)) |
| 224 |
print( |
| 225 |
' __comp_reply "{0}"'.format( |
| 226 |
(" ".join(sorted(sorted_levels[level][sub]))) |
| 227 |
) |
| 228 |
) |
| 229 |
print(' return 0') |
| 230 |
print(' ;;') |
| 231 |
print(' esac') |
| 232 |
print(' done') |
| 233 |
print( |
| 234 |
' __comp_reply "{0}"'.format( |
| 235 |
(" ".join(sorted(sorted_levels['0']['kiwi-ng']))) |
| 236 |
) |
| 237 |
) |
| 238 |
print(' return 0') |
| 239 |
print('}') |
| 240 |
print(dedent(''' |
| 241 |
#======================================== |
| 242 |
# comp_reply |
| 243 |
#---------------------------------------- |
| 244 |
function __comp_reply { |
| 245 |
word_list=$@ |
| 246 |
COMPREPLY=($(compgen -W "$word_list" -- ${cur})) |
| 247 |
} |
| 248 |
|
| 249 |
complete -F _kiwi -o default kiwi |
| 250 |
complete -F _kiwi -o default kiwi-ng |
| 251 |
''').strip()) |