aboutsummaryrefslogtreecommitdiff
blob: 30ffa93f5b5b42317431a584b80599af3a3152d4 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/python
#
# Copyright 2010 Brian Dolbec <brian.dolbec@gmail.com>
# Copyright(c) 2010, Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#

"""Provides a breakdown list of USE flags or keywords used and by
what packages according to the Installed package database"""

import gentoolkit
from gentoolkit.module_base import ModuleBase
from gentoolkit import pprinter as pp
from gentoolkit.flag import get_installed_use, get_flags
from gentoolkit.enalyze.lib import FlagAnalyzer, KeywordAnalyser
from gentoolkit.enalyze.output import nl, AnalysisPrinter
from gentoolkit.package import Package
from gentoolkit.helpers import get_installed_cpvs

import portage


def gather_flags_info(
		cpvs=None,
		system_flags=None,
		include_unset=False,
		target="USE",
		use_portage=False,
		#  override-able for testing
		_get_flags=get_flags,
		_get_used=get_installed_use
		):
	"""Analyze the installed pkgs USE flags for frequency of use

	@type cpvs: list
	@param cpvs: optional list of [cat/pkg-ver,...] to analyze or
			defaults to entire installed pkg db
	@type: system_flags: list
	@param system_flags: the current default USE flags as defined
			by portage.settings["USE"].split()
	@type include_unset: bool
	@param include_unset: controls the inclusion of unset USE flags in the report.
	@type target: string
	@param target: the environment variable being analyzed
			one of ["USE", "PKGUSE"]
	@type _get_flags: function
	@param _get_flags: ovride-able for testing,
			defaults to gentoolkit.enalyze.lib.get_flags
	@param _get_used: ovride-able for testing,
			defaults to gentoolkit.enalyze.lib.get_installed_use
	@rtype dict. {flag:{"+":[cat/pkg-ver,...], "-":[cat/pkg-ver,...], "unset":[]}
	"""
	if cpvs is None:
		cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
	# pass them in to override for tests
	flags = FlagAnalyzer(system_flags,
		filter_defaults=False,
		target=target,
		_get_flags=_get_flags,
		_get_used=get_installed_use
	)
	flag_users = {}
	for cpv in cpvs:
		if cpv.startswith("virtual"):
			continue
		if use_portage:
			plus, minus, unset = flags.analyse_cpv(cpv)
		else:
			pkg = Package(cpv)
			plus, minus, unset = flags.analyse_pkg(pkg)
		for flag in plus:
			if flag in flag_users:
				flag_users[flag]["+"].append(cpv)
			else:
				flag_users[flag] = {"+": [cpv], "-": []}
		for flag in minus:
			if flag in flag_users:
				flag_users[flag]["-"].append(cpv)
			else:
				flag_users[flag] = {"+":[], "-": [cpv]}
		if include_unset:
			for flag in unset:
				if flag in flag_users:
					if "unset" in flag_users[flag]:
						flag_users[flag]["unset"].append(cpv)
					else:
						flag_users[flag]["unset"] = [cpv]
				else:
					flag_users[flag] = {"+": [], "-": [], "unset": [cpv]}
	return flag_users


