aboutsummaryrefslogtreecommitdiff
blob: 11f00d89706cf2046e6b738dd06565ec7418a691 (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
# -*- coding:utf-8 -*-

import logging
import os

from _emerge.Package import Package

# import our initialized portage instance
from repoman._portage import portage
from repoman.config import load_config


class QAData(object):

	def __init__(self):
		# Create the main exported data variables
		self.max_desc_len = None
		self.allowed_filename_chars = None
		self.qahelp = None
		self.qacats = None
		self.qawarnings = None
		self.missingvars = None
		self.allvars = None
		self.valid_restrict = None
		self.suspect_rdepend = None
		self.suspect_virtual = None
		self.ruby_deprecated = None
		self.no_exec = None


	def load_repo_config(self, repopaths, options, valid_versions):
		'''Load the repository repoman qa_data.yml config

		@param repopaths: list of strings, The path of the repository being scanned
						 This could be a parent repository using the
						 repoman_masters layout.conf variable
		'''
		qadata = load_config([os.path.join(path,'qa_data.yaml') for path in repopaths], 'yaml', valid_versions)
		if qadata == {}:
			logging.error("QAData: Failed to load a valid 'qa_data.yaml' file at paths: %s", repopaths)
			return False
		self.max_desc_len = qadata.get('max_description_length', 80)
		self.allowed_filename_chars = qadata.get("allowed_filename_chars", "a-zA-Z0-9._-+:")

		self.qahelp = qadata.get('qahelp', {})

		self.qacats = []
		for x in sorted(self.qahelp):
			for y in sorted(self.qahelp[x]):
				self.qacats.append('.'.join([x, y]))
		self.qacats.sort()

		self.qawarnings = set(qadata.get('qawarnings', []))
		if options.experimental_inherit == 'y':
			# This is experimental, so it's non-fatal.
			self.qawarnings.add("inherit.missing")

		self.missingvars = qadata.get("missingvars", [])
		logging.debug("QAData: missingvars: %s", self.missingvars)
		self.allvars = set(x for x in portage.auxdbkeys if not x.startswith("UNUSED_"))
		self.allvars.update(Package.metadata_keys)
		self.allvars = sorted(self.allvars)

		for x in self.missingvars:
			x += ".missing"
			if x not in self.qacats:
				logging.warning('QAData: * missingvars values need to be added to qahelp ("%s")' % x)
				self.qacats.append(x)
				self.qawarnings.add(x)

		self.valid_restrict = frozenset(qadata.get("valid_restrict", []))

		self.suspect_rdepend = frozenset(qadata.get("suspect_rdepend", []))

		self.suspect_virtual = qadata.get("suspect_virtual", {})

		self.ruby_deprecated = frozenset(qadata.get("ruby_deprecated", []))

		# file.executable
		self.no_exec = frozenset(qadata.get("no_exec_files", []))
		logging.debug("QAData: completed loading file: %s", repopaths)
		return True


def format_qa_output(
	formatter, fails, dofull, dofail, options, qawarnings):
	"""Helper function that formats output properly

	@param formatter: an instance of Formatter
	@type formatter: Formatter
	@param fails: dict of qa status failures
	@type fails: dict
	@param dofull: Whether to print full results or a summary
	@type dofull: boolean
	@param dofail: Whether failure was hard or soft
	@type dofail: boolean
	@param options: The command-line options provided to repoman
	@type options: Namespace
	@param qawarnings: the set of warning types
	@type qawarnings: set
	@return: None (modifies formatter)
	"""
	full = options.mode == 'full'
	# we only want key value pairs where value > 0
	for category in sorted(fails):
		number = len(fails[category])
		formatter.add_literal_data("  " + category)
		spacing_width = 30 - len(category)
		if category in qawarnings:
			formatter.push_style("WARN")
		else:
			formatter.push_style("BAD")
			formatter.add_literal_data(" [fatal]")
			spacing_width -= 8

		formatter.add_literal_data(" " * spacing_width)
		formatter.add_literal_data("%s" % number)
		formatter.pop_style()
		formatter.add_line_break()
		if not dofull:
			if not full and dofail and category in qawarnings:
				# warnings are considered noise when there are failures
				continue
			fails_list = fails[category]
			if not full and len(fails_list) > 12:
				fails_list = fails_list[:12]
			for failure in fails_list:
				formatter.add_literal_data("   " + failure)
				formatter.add_line_break()


def format_qa_output_column(
	formatter, fails, dofull, dofail, options, qawarnings):
	"""Helper function that formats output in a machine-parseable column format

	@param formatter: an instance of Formatter
	@type formatter: Formatter
	@param fails: dict of qa status failures
	@type fails: dict
	@param dofull: Whether to print full results or a summary
	@type dofull: boolean
	@param dofail: Whether failure was hard or soft
	@type dofail: boolean
	@param options: The command-line options provided to repoman
	@type options: Namespace
	@param qawarnings: the set of warning types
	@type qawarnings: set
	@return: None (modifies formatter)
	"""
	full = options.mode == 'full'
	for category in sorted(fails):
		number = len(fails[category])
		formatter.add_literal_data("NumberOf " + category + " ")
		if category in qawarnings:
			formatter.push_style("WARN")
		else:
			formatter.push_style("BAD")
		formatter.add_literal_data("%s" % number)
		formatter.pop_style()
		formatter.add_line_break()
		if not dofull:
			if not full and dofail and category in qawarnings:
				# warnings are considered noise when there are failures
				continue
			fails_list = fails[category]
			if not full and len(fails_list) > 12:
				fails_list = fails_list[:12]
			for failure in fails_list:
				formatter.add_literal_data(category + " " + failure)
				formatter.add_line_break()