aboutsummaryrefslogtreecommitdiff
blob: 53fded4bcaf59028f51b1f0192c85778509d2d1a (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
#!/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 rebuild file of USE flags or keywords used and by
what packages according to the Installed package database"""


from __future__ import print_function


import os
import sys
if sys.hexversion < 0x3000000:
	from io import open

import gentoolkit
from gentoolkit.module_base import ModuleBase
from gentoolkit import pprinter as pp
from gentoolkit.enalyze.lib import (get_installed_use, get_flags, FlagAnalyzer,
	KeywordAnalyser)
from gentoolkit.flag import reduce_flags
from gentoolkit.enalyze.output import RebuildPrinter
from gentoolkit.atom import Atom


import portage
from portage import _encodings, _unicode_decode, _unicode_encode

def cpv_all_diff_use(
		cpvs=None,
		system_flags=None,
		#  override-able for testing
		_get_flags=get_flags,
		_get_used=get_installed_use
		):
	"""Data gathering and analysis function determines
	the difference between the current default USE flag settings
	and the currently installed pkgs recorded USE flag settings

	@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 _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. {cpv:['flag1', '-flag2',...]}
	"""
	if cpvs is None:
		cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
	cpvs.sort()
	data = {}
	cp_counts = {}
	# pass them in to override for tests
	flags = FlagAnalyzer(system_flags,
		filter_defaults=True,
		target="USE",
		_get_flags=_get_flags,
		_get_used=get_installed_use
	)
	for cpv in cpvs:
		plus, minus, unset = flags.analyse_cpv(cpv)
		atom = Atom("="+cpv)
		atom.slot = portage.db[portage.root]["vartree"].dbapi.aux_get(atom.cpv, ["SLOT"])[0]
		for flag in minus:
			plus.add("-"+flag)
		if len(plus):
			if atom.cp not in data:
				data[atom.cp] = []
			if atom.cp not in cp_counts:
				cp_counts[atom.cp] = 0
			atom.use = list(plus)
			data[atom.cp].append(atom)
			cp_counts[atom.cp] += 1
	return data, cp_counts


def cpv_all_diff_keywords(
		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 difference from ACCEPT_KEYWORDS

	@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 = {}
	cp_counts = {}
	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 ["~", "-"] and keyword not in system_keywords:
			atom = Atom("="+cpv)
			if atom.cp not in keyword_users:
				keyword_users[atom.cp] = []
			if atom.cp not in cp_counts:
				cp_counts[atom.cp] = 0
			if key in ["~"]:
				atom.keyword = keyword
				atom.slot = portage.db[portage.root]["vartree"].dbapi.aux_get(atom.cpv, ["SLOT"])[0]
				keyword_users[atom.cp].append(atom)
				cp_counts[atom.cp] += 1
			elif key in ["-"]:
				#print "adding cpv to missing:", cpv
				atom.keyword = "**"
				atom.slot = portage.db[portage.root]["vartree"].dbapi.aux_get(atom.cpv, ["SLOT"])[0]
				keyword_users[atom.cp].append(atom)
				cp_counts[atom.cp] += 1
	return keyword_users, cp_counts


class Rebuild(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 = "rebuild"
		self.options = {
			"use": False,
			"keywords": False,
			"unmask": False,
			"verbose": False,
			"quiet": False,
			"exact": False,
			"pretend": False,
			"prefix": False,
			"portage": True,
			"slot": False
			#"unset": False
		}
		self.module_opts = {
			"-p": ("pretend", "boolean", True),
			"--pretend": ("pretend", "boolean", True),
			"-e": ("exact", "boolean", True),
			"--exact": ("exact", "boolean", True),
			"-s": ("slot", "boolean", True),
			"--slot": ("slot", "boolean", True),
			"-v": ("verbose", "boolean", True),
			"--verbose": ("verbose", "boolean", True),
		}
		self.formatted_options = [
			("    -h, --help",  "Outputs this useage message"),
			("    -p, --pretend", "Does not actually create the files."),
			("    ", "It directs the outputs to the screen"),
			("    -e, --exact", "will atomize the package with a"),
			("  ", "leading '=' and include the version"),
			("    -s, --slot", "will atomize the package with a"),
			("  ", "leading '=' and include the slot")
		]
		self.formatted_args = [
			("    use",
			"causes the action to analyze the installed packages USE flags"),
			("    keywords",
			"causes the action to analyze the installed packages keywords"),
			("    unmask",
			"causes the action to analyze the installed packages " + \
			"current mask status")
		]
		self.short_opts = "hepsv"
		self.long_opts = ("help", "exact", "pretend", "slot", "verbose")
		self.need_queries = True
		self.arg_spec = "TargetSpec"
		self.arg_options = ['use', 'keywords', '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.",
			"The file generated is saved in your home directory",
			"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
		"""
		self.options['quiet'] = quiet
		query = self.main_setup(input_args)
		query = self.validate_query(query)
		if query in ["use"]:
			self.rebuild_use()
		elif query in ["keywords"]:
			self.rebuild_keywords()
		elif query in ["unmask"]:
			self.rebuild_unmask()


	def rebuild_use(self):
		if not self.options["quiet"]:
			print()
			print("  -- Scanning installed packages for USE flag settings that")
			print("     do not match the default settings")
		system_use = portage.settings["USE"].split()
		output = RebuildPrinter(
			"use", self.options["pretend"], self.options["exact"],
				self.options['slot'])
		pkgs, cp_counts = cpv_all_diff_use(system_flags=system_use)
		pkg_count = len(pkgs)
		if self.options["verbose"]:
			print()
			print((pp.emph("  -- Found ") +  pp.number(str(pkg_count)) +
				pp.emph(" packages that need entries")))
			#print pp.emph("     package.use to maintain their current setting")
		pkg_keys = []
		if pkgs:
			pkg_keys = sorted(pkgs)
			#print len(pkgs)
			if self.options["pretend"] and not self.options["quiet"]:
				print()
				print(pp.globaloption(
					"  -- These are the installed packages & use flags " +
					"that were detected"))
				print(pp.globaloption("     to need use flag settings other " +
					"than the defaults."))
				print()
			elif not self.options["quiet"]:
				print("  -- preparing pkgs for file entries")
			for pkg in pkg_keys:
				output(pkg, pkgs[pkg], cp_counts[pkg])
			if self.options['verbose']:
				message = (pp.emph("     ") +
					pp.number(str(pkg_count)) +
					pp.emph(" different packages"))
				print()
				print(pp.globaloption("  -- Totals"))
				print(message)
				#print
				#unique = list(unique_flags)
				#unique.sort()
				#print unique
			if not self.options["pretend"]:
				filepath = os.path.expanduser('~/package.use.test')
				self.save_file(filepath, output.lines)

	def rebuild_keywords(self):
		#print("Module action not yet available")
		#print()
		"""This will scan the installed packages db and analyze the
		keywords used for installation and produce a report on them.
		"""
		system_keywords = portage.settings["ACCEPT_KEYWORDS"].split()
		output = RebuildPrinter(
			"keywords", self.options["pretend"], self.options["exact"],
			self.options['slot'])
		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)
		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("REBUILD_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

		cpvs = portage.db[portage.root]["vartree"].dbapi.cpv_all()
		#print "Total number of installed ebuilds =", len(cpvs)
		pkgs, cp_counts = cpv_all_diff_keywords(
			cpvs=cpvs,
			system_keywords=system_keywords,
			use_portage=self.options['portage'],
			analyser = self.analyser
			)
		#print([pkgs[p][0].cpv for p in pkgs])
		pkg_keys = []
		if pkgs:
			pkg_keys = sorted(pkgs)
			#print(len(pkgs))
			if self.options["pretend"] and not self.options["quiet"]:
				print()
				print(pp.globaloption(
					"  -- These are the installed packages & keywords " +
					"that were detected"))
				print(pp.globaloption("     to need keyword settings other " +
					"than the defaults."))
				print()
			elif not self.options["quiet"]:
				print("  -- preparing pkgs for file entries")
			for pkg in pkg_keys:
				output(pkg, pkgs[pkg], cp_counts[pkg])
		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 entries in report =",
				pp.output.red(str(len(pkg_keys))))
			if self.options["verbose"]:
				print("Total number of installed ebuilds =",
					pp.output.red(str(len(cpvs))))
			print()
			if not self.options["pretend"]:
				filepath = os.path.expanduser('~/package.keywords.test')
				self.save_file(filepath, output.lines)


	def rebuild_unmask(self):
		self.not_implemented("unmask")


	def save_file(self, filepath, data):
		"""Writes the data to the file determined by filepath

		@param filepath: string. eg. '/path/to/filename'
		@param data: list of lines to write to filepath
		"""
		if  not self.options["quiet"]:
			print('   - Saving file: %s' %filepath)
		with open(_unicode_encode(filepath, encoding=_encodings['fs']), mode="w",
				encoding=_encodings['content']) as output:
			output.write('\n'.join(data))
		print("   - Done")


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 = Rebuild()
	query_module.run(input_args, gentoolkit.CONFIG['quiet'])

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