def gather_keywords_info(
		cpvs=None,
		system_keywords=None,
		use_portage=False,
		#  override-able for testing
		keywords=portage.settings["ACCEPT_KEYWORDS"],
		analyser = None
		):
	"""Analyze the installed pkgs 'keywords' for frequency of use

	@param cpvs: optional list of [cat/pkg-ver,...] to analyze or
			defaults to entire installed pkg db
	@param system_keywords: list of the system keywords
	@param keywords: user defined list of keywords to check and report on
			or reports on all relevant keywords found to have been used.
	@param _get_kwds: overridable function for testing
	@param _get_used: overridable function for testing
	@rtype dict. {keyword:{"stable":[cat/pkg-ver,...], "testing":[cat/pkg-ver,...]}
	"""
	if cpvs is None:
		cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
	keyword_users = {}
	for cpv in cpvs:
		if cpv.startswith("virtual"):
			continue
		if use_portage:
			keyword = analyser.get_inst_keyword_cpv(cpv)
		else:
			pkg = Package(cpv)
			keyword = analyser.get_inst_keyword_pkg(pkg)
		#print "returned keyword =", cpv, keyword, keyword[0]
		key = keyword[0]
		if key in ["~", "-"]:
			_kwd = keyword[1:]
			if _kwd in keyword_users:
				if key in ["~"]:
					keyword_users[_kwd]["testing"].append(cpv)
				elif key in ["-"]:
					#print "adding cpv to missing:", cpv
					keyword_users[_kwd]["missing"].append(cpv)
			else:
				if key in ["~"]:
					keyword_users[_kwd] = {"stable": [],
						"testing": [cpv], "missing": []}
				elif key in ["-"]:
					keyword_users[_kwd] = {"stable": [],
						"testing": [], "missing": [cpv]}
				else:
					keyword_users[_kwd] = {"stable": [cpv],
						"testing": [], "missing": []}
		elif keyword in keyword_users:
				keyword_users[keyword]["stable"].append(cpv)
		else:
				keyword_users[keyword] = {
					"stable": [cpv],
					"testing": [],
					"missing": []
					}
	return keyword_users


