|
Packit |
8ea169 |
import sys
|
|
Packit |
8ea169 |
import problem
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
from abrtcli.l18n import _
|
|
Packit |
8ea169 |
from abrtcli.utils import get_human_identifier, sort_problems
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
def get_match_data(auth=False):
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
Return tuple of two dictionaries: one with components as keys
|
|
Packit |
8ea169 |
and one with short_ids as keys
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
Utility function used by match_ functions
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
by_human_id = {}
|
|
Packit |
8ea169 |
by_short_id = {}
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
for prob in problem.list(auth=auth):
|
|
Packit |
8ea169 |
comp_or_exe, val = get_human_identifier(prob)
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
if val in by_human_id:
|
|
Packit |
8ea169 |
by_human_id[val].append(prob)
|
|
Packit |
8ea169 |
else:
|
|
Packit |
8ea169 |
by_human_id[val] = [prob]
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
if prob.short_id in by_short_id:
|
|
Packit |
8ea169 |
by_short_id[prob.short_id].append(prob)
|
|
Packit |
8ea169 |
else:
|
|
Packit |
8ea169 |
by_short_id[prob.short_id] = [prob]
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
return by_human_id, by_short_id
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
def match_completer(prefix, parsed_args, **kwargs):
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
Completer generator used by cli commands using problem lookup
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
by_human_id, by_short_id = get_match_data()
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
for short_id in by_short_id.keys():
|
|
Packit |
8ea169 |
yield short_id
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
for human_id, probs in by_human_id.items():
|
|
Packit |
8ea169 |
if len(probs) == 1:
|
|
Packit |
8ea169 |
yield '{0}'.format(human_id)
|
|
Packit |
8ea169 |
else:
|
|
Packit |
8ea169 |
for prob in probs:
|
|
Packit |
8ea169 |
yield '{0}@{1}'.format(human_id, prob.short_id)
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
def match_lookup(in_arg, auth=False):
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
Return problems that match `in_arg` passed on command line
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
by_human_id, by_short_id = get_match_data(auth=auth)
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
res = None
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
if in_arg in by_human_id:
|
|
Packit |
8ea169 |
res = by_human_id[in_arg]
|
|
Packit |
8ea169 |
elif in_arg in by_short_id:
|
|
Packit |
8ea169 |
res = by_short_id[in_arg]
|
|
Packit |
8ea169 |
elif '@' in in_arg:
|
|
Packit |
8ea169 |
human_id, short_id = in_arg.split('@', 1)
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
if human_id in by_human_id:
|
|
Packit |
8ea169 |
probs = by_human_id[human_id]
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
res = list(filter(lambda p: p.short_id == short_id, probs))
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
return res
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
def match_get_problem(problem_match, allow_multiple=False, auth=False):
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
Return problem matching `problem_match` pattern
|
|
Packit |
8ea169 |
or exit if there are no such problems or pattern
|
|
Packit |
8ea169 |
results in multiple problems (unless `allow_multiple` is set
|
|
Packit |
8ea169 |
to True).
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
prob = None
|
|
Packit |
8ea169 |
if problem_match == 'last':
|
|
Packit |
8ea169 |
probs = sort_problems(problem.list(auth=auth))
|
|
Packit |
8ea169 |
if not probs:
|
|
Packit |
8ea169 |
print(_('No problems'))
|
|
Packit |
8ea169 |
sys.exit(0)
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
prob = probs[0]
|
|
Packit |
8ea169 |
else:
|
|
Packit |
8ea169 |
probs = match_lookup(problem_match, auth=auth)
|
|
Packit |
8ea169 |
if not probs:
|
|
Packit |
8ea169 |
print(_('No problem(s) matched'))
|
|
Packit |
8ea169 |
sys.exit(1)
|
|
Packit |
8ea169 |
elif len(probs) > 1:
|
|
Packit |
8ea169 |
if allow_multiple:
|
|
Packit |
8ea169 |
return probs
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
match_collision(probs)
|
|
Packit |
8ea169 |
sys.exit(1)
|
|
Packit |
8ea169 |
else:
|
|
Packit |
8ea169 |
prob = probs[0]
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
return prob
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
def match_collision(probs):
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
Handle matches that result in multiple problems by telling user
|
|
Packit |
8ea169 |
to be more specific
|
|
Packit |
8ea169 |
'''
|
|
Packit |
8ea169 |
|
|
Packit |
8ea169 |
print(_('Ambiguous match specified resulting in multiple problems:'))
|
|
Packit |
8ea169 |
for prob in probs:
|
|
Packit |
8ea169 |
field, val = get_human_identifier(prob)
|
|
Packit |
8ea169 |
print('- {}@{} ({})'.format(val, prob.short_id, prob.time))
|