summaryrefslogtreecommitdiff
blob: c45530a9143253ba351f6c0559f0ccfb1dcbaf2b (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
#!/usr/bin/env python
# Copyright 2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import curses.ascii
import itertools
import optparse
import os
import re
import subprocess
import sys
import textwrap
import xml.etree

import bugz.bugzilla
import portage.versions

CPV_REGEX = re.compile("[A-Za-z0-9+_.-]+/[A-Za-z0-9+_-]+-[0-9]+(?:\.[0-9]+)*[a-z0-9_]*(?:-r[0-9]+)?")

def unicode_sanitize(text):
	"""Converts a possibly unicode text to a regular string."""
	if type(text) == unicode:
		real_output = text
	else:
		real_output = unicode(text, errors='replace')
	return real_output.encode("utf-8")

class TermTooSmall(Exception):
	pass

class Bug:
	def __init__(self, xml=None, id_number=None, summary=None, status=None):
		if xml is not None:
			self.__id = int(xml.find("bug_id").text)
			self.__summary = xml.find("short_desc").text
			self.__status = xml.find("bug_status").text
			self.__depends_on = [int(dep.text) for dep in xml.findall("dependson")]
			self.__comments = [c.find("who").text + "\n" + c.find("thetext").text for c in xml.findall("long_desc")]
		if id_number is not None:
			self.__id = id_number
		if summary is not None:
			self.__summary = summary
		if status is not None:
			self.__status = status
		self.__cpvs_detected = False
		self.__cpvs = []
	
	def detect_cpvs(self):
		if self.__cpvs_detected:
			return
		for cpv_candidate in CPV_REGEX.findall(self.summary()):
			if portage.db["/"]["porttree"].dbapi.cpv_exists(cpv_candidate):
				self.__cpvs.append(cpv_candidate)
		self.__cpvs_detected = True
	
	def id_number(self):
		return self.__id
	
	def summary(self):
		return self.__summary
	
	def status(self):
		return self.__status
	
	def depends_on(self):
		return self.__depends_on
	
	def comments(self):
		return self.__comments
	
	def cpvs(self):
		assert(self.__cpvs_detected)
		return self.__cpvs

class BugQueue:
	def __init__(self):
		self.__bug_list = []
		self.__bug_set = set()
	
	def add_bug(self, bug):
		if self.has_bug(bug):
			return
		self.__bug_list.append(bug)
		self.__bug_set.add(bug.id_number())
	
	def has_bug(self, bug):
		return bug.id_number() in self.__bug_set
	
	def generate_stabilization_list(self):
		result = []
		for bug in self.__bug_list:
			result.append("# Bug %d: %s" % (bug.id_number(), bug.summary()))
			for cpv in bug.cpvs():
				result.append("=" + cpv)
		return "\n".join(result)

# Main class (called with curses.wrapper later).
class MainWindow:
	def __init__(self, screen, bugs, bugs_dict, related_bugs, repoman_dict, bug_queue):
		self.bugs = bugs
		self.bugs_dict = bugs_dict
		self.related_bugs = related_bugs
		self.repoman_dict = repoman_dict
		self.bug_queue = bug_queue

		curses.curs_set(0)
		self.screen = screen

		curses.use_default_colors()
		self.init_screen()

		c = self.screen.getch()
		while c not in (ord("q"), curses.ascii.ESC):
			if c == ord("j"):
				self.scroll_bugs_pad(1)
			elif c == ord("k"):
				self.scroll_bugs_pad(-1)
			elif c == curses.KEY_DOWN:
				self.scroll_contents_pad(1)
			elif c == curses.KEY_UP:
				self.scroll_contents_pad(-1)
			elif c == curses.KEY_RESIZE:
				self.init_screen()
			elif c == ord("a"):
				self.add_bug_to_queue()

			c = self.screen.getch()
	
	def bug_for_id(self, bug_id):
		if bug_id in self.bugs_dict:
			return self.bugs_dict[bug_id]
		return Bug(id_number=bug_id, summary='(summary unavailable)', status='UNKNOWN')

	def init_screen(self):
		(self.height, self.width) = self.screen.getmaxyx()

		if self.height < 12 or self.width < 80:
			raise TermTooSmall()

		self.screen.border()
		self.screen.vline(1, self.width / 3, curses.ACS_VLINE, self.height - 2)
		self.screen.refresh()

		self.fill_bugs_pad()
		self.refresh_bugs_pad()

		self.fill_contents_pad()
		self.refresh_contents_pad()

	def fill_bugs_pad(self):
		self.bugs_pad = curses.newpad(len(self.bugs),self.width)
		self.bugs_pad.erase()

		self.bugs_pad_pos = 0

		for i in range(len(self.bugs)):
			self.bugs_pad.addstr(i, 0,
				unicode_sanitize("    %d %s" % (self.bugs[i].id_number(), self.bugs[i].summary())))

	def scroll_bugs_pad(self, amount):
		height = len(self.bugs)

		self.bugs_pad_pos += amount
		if self.bugs_pad_pos < 0:
			self.bugs_pad_pos = 0
		if self.bugs_pad_pos >= height:
			self.bugs_pad_pos = height - 1
		self.refresh_bugs_pad()

		self.fill_contents_pad()
		self.refresh_contents_pad()

	def refresh_bugs_pad(self):
		(height, width) = self.bugs_pad.getmaxyx()
		for i in range(height):
			self.bugs_pad.addstr(i, 0, "   ")
			if self.bug_queue.has_bug(self.bugs[i]):
				self.bugs_pad.addch(i, 2, "+")
		self.bugs_pad.addch(self.bugs_pad_pos, 0, "*")
		pos = min(height - self.height + 2, max(0, self.bugs_pad_pos - (self.height / 2)))
		self.bugs_pad.refresh(
			pos, 0,
			1, 1,
			self.height - 2, self.width / 3 - 1)

	def fill_contents_pad(self):
		width = 2 * self.width / 3

		bug = self.bugs[self.bugs_pad_pos]

		output = []
		output += textwrap.wrap(bug.summary(), width=width-2)
		output.append("-" * (width - 2))

		cpvs = bug.cpvs()
		if cpvs:
			output += textwrap.wrap("Found package cpvs:", width=width-2)
			for cpv in cpvs:
				output += textwrap.wrap(cpv, width=width-2)
			output += textwrap.wrap("Press 'a' to add them to the stabilization queue.", width=width-2)
			output.append("-" * (width - 2))

		deps = [self.bug_for_id(dep_id) for dep_id in bug.depends_on()]
		if deps:
			output += textwrap.wrap("Depends on:", width=width-2)
			for dep in deps:
				desc = "%d %s %s" % (dep.id_number(), dep.status(), dep.summary())
				output += textwrap.wrap(desc, width=width-2)
			output.append("-" * (width - 2))
	
		related = self.related_bugs[bug.id_number()]
		if related:
			output += textwrap.wrap("Related bugs:", width=width-2)
			for related_bug in related:
				if str(related_bug['bugid']) == str(bug.id_number()):
					continue
				desc = related_bug['bugid'] + " " + related_bug['desc']
				output += textwrap.wrap(desc, width=width-2)
			output.append("-" * (width - 2))
	
		if bug.id_number() in repoman_dict and repoman_dict[bug.id_number()]:
			output += textwrap.wrap("Repoman output:", width=width-2)
			lines = repoman_dict[bug.id_number()].split("\n")
			for line in lines:
				output += textwrap.wrap(line, width=width-2)
			output.append("-" * (width - 2))
	
		for comment in bug.comments():
			for line in comment.split("\n"):
				output += textwrap.wrap(line, width=width-2)
			output.append("-" * (width - 2))

		self.contents_pad_length = len(output)

		self.contents_pad = curses.newpad(max(self.contents_pad_length, self.height), width)
		self.contents_pad.erase()

		self.contents_pad_pos = 0

		for i in range(len(output)):
			self.contents_pad.addstr(i, 0, unicode_sanitize(output[i]))

	def scroll_contents_pad(self, amount):
		height = self.contents_pad_length - self.height + 3

		self.contents_pad_pos += amount
		if self.contents_pad_pos < 0:
			self.contents_pad_pos = 0
		if self.contents_pad_pos >= height:
			self.contents_pad_pos = height - 1
		self.refresh_contents_pad()

	def refresh_contents_pad(self):
		self.contents_pad.refresh(
			self.contents_pad_pos, 0,
			1, self.width / 3 + 1,
			self.height - 2, self.width - 2)
		self.screen.refresh()
	
	def add_bug_to_queue(self):
		bug = self.bugs[self.bugs_pad_pos]

		# For now we only support auto-detected CPVs.
		if not bug.cpvs():
			return

		self.bug_queue.add_bug(bug)
		self.refresh_bugs_pad()

if __name__ == "__main__":
	parser = optparse.OptionParser()
	parser.add_option("--arch", dest="arch", help="Gentoo arch to use, e.g. x86, amd64, ...")
	parser.add_option("-o", "--output", dest="output_filename", default="package.keywords", help="Output filename for generated package.keywords file [default=%default]")
	parser.add_option("--repo", dest="repo", help="Path to portage CVS repository")
	parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Include more output, e.g. related bugs")

	(options, args) = parser.parse_args()
	if not options.arch:
		parser.error("--arch option is required")
	if args:
		parser.error("unrecognized command-line args")

	bug_queue = BugQueue()

	bugzilla = bugz.bugzilla.Bugz('http://bugs.gentoo.org', skip_auth=True)

	print "Searching for arch bugs..."
	raw_bugs = bugzilla.search("", cc="%s@gentoo.org" % options.arch, keywords="STABLEREQ", status=None)
	bugs = [Bug(xml) for xml in bugzilla.get([bug['bugid'] for bug in raw_bugs]).findall("bug")]

	if not bugs:
		print 'The bug list is empty. Exiting.'
		sys.exit(0)

	dep_bug_ids = []

	bugs_dict = {}
	related_bugs = {}
	repoman_dict = {}
	for bug in bugs:
		print "Processing bug %d: %s" % (bug.id_number(), bug.summary())
		bugs_dict[bug.id_number()] = bug
		related_bugs[bug.id_number()] = []
		repoman_dict[bug.id_number()] = ""
		bug.detect_cpvs()
		for cpv in bug.cpvs():
			pv = portage.versions.cpv_getkey(cpv)
			if options.verbose:
				related_bugs[bug.id_number()] += bugzilla.search(pv, status=None)

			if options.repo:
				cvs_path = os.path.join(options.repo, pv)
				ebuild_name = portage.versions.catsplit(cpv)[1] + ".ebuild"
				ebuild_path = os.path.join(cvs_path, ebuild_name)
				manifest_path = os.path.join(cvs_path, 'Manifest')
				if os.path.exists(ebuild_path):
					original_contents = open(ebuild_path).read()
					manifest_contents = open(manifest_path).read()
					try:
						output = repoman_dict[bug.id_number()]
						output += subprocess.Popen(["ekeyword", options.arch, ebuild_name], cwd=cvs_path, stdout=subprocess.PIPE).communicate()[0]
						subprocess.check_call(["repoman", "manifest"], cwd=cvs_path)
						output += subprocess.Popen(["repoman", "full"], cwd=cvs_path, stdout=subprocess.PIPE).communicate()[0]
						repoman_dict[bug.id_number()] = output
					finally:
						f = open(ebuild_path, "w")
						f.write(original_contents)
						f.close()
						f = open(manifest_path, "w")
						f.write(manifest_contents)
						f.close()
		dep_bug_ids += bug.depends_on()

	dep_bug_ids = list(set(dep_bug_ids))
	dep_bugs = [Bug(xml) for xml in bugzilla.get(dep_bug_ids).findall("bug")]
	for bug in dep_bugs:
		bugs_dict[bug.id_number()] = bug

	try:
		curses.wrapper(MainWindow, bugs=bugs, bugs_dict=bugs_dict, related_bugs=related_bugs, repoman_dict=repoman_dict, bug_queue=bug_queue)
	except TermTooSmall:
		print "Your terminal window is too small, please try to enlarge it"
		sys.exit(1)
	
	stabilization_list = bug_queue.generate_stabilization_list()
	if stabilization_list:
		with open(options.output_filename, "w") as f:
			f.write(stabilization_list)
			print "Writing stabilization list to %s" % options.output_filename