aboutsummaryrefslogtreecommitdiff
blob: a73ef1ba5e07aa45504a28f465d02342ab37bcdf (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright(c) 2009, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
#
# $Header: $

"""Gentoo package query tool"""

from __future__ import print_function

__all__ = (
	'format_options',
	'format_package_names',
	'mod_usage'
)
__docformat__ = 'epytext'
# version is dynamically set by distutils sdist
__version__ = "svn"

# =======
# Imports
# =======

import errno
import os
import sys
import time
from getopt import getopt, GetoptError

import portage

import gentoolkit
from gentoolkit import CONFIG
from gentoolkit import errors
from gentoolkit import pprinter as pp
from gentoolkit.textwrap_ import TextWrapper

__productname__ = "equery"
__authors__ = (
	'Karl Trygve Kalleberg - Original author',
	'Douglas Anderson - 0.3.0 author'
)

# =======
# Globals
# =======

NAME_MAP = {
	'b': 'belongs',
	'c': 'changes',
	'k': 'check',
	'd': 'depends',
	'g': 'depgraph',
	'f': 'files',
	'h': 'hasuse',
	'l': 'list_',
	'y': 'keywords',
	'a': 'has',
	'm': 'meta',
	's': 'size',
	'u': 'uses',
	'w': 'which'
}

# =========
# Functions
# =========

def print_help(with_description=True):
	"""Print description, usage and a detailed help message.

	@param with_description (bool): Option to print module's __doc__ or not
	"""

	if with_description:
		print(__doc__)
	print(main_usage())
	print()
	print(pp.globaloption("global options"))
	print(format_options((
		(" -h, --help", "display this help message"),
		(" -q, --quiet", "minimal output"),
		(" -C, --no-color", "turn off colors"),
		(" -N, --no-pipe", "turn off pipe detection"),
		(" -V, --version", "display version info")
	)))
	print()
	print(pp.command("modules") + " (" + pp.command("short name") + ")")
	print(format_options((
		(" (b)elongs", "list what package FILES belong to"),
		(" (c)hanges", "list changelog entries for ATOM"),
		(" chec(k)", "verify checksums and timestamps for PKG"),
		(" (d)epends", "list all packages directly depending on ATOM"),
		(" dep(g)raph", "display a tree of all dependencies for PKG"),
		(" (f)iles", "list all files installed by PKG"),
		(" h(a)s", "list all packages for matching ENVIRONMENT data stored in /var/db/pkg"),
		(" (h)asuse", "list all packages that have USE flag"),
		(" ke(y)words", "display keywords for specified PKG"),
		(" (l)ist", "list package matching PKG"),
		(" (m)eta", "display metadata about PKG"),
		(" (s)ize", "display total size of all files owned by PKG"),
		(" (u)ses", "display USE flags for PKG"),
		(" (w)hich", "print full path to ebuild for PKG")
	)))


def expand_module_name(module_name):
	"""Returns one of the values of NAME_MAP or raises KeyError"""

	if module_name == 'list':
		# list is a Python builtin type, so we must rename our module
		return 'list_'
	elif module_name in NAME_MAP.values():
		return module_name
	else:
		return NAME_MAP[module_name]


def format_options(options):
	"""Format module options.

	@type options: list
	@param options: [('option 1', 'description 1'), ('option 2', 'des... )]
	@rtype: str
	@return: formatted options string
	"""

	result = []
	twrap = TextWrapper(width=CONFIG['termWidth'])
	opts = (x[0] for x in options)
	descs = (x[1] for x in options)
	for opt, desc in zip(opts, descs):
		twrap.initial_indent = pp.emph(opt.ljust(25))
		twrap.subsequent_indent = " " * 25
		result.append(twrap.fill(desc))

	return '\n'.join(result)


def format_filetype(path, fdesc, show_type=False, show_md5=False,
		show_timestamp=False):
	"""Format a path for printing.

	@type path: str
	@param path: the path
	@type fdesc: list
	@param fdesc: [file_type, timestamp, MD5 sum/symlink target]
		file_type is one of dev, dir, obj, sym.
		If file_type is dir, there is no timestamp or MD5 sum.
		If file_type is sym, fdesc[2] is the target of the symlink.
	@type show_type: bool
	@param show_type: if True, prepend the file's type to the formatted string
	@type show_md5: bool
	@param show_md5: if True, append MD5 sum to the formatted string
	@type show_timestamp: bool
	@param show_timestamp: if True, append time-of-creation after pathname
	@rtype: str
	@return: formatted pathname with optional added information
	"""

	ftype = fpath = stamp = md5sum = ""

	if fdesc[0] == "obj":
		ftype = "file"
		fpath = path
		stamp = format_timestamp(fdesc[1])
		md5sum = fdesc[2]
	elif fdesc[0] == "dir":
		ftype = "dir"
		fpath = pp.path(path)
	elif fdesc[0] == "sym":
		ftype = "sym"
		stamp = format_timestamp(fdesc[1])
		tgt = fdesc[2].split()[0]
		if CONFIG["piping"]:
			fpath = path
		else:
			fpath = pp.path_symlink(path + " -> " + tgt)
	elif fdesc[0] == "dev":
		ftype = "dev"
		fpath = path
	else:
		sys.stderr.write(
			pp.error("%s has unknown type: %s" % (path, fdesc[0]))
		)

	result = ""
	if show_type:
		result += "%4s " % ftype
	result += fpath
	if show_timestamp:
		result += "  " + stamp
	if show_md5:
		result += "  " + md5sum

	return result


def format_timestamp(timestamp):
	"""Format a timestamp into, e.g., '2009-01-31 21:19:44' format"""

	return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(timestamp)))