class Analyse(ModuleBase):
	"""Installed db analysis tool to query the installed databse
	and produce/output stats for USE flags or keywords/mask.
	The 'rebuild' action output is in the form suitable for file type output
	to create a new package.use, package.keywords, package.unmask
	type files in the event of needing to rebuild the
	/etc/portage/* user configs
	"""
	def __init__(self):
		ModuleBase.__init__(self)
		self.command_name = "enalyze"
		self.module_name = "analyze"
		self.options = {
			"flags": False,
			"keywords": False,
			"packages": False,
			"unset": False,
			"verbose": False,
			"quiet": False,
			'prefix': False,
			'portage': True,
			"width": 80,
			"prepend": "",
		}
		self.module_opts = {
			"-f": ("flags", "boolean", True),
			"--flags": ("flags", "boolean", True),
			"-k": ("keywords", "boolean", True),
			"--keywords": ("keywords", "boolean", True),
			"-u": ("unset", "boolean", True),
			"--unset": ("unset", "boolean", True),
			"-v": ("verbose", "boolean", True),
			"--verbose": ("verbose", "boolean", True),
			"-p": ("prefix", "boolean", True),
			"--prefix": ("prefix", "boolean", True),
			"-P": ("prepend", "char", None),
			"--prepend": ("prepend", "char", None),
			"-G": ("portage", "boolean", False),
			"--portage": ("portage", "boolean", False),
			"-W": ("width", "int", 80),
			"--width": ("width", "int", 80),
		}
		self.formatted_options = [
			("  -h, --help",  "Outputs this useage message"),
			("  -u, --unset",
			"Additionally include any unset USE flags and the packages"),
			("", "that could use them"),
			("  -v, --verbose",
			"Used in the analyze action to output more detailed information"),
			("  -p, --prefix",
			"Used for testing purposes only, runs report using " +
			"a prefix keyword and 'prefix' USE flag"),
			("  -P, --prepend",
			"Prepend the string to any list output.  " +
			"ie: prepend '* ' to the ""front of each package being listed."
			"This is useful for generating preformatted wiki text."),
			#(" -G, --portage",
			#"Use portage directly instead of gentoolkit's Package " +
			#"object for some operations. Usually a little faster."),
			("  -W, --width",
			"Format the output to wrap at 'WIDTH' ie: long line output"),
		]
		self.formatted_args = [
			("  use",
			"Causes the action to analyze the installed packages USE flags"),
			("  pkguse",
			"Causes the action to analyze the installed packages PKGUSE flags"),
			("  ",
			"These are flags that have been set in /etc/portage/package.use"),
			("  keywords",
			"Causes the action to analyze the installed packages keywords"),
			("  packages",
			"Causes the action to analyze the installed packages and the"),
			("  ",
			"USE flags they were installed with"),
			("  unmask",
			"Causes the action to analyze the installed packages"),
			("  ",
			"for those that need to be unmasked")
		]
		self.short_opts = "huvpGP:W:"
		self.long_opts = ("help", "unset", "verbose", "prefix", "prepend=",
						"width=") #, "portage")
		self.need_queries = True
		self.arg_spec = "Target"
		self.arg_options = ['use', 'pkguse','keywords', 'packages', 'unmask']
		self.arg_option = False
		self.warning = (
			"   CAUTION",
			"This is beta software and some features/options are incomplete,",
			"some features may change in future releases includig its name.",
			"Feedback will be appreciated, http://bugs.gentoo.org")


	def run(self, input_args, quiet=False):
		"""runs the module

		@param input_args: input arguments to be parsed
		"""
		query = self.main_setup(input_args)
		query = self.validate_query(query)
		self.set_quiet(quiet)
		if query in ["use", "pkguse"]:
			self.analyse_flags(query)
		elif query in ["keywords"]:
			self.analyse_keywords()
		elif query in ["packages"]:
			self.analyse_packages()
		elif query in ["unmask"]:
			self.analyse_unmask()

	def analyse_flags(self, target):
		"""This will scan the installed packages db and analyze the
		USE flags used for installation and produce a report on how
		they were used.

		@type target: string
		@param target: the target to be analyzed, one of ["use", "pkguse"]
		"""
		system_use = portage.settings["USE"].split()

		self.printer = AnalysisPrinter(
				"use",
				self.options["verbose"],
				system_use,
				width=self.options["width"],
				prepend=self.options["prepend"])
		if self.options["verbose"]:
			cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
			#cpvs = get_installed_cpvs()
			#print "Total number of installed ebuilds =", len(cpvs)
			flag_users = gather_flags_info(cpvs, system_use,
				self.options["unset"], target=target.upper(),
				use_portage=self.options['portage'])
		else:
			cpvs = get_installed_cpvs()
			flag_users = gather_flags_info(cpvs, system_flags=system_use,
				include_unset=self.options["unset"], target=target.upper(),
				use_portage=self.options['portage'])
		#print flag_users
		flag_keys = sorted(flag_users)
		if self.options["verbose"]:
			print(" Flag                                 System  #pkgs   cat/pkg-ver")
			#blankline = nl
		elif not self.options['quiet']:
			print(" Flag                                 System  #pkgs")
			#blankline = lambda: None
		for flag in flag_keys:
			flag_pos = flag_users[flag]["+"]
			if len(flag_pos):
				self.printer(flag, "+", flag_pos)
				#blankline()
			flag_neg = flag_users[flag]["-"]
			if len(flag_neg):
				self.printer(flag, "-", flag_neg)
				#blankline()
			if "unset" in flag_users[flag] and flag_users[flag]["unset"]:
				flag_unset = flag_users[flag]["unset"]
				self.printer(flag, "unset", flag_unset)
			#blankline()
		if not self.options['quiet']:
			print("===================================================")
			print("Total number of flags in report =",
				pp.output.red(str(len(flag_keys))))
			if self.options["verbose"]:
				print("Total number of installed ebuilds =",
					pp.output.red(str(len([x for x in cpvs]))))
			print()


	def analyse_keywords(self, keywords=None):
		"""This will scan the installed packages db and analyze the
		keywords used for installation and produce a report on them.
		"""
		print()
		system_keywords = portage.settings["ACCEPT_KEYWORDS"]
		arch = portage.settings["ARCH"]
		if self.options["prefix"]:
			# build a new keyword for testing
			system_keywords = "~" + arch + "-linux"
		if self.options["verbose"] or self.options["prefix"]:
			print("Current system ARCH =", arch)
			print("Current system ACCEPT_KEYWORDS =", system_keywords)
		system_keywords = system_keywords.split()
		self.printer = AnalysisPrinter(
				"keywords",
				self.options["verbose"],
				system_keywords,
				width=self.options["width"],
				prepend=self.options["prepend"])
		self.analyser = KeywordAnalyser( arch, system_keywords, portage.db[portage.root]["vartree"].dbapi)
		#self.analyser.set_order(portage.settings["USE"].split())
		# only for testing
		test_use = portage.settings["USE"].split()
		if self.options['prefix'] and 'prefix' not in test_use:
			print("ANALYSE_KEYWORDS() 'prefix' flag not found in system",
				"USE flags!!!  appending for testing")
			print()
			test_use.append('prefix')
		self.analyser.set_order(test_use)
		# /end testing

		if self.options["verbose"]:
			cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
			#print "Total number of installed ebuilds =", len(cpvs)
			keyword_users = gather_keywords_info(
				cpvs=cpvs,
				system_keywords=system_keywords,
				use_portage=self.options['portage'],
				keywords=keywords, analyser = self.analyser
				)
			blankline = nl
		else:
			keyword_users = gather_keywords_info(
				system_keywords=system_keywords,
				use_portage=self.options['portage'],
				keywords=keywords,
				analyser = self.analyser
				)
			blankline = lambda: None
		#print keyword_users
		keyword_keys = sorted(keyword_users)
		if self.options["verbose"]:
			print(" Keyword               System  #pkgs   cat/pkg-ver")
		elif not self.options['quiet']:
			print(" Keyword               System  #pkgs")
		for keyword in keyword_keys:
			kwd_stable = keyword_users[keyword]["stable"]
			if len(kwd_stable):
				self.printer(keyword, " ", kwd_stable)
				blankline()
			kwd_testing = keyword_users[keyword]["testing"]
			if len(kwd_testing):
				self.printer(keyword, "~", kwd_testing)
				blankline()
			kwd_missing = keyword_users[keyword]["missing"]
			if len(kwd_missing):
				self.printer(keyword, "-", kwd_missing)
				blankline
		if not self.options['quiet']:
			if self.analyser.mismatched:
				print("_________________________________________________")
				print(("The following packages were found to have a \n" +
					"different recorded ARCH than the current system ARCH"))
				for cpv in self.analyser.mismatched:
					print("\t", pp.cpv(cpv))
			print("===================================================")
			print("Total number of keywords in report =",
				pp.output.red(str(len(keyword_keys))))
			if self.options["verbose"]:
				print("Total number of installed ebuilds =",
					pp.output.red(str(len(cpvs))))
			print()


	def analyse_packages(self):
		"""This will scan the installed packages db and analyze the
		USE flags used for installation and produce a report.

		@type target: string
		@param target: the target to be analyzed, one of ["use", "pkguse"]
		"""
		system_use = portage.settings["USE"].split()
		if self.options["verbose"]:
			cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
			key_width = 45
		else:
			cpvs = get_installed_cpvs()
			key_width = 1

		self.printer = AnalysisPrinter(
				"packages",
				self.options["verbose"],
				key_width=key_width,
				width=self.options["width"],
				prepend=self.options["prepend"])

		cpvs = sorted(cpvs)
		flags = FlagAnalyzer(
					system=system_use,
					filter_defaults=False,
					target="USE"
					)

		if self.options["verbose"]:
			print("   cat/pkg-ver                             USE Flags")
				#   "app-emulation/emul-linux-x86-sdl-20100915 ...."
			#blankline = nl
		elif not self.options['quiet']:
			print("   cat/pkg-ver                             USE Flags")
			#blankline = lambda: None
		for cpv in cpvs:
			(flag_plus, flag_neg, unset) = flags.analyse_cpv(cpv)
			if self.options["unset"]:
				self.printer(cpv, "", (sorted(flag_plus), sorted(flag_neg),
					sorted(unset)))
			else:
				self.printer(cpv, "", (sorted(flag_plus), sorted(flag_neg), []))
		if not self.options['quiet']:
			print("===================================================")
			print("Total number of installed ebuilds =",
				pp.output.red(str(len([x for x in cpvs]))))
			print()


	def analyse_unmask(self):
		"""This will scan the installed packages db and analyze the
		unmasking used for installation and produce a report on them.
		"""
		self.not_implemented("unmask")



def main(input_args):
	"""Common starting method by the analyze master
	unless all modules are converted to this class method.

	@param input_args: input args as supplied by equery master module.
	"""
	query_module = Analyse()
	query_module.run(input_args, gentoolkit.CONFIG['quiet'])

# vim: set ts=4 sw=4 tw=79: