1 # Copyright 2010-2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
8 from portage import _encodings
9 from portage import _unicode_decode
10 from portage.dep import Atom
11 from portage.elog import messages as elog_messages
12 from portage.exception import InvalidAtom
13 from portage.package.ebuild._ipc.IpcCommand import IpcCommand
14 from portage.util import normalize_path
15 from portage.versions import best
17 class QueryCommand(IpcCommand):
19 __slots__ = ('phase', 'settings',)
23 def __init__(self, settings, phase):
24 IpcCommand.__init__(self)
25 self.settings = settings
28 def __call__(self, argv):
30 @returns: tuple of (stdout, stderr, returncode)
33 cmd, root, atom_str = argv
38 return ('', 'invalid atom: %s\n' % atom_str, 2)
42 atom = Atom(atom_str, eapi=self.settings.get('EAPI'))
43 except InvalidAtom as e:
44 warnings.append(_unicode_decode("QA Notice: %s: %s") % (cmd, e))
46 use = self.settings.get('PORTAGE_BUILT_USE')
48 use = self.settings['PORTAGE_USE']
50 use = frozenset(use.split())
51 atom = atom.evaluate_conditionals(use)
59 warnings_str = self._elog('eqawarn', warnings)
61 root = normalize_path(root).rstrip(os.path.sep) + os.path.sep
63 return ('', 'invalid ROOT: %s\n' % root, 2)
65 vardb = db[root]["vartree"].dbapi
67 if cmd == 'has_version':
72 return ('', warnings_str, returncode)
73 elif cmd == 'best_version':
74 m = best(vardb.match(atom))
75 return ('%s\n' % m, warnings_str, 0)
77 return ('', 'invalid command: %s\n' % cmd, 2)
79 def _elog(self, elog_funcname, lines):
81 This returns a string, to be returned via ipc and displayed at the
82 appropriate place in the build output. We wouldn't want to open the
83 log here since it is already opened by AbstractEbuildProcess and we
84 don't want to corrupt it, especially if it is being written with
89 elog_func = getattr(elog_messages, elog_funcname)
90 global_havecolor = portage.output.havecolor
92 portage.output.havecolor = \
93 self.settings.get('NOCOLOR', 'false').lower() in ('no', 'false')
95 elog_func(line, phase=phase, key=self.settings.mycpv, out=out)
97 portage.output.havecolor = global_havecolor
98 msg = _unicode_decode(out.getvalue(),
99 encoding=_encodings['content'], errors='replace')