aboutsummaryrefslogtreecommitdiff
blob: 80caf1d35a348530ce59d63c1b55c7e973c789e2 (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
# Copyright(c) 2009, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
#
# $Header: $

"""List files owned by a given package"""

__docformat__ = 'epytext'

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

import os
import sys
from getopt import gnu_getopt, GetoptError

import gentoolkit
import gentoolkit.pprinter as pp
from gentoolkit.equery import format_filetype, format_options, mod_usage, \
	Config
from gentoolkit.helpers2 import do_lookup

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

QUERY_OPTS = {
	"categoryFilter": None,
	"includeInstalled": True,
	"includePortTree": False,
	"includeOverlayTree": False,
	"includeMasked": True,
	"isRegex": False,
	"matchExact": True,
	"outputTree": False,
	"printMatchInfo": (not Config['quiet']),
	"showType": False,
	"showTimestamp": False,
	"showMD5": False,
	"typeFilter": None
}

FILTER_RULES = ('dir', 'obj', 'sym', 'dev', 'path', 'conf', 'cmd', 'doc',
	'man', 'info')

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

def print_help(with_description=True):
	"""Print description, usage and a detailed help message.
	
	@type with_description: bool
	@param with_description: if true, print module's __doc__ string
	"""

	if with_description:
		print __doc__.strip()
		print
	print mod_usage(mod_name="files")
	print
	print pp.command("options")
	print format_options((
		(" -h, --help", "display this help message"),
		(" -m, --md5sum", "include MD5 sum in output"),
		(" -s, --timestamp", "include timestamp in output"),
		(" -t, --type", "include file type in output"),
		("     --tree", "display results in a tree (turns off other options)"),
		(" -f, --filter=RULES", "filter output by file type"),
		("              RULES", 
			"a comma-separated list (no spaces); choose from:")
	))
	print " " * 24, ', '.join(pp.emph(x) for x in FILTER_RULES)


def display_files(contents):
	"""Display the content of an installed package.

	@see: gentoolkit.package.Package.get_contents
	@type contents: dict
	@param contents: {'path': ['filetype', ...], ...}
	"""

	filenames = contents.keys()
	filenames.sort()
	last = []

	for name in filenames:
		if QUERY_OPTS["outputTree"]:
			basename = name.split("/")[1:]
			if contents[name][0] == "dir":
				if len(last) == 0:
					last = basename
					print pp.path(" /" + basename[0])
					continue
				numol = 0
				for i, directory in enumerate(basename):
					try:
						if directory in last[i]:
							numol = i + 1
							continue
					# W0704: Except doesn't do anything
					# pylint: disable-msg=W0704
					except IndexError:
						pass
					last = basename
					if len(last) == 1:
						print pp.path(" " + last[0])
						continue
					ind = " " * (numol * 3)
					print pp.path(ind + "> " + "/" + last[-1])
			elif contents[name][0] == "sym":
				print pp.path(" " * (len(last) * 3) + "+"),
				print pp.path_symlink(basename[-1] + " -> " + contents[name][2])
			else: 
				print pp.path(" " * (len(last) * 3) + "+ ") + basename[-1]
		else:
			pp.print_info(0, format_filetype(
				name,
				contents[name],
				show_type=QUERY_OPTS["showType"],
				show_md5=QUERY_OPTS["showMD5"],
				show_timestamp=QUERY_OPTS["showTimestamp"]))


def filter_by_doc(contents, content_filter):
	"""Return a copy of content filtered by documentation."""

	filtered_content = {}
	for doctype in ('doc' ,'man' ,'info'):
		# List only files from /usr/share/{doc,man,info}
		if doctype in content_filter:
			docpath = os.path.join(os.sep, 'usr', 'share', doctype)
			for path in contents:
				if contents[path][0] == 'obj' and path.startswith(docpath):
					filtered_content[path] = contents[path]
	
	return filtered_content


def filter_by_command(contents):
	"""Return a copy of content filtered by executable commands."""

	filtered_content = {}
	userpath = os.environ["PATH"].split(os.pathsep)
	userpath = [os.path.normpath(x) for x in userpath]
	for path in contents:
		if (contents[path][0] in ['obj', 'sym'] and
			os.path.dirname(path) in userpath):
			filtered_content[path] = contents[path]
	
	return filtered_content


def filter_by_path(contents):
	"""Return a copy of content filtered by file paths."""

	filtered_content = {}
	paths = list(reversed(sorted(contents.keys())))
	while paths:
		basepath = paths.pop()
		if contents[basepath][0] == 'dir':
			check_subdirs = False
			for path in paths:
				if (contents[path][0] != "dir" and
					os.path.dirname(path) == basepath):
					filtered_content[basepath] = contents[basepath]
					check_subdirs = True
					break
			if check_subdirs:
				while (paths and paths[-1].startswith(basepath)):
					paths.pop()
	
	return filtered_content


def filter_by_conf(contents):
	"""Return a copy of content filtered by configuration files."""

	filtered_content = {}
	conf_path = gentoolkit.settings["CONFIG_PROTECT"].split()
	conf_path = tuple(os.path.normpath(x) for x in conf_path)
	conf_mask_path = gentoolkit.settings["CONFIG_PROTECT_MASK"].split()
	conf_mask_path = tuple(os.path.normpath(x) for x in conf_mask_path)
	for path in contents:
		if contents[path][0] == 'obj' and path.startswith(conf_path):
			if not path.startswith(conf_mask_path):
				filtered_content[path] = contents[path]

	return filtered_content


def filter_contents(contents):
	"""Filter files by type if specified by the user.

	@see: gentoolkit.package.Package.get_contents
	@type contents: dict
	@param contents: {'path': ['filetype', ...], ...}
	@rtype: dict
	@return: contents with unrequested filetypes stripped
	"""

	if QUERY_OPTS['typeFilter']:
		content_filter = QUERY_OPTS['typeFilter']
	else:
		return contents
	
	filtered_content = {}
	if frozenset(('dir', 'obj', 'sym', 'dev')).intersection(content_filter):
		# Filter elements by type (as recorded in CONTENTS)
		for path in contents:
			if contents[path][0] in content_filter:
				filtered_content[path] = contents[path]
	if "cmd" in content_filter:
		filtered_content.update(filter_by_command(contents))
	if "path" in content_filter:
		filtered_content.update(filter_by_path(contents))
	if "conf" in content_filter:
		filtered_content.update(filter_by_conf(contents))
	if frozenset(('doc' ,'man' ,'info')).intersection(content_filter):
		filtered_content.update(filter_by_doc(contents, content_filter))
	
	return filtered_content


def parse_module_options(module_opts):
	"""Parse module options and update GLOBAL_OPTS"""

	content_filter = []
	opts = (x[0] for x in module_opts)
	posargs = (x[1] for x in module_opts)
	for opt, posarg in zip(opts, posargs):
		if opt in ('-h', '--help'):
			print_help()
			sys.exit(0)
		elif opt in ('-e', '--exact-name'):
			QUERY_OPTS["matchExact"] = True
		elif opt in ('-m', '--md5sum'):
			QUERY_OPTS["showMD5"] = True
		elif opt in ('-s', '--timestamp'):
			QUERY_OPTS["showTimestamp"] = True
		elif opt in ('-t', '--type'):
			QUERY_OPTS["showType"] = True
		elif opt in ('--tree'):
			QUERY_OPTS["outputTree"] = True
		elif opt in ('-f', '--filter'):
			f_split = posarg.split(',')
			content_filter.extend(x.lstrip('=') for x in f_split)
			for rule in content_filter:
				if not rule in FILTER_RULES:
					pp.print_error("Invalid filter rule '%s'" % rule)
					print
					print_help(with_description=False)
					sys.exit(2)
			QUERY_OPTS["typeFilter"] = content_filter


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

	short_opts = "hemstf:"
	long_opts = ('help', 'exact-name', 'md5sum', 'timestamp', 'type', 'tree',
		'filter=') 

	try:
		module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
	except GetoptError, err:
		pp.print_error("Module %s" % err)
		print
		print_help(with_description=False)
		sys.exit(2)

	parse_module_options(module_opts)
	
	if not queries:
		print_help()
		sys.exit(2)

	# Turn off filtering for tree output
	if QUERY_OPTS["outputTree"]:
		QUERY_OPTS["typeFilter"] = None

	#
	# Output files
	#

	first_run = True
	for query in queries:
		if not first_run:
			print

		matches = do_lookup(query, QUERY_OPTS)

		if not matches:
			pp.print_error("No matching packages found for %s" % query)

		for pkg in matches:
			if Config['verbose']:
				print " * Contents of %s:" % pp.cpv(pkg.cpv)

			contents = pkg.get_contents()
			display_files(filter_contents(contents))

		first_run = False