def initialize_configuration():
	"""Setup the standard equery config"""

	# Get terminal size
	term_width = pp.output.get_term_size()[1]
	if term_width < 1:
		# get_term_size() failed. Set a sane default width:
		term_width = 80

	# Terminal size, minus a 1-char margin for text wrapping
	CONFIG['termWidth'] = term_width - 1

	# Guess color output
	if (CONFIG['color'] == -1 and (not sys.stdout.isatty() or
		os.getenv("NOCOLOR") in ("yes", "true")) or CONFIG['color'] == 0):
		pp.output.nocolor()

	if CONFIG['piping']:
		CONFIG['verbose'] = False
		# set extra wide, should disable wrapping unless 
		# there is some extra long text 
		CONFIG['termWidth'] = 600

	CONFIG['debug'] = bool(os.getenv('DEBUG', False))


def main_usage():
	"""Return the main usage message for equery"""

	return "%(usage)s %(product)s [%(g_opts)s] %(mod_name)s [%(mod_opts)s]" % {
		'usage': pp.emph("Usage:"),
		'product': pp.productname(__productname__),
		'g_opts': pp.globaloption("global-options"),
		'mod_name': pp.command("module-name"),
		'mod_opts': pp.localoption("module-options")
	}


def mod_usage(mod_name="module", arg="pkgspec", optional=False):
	"""Provide a consistent usage message to the calling module.

	@type arg: string
	@param arg: what kind of argument the module takes (pkgspec, filename, etc)
	@type optional: bool
	@param optional: is the argument optional?
	"""

	return "%(usage)s: %(mod_name)s [%(opts)s] %(arg)s" % {
		'usage': pp.emph("Usage"),
		'mod_name': pp.command(mod_name),
		'opts': pp.localoption("options"),
		'arg': ("[%s]" % pp.emph(arg)) if optional else pp.emph(arg)
	}


def parse_global_options(global_opts, args):
	"""Parse global input args and return True if we should display help for
	the called module, else False (or display help and exit from here).
	"""

	need_help = False
	do_help = False
	opts = (opt[0] for opt in global_opts)
	for opt in opts:
		if opt in ('-h', '--help'):
			if args:
				need_help = True
			else:
				do_help = True
		elif opt in ('-q','--quiet'):
			CONFIG['quiet'] = True
		elif opt in ('-C', '--no-color', '--nocolor'):
			CONFIG['color'] = 0
			pp.output.nocolor()
		elif opt in ('-N', '--no-pipe'):
			CONFIG['piping'] = False
		elif opt in ('-V', '--version'):
			print_version()
			sys.exit(0)
		elif opt in ('--debug'):
			CONFIG['debug'] = True
	if do_help:
		print_help()
		sys.exit(0)
	return need_help


def print_version():
	"""Print the version of this tool to the console."""

	print("%(product)s (%(version)s) - %(docstring)s" % {
		"product": pp.productname(__productname__),
		"version": __version__,
		"docstring": __doc__
	})


def split_arguments(args):
	"""Separate module name from module arguments"""

	return args.pop(0), args


def main(argv):
	"""Parse input and run the program."""

	short_opts = "hqCNV"
	long_opts = (
		'help', 'quiet', 'nocolor', 'no-color', 'no-pipe', 'version', 'debug'
	)

	initialize_configuration()

	try:
		global_opts, args = getopt(argv[1:], short_opts, long_opts)
	except GetoptError as err:
		sys.stderr.write(pp.error("Global %s" % err))
		print_help(with_description=False)
		sys.exit(2)

	# Parse global options
	need_help = parse_global_options(global_opts, args)

	# verbose is shorthand for the very common 'not quiet or piping'
	if CONFIG['quiet'] or CONFIG['piping']:
		CONFIG['verbose'] = False
	else:
		CONFIG['verbose'] = True

	try:
		module_name, module_args = split_arguments(args)
	except IndexError:
		print_help()
		sys.exit(2)

	if need_help:
		module_args.append('--help')

	try:
		expanded_module_name = expand_module_name(module_name)
	except KeyError:
		sys.stderr.write(pp.error("Unknown module '%s'" % module_name))
		print_help(with_description=False)
		sys.exit(2)

	try:
		loaded_module = __import__(
			expanded_module_name, globals(), locals(), [], -1
		)
		loaded_module.main(module_args)
	except portage.exception.AmbiguousPackageName as err:
		raise errors.GentoolkitAmbiguousPackage(err.args[0])
	except IOError as err:
		if err.errno != errno.EPIPE:
			raise

if __name__ == '__main__':
	main(sys.argv)