summaryrefslogtreecommitdiff
blob: 957598e600e484e9e057b3893e957b5f03faa6d4 (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/usr/bin/env python

import base64
import csv
import getpass
import locale
import mimetypes
import os
import re
import sys

from cookielib import LWPCookieJar, CookieJar
from cStringIO import StringIO
from urlparse import urlsplit, urljoin
from urllib import urlencode, quote
from urllib2 import build_opener, HTTPCookieProcessor, Request

from config import config

from xml.etree import ElementTree

COOKIE_FILE = '.bugz_cookie'

#
# Return a string truncated to the given length if it is longer.
#

def ellipsis(text, length):
	if len(text) > length:
		return text[:length-4] + "..."
	else:
		return text

#
# HTTP file uploads in Python
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
#

def post_multipart(host, selector, fields, files):
	"""
	Post fields and files to an http host as multipart/form-data.
	fields is a sequence of (name, value) elements for regular form fields.
	files is a sequence of (name, filename, value) elements for data to be uploaded as files
	Return the server's response page.
	"""
	content_type, body = encode_multipart_formdata(fields, files)
	h = httplib.HTTP(host)
	h.putrequest('POST', selector)
	h.putheader('content-type', content_type)
	h.putheader('content-length', str(len(body)))
	h.endheaders()
	h.send(body)
	errcode, errmsg, headers = h.getreply()
	return h.file.read()

def encode_multipart_formdata(fields, files):
	"""
	fields is a sequence of (name, value) elements for regular form fields.
	files is a sequence of (name, filename, value) elements for data to be uploaded as files
	Return (content_type, body) ready for httplib.HTTP instance
	"""
	BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
	CRLF = '\r\n'
	L = []
	for (key, value) in fields:
		L.append('--' + BOUNDARY)
		L.append('Content-Disposition: form-data; name="%s"' % key)
		L.append('')
		L.append(value)
	for (key, filename, value) in files:
		L.append('--' + BOUNDARY)
		L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
		L.append('Content-Type: %s' % get_content_type(filename))
		L.append('')
		L.append(value)
	L.append('--' + BOUNDARY + '--')
	L.append('')
	body = CRLF.join(L)
	content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
	return content_type, body

def get_content_type(filename):
	return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

#
# Override the behaviour of elementtree and allow us to
# force the encoding to utf-8
# Not needed in Python 2.7, since ElementTree.XMLTreeBuilder uses the forced
# encoding.
#

class ForcedEncodingXMLTreeBuilder(ElementTree.XMLTreeBuilder):
	def __init__(self, html = 0, target = None, encoding = None):
		try:
			from xml.parsers import expat
		except ImportError:
			raise ImportError(
				"No module named expat; use SimpleXMLTreeBuilder instead"
				)
		self._parser = parser = expat.ParserCreate(encoding, "}")
		if target is None:
			target = ElementTree.TreeBuilder()
		self._target = target
		self._names = {} # name memo cache
		# callbacks
		parser.DefaultHandlerExpand = self._default
		parser.StartElementHandler = self._start
		parser.EndElementHandler = self._end
		parser.CharacterDataHandler = self._data
		# let expat do the buffering, if supported
		try:
			self._parser.buffer_text = 1
		except AttributeError:
			pass
		# use new-style attribute handling, if supported
		try:
			self._parser.ordered_attributes = 1
			self._parser.specified_attributes = 1
			parser.StartElementHandler = self._start_list
		except AttributeError:
			pass
		encoding = None
		if not parser.returns_unicode:
			encoding = "utf-8"
		# target.xml(encoding, None)
		self._doctype = None
		self.entity = {}

#
# Real bugzilla interface
#

class Bugz:
	""" Converts sane method calls to Bugzilla HTTP requests.

	@ivar base: base url of bugzilla.
	@ivar user: username for authenticated operations.
	@ivar password: password for authenticated operations
	@ivar cookiejar: for authenticated sessions so we only auth once.
	@ivar forget: forget user/password after session.
	@ivar authenticated: is this session authenticated already
	"""

	def __init__(self, base, user = None, password = None, forget = False,
			skip_auth = False, httpuser = None, httppassword = None ):
		"""
		{user} and {password} will be prompted if an action needs them
		and they are not supplied.

		if {forget} is set, the login cookie will be destroyed on quit.

		@param base: base url of the bugzilla
		@type  base: string
		@keyword user: username for authenticated actions.
		@type    user: string
		@keyword password: password for authenticated actions.
		@type    password: string
		@keyword forget: forget login session after termination.
		@type    forget: bool
		@keyword skip_auth: do not authenticate
		@type    skip_auth: bool
		"""
		self.base = base
		scheme, self.host, self.path, query, frag  = urlsplit(self.base)
		self.authenticated = False
		self.forget = forget

		if not self.forget:
			try:
				cookie_file = os.path.join(os.environ['HOME'], COOKIE_FILE)
				self.cookiejar = LWPCookieJar(cookie_file)
				if forget:
					try:
						self.cookiejar.load()
						self.cookiejar.clear()
						self.cookiejar.save()
						os.chmod(self.cookiejar.filename, 0600)
					except IOError:
						pass
			except KeyError:
				self.warn('Unable to save session cookies in %s' % cookie_file)
				self.cookiejar = CookieJar(cookie_file)
		else:
			self.cookiejar = CookieJar()

		self.opener = build_opener(HTTPCookieProcessor(self.cookiejar))
		self.user = user
		self.password = password
		self.httpuser = httpuser
		self.httppassword = httppassword
		self.skip_auth = skip_auth

	def log(self, status_msg):
		"""Default logging handler. Expected to be overridden by
		the UI implementing subclass.

		@param status_msg: status message to print
		@type  status_msg: string
		"""
		return

	def warn(self, warn_msg):
		"""Default logging handler. Expected to be overridden by
		the UI implementing subclass.

		@param status_msg: status message to print
		@type  status_msg: string
		"""
		return

	def get_input(self, prompt):
		"""Default input handler. Expected to be override by the
		UI implementing subclass.

		@param prompt: Prompt message
		@type  prompt: string
		"""
		return ''

	def auth(self):
		"""Authenticate a session.
		"""
		# check if we need to authenticate
		if self.authenticated:
			return

		# try seeing if we really need to request login
		if not self.forget:
			try:
				self.cookiejar.load()
			except IOError:
				pass

		req_url = urljoin(self.base, config.urls['auth'])
		req_url += '?GoAheadAndLogIn=1'
		req = Request(req_url, None, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)
		re_request_login = re.compile(r'<title>.*Log in to .*</title>')
		if not re_request_login.search(resp.read()):
			self.log('Already logged in.')
			self.authenticated = True
			return

		# prompt for username if we were not supplied with it
		if not self.user:
			self.log('No username given.')
			self.user = self.get_input('Username: ')

		# prompt for password if we were not supplied with it
		if not self.password:
			self.log('No password given.')
			self.password = getpass.getpass()

		# perform login
		qparams = config.params['auth'].copy()
		qparams['Bugzilla_login'] = self.user
		qparams['Bugzilla_password'] = self.password
		if not self.forget:
			qparams['Bugzilla_remember'] = 'on'

		req_url = urljoin(self.base, config.urls['auth'])
		req = Request(req_url, urlencode(qparams), config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)
		if resp.info().has_key('Set-Cookie'):
			self.authenticated = True
			if not self.forget:
				self.cookiejar.save()
				os.chmod(self.cookiejar.filename, 0600)
			return True
		else:
			raise RuntimeError("Failed to login")

	def extractResults(self, resp):
		# parse the results into dicts.
		results = []
		columns = []
		rows = []

		for r in csv.reader(resp): rows.append(r)
		for field in rows[0]:
			if config.choices['column_alias'].has_key(field):
				columns.append(config.choices['column_alias'][field])
			else:
				self.log('Unknown field: ' + field)
				columns.append(field)
		for row in rows[1:]:
			if "Missing Search" in row[0]:
				self.log('Bugzilla error (Missing search found)')
				return None
			fields = {}
			for i in range(min(len(row), len(columns))):
				fields[columns[i]] = row[i]
			results.append(fields)
		return results

	def search(self, query, comments = False, order = 'number',
			assigned_to = None, reporter = None, cc = None,
			commenter = None, whiteboard = None, keywords = None,
			status = [], severity = [], priority = [], product = [],
			component = []):
		"""Search bugzilla for a bug.

		@param query: query string to search in title or {comments}.
		@type  query: string
		@param order: what order to returns bugs in.
		@type  order: string

		@keyword assigned_to: email address which the bug is assigned to.
		@type    assigned_to: string
		@keyword reporter: email address matching the bug reporter.
		@type    reporter: string
		@keyword cc: email that is contained in the CC list
		@type    cc: string
		@keyword commenter: email of a commenter.
		@type    commenter: string

		@keyword whiteboard: string to search in status whiteboard (gentoo?)
		@type    whiteboard: string
		@keyword keywords: keyword to search for
		@type    keywords: string

		@keyword status: bug status to match. default is ['NEW', 'ASSIGNED',
						 'REOPENED'].
		@type    status: list
		@keyword severity: severity to match, empty means all.
		@type    severity: list
		@keyword priority: priority levels to patch, empty means all.
		@type    priority: list
		@keyword comments: search comments instead of just bug title.
		@type    comments: bool
		@keyword product: search within products. empty means all.
		@type    product: list
		@keyword component: search within components. empty means all.
		@type    component: list

		@return: list of bugs, each bug represented as a dict
		@rtype: list of dicts
		"""

		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['list'].copy()
		if comments:
			qparams['long_desc'] = query
		else:
			qparams['short_desc'] = query

		qparams['order'] = config.choices['order'].get(order, 'Bug Number')
		qparams['bug_severity'] = severity or []
		qparams['priority'] = priority or []
		if status is None:
			# NEW, ASSIGNED and REOPENED is obsolete as of bugzilla 3.x and has
			# been removed from bugs.gentoo.org on 2011/05/01
			qparams['bug_status'] = ['NEW', 'ASSIGNED', 'REOPENED', 'UNCONFIRMED', 'CONFIRMED', 'IN_PROGRESS']
		elif [s.upper() for s in status] == ['ALL']:
			qparams['bug_status'] = config.choices['status']
		else:
			qparams['bug_status'] = [s.upper() for s in status]
		qparams['product'] = product or ''
		qparams['component'] = component or ''
		qparams['status_whiteboard'] = whiteboard or ''
		qparams['keywords'] = keywords or ''

		# hoops to jump through for emails, since there are
		# only two fields, we have to figure out what combinations
		# to use if all three are set.
		unique = list(set([assigned_to, cc, reporter, commenter]))
		unique = [u for u in unique if u]
		if len(unique) < 3:
			for i in range(len(unique)):
				e = unique[i]
				n = i + 1
				qparams['email%d' % n] = e
				qparams['emailassigned_to%d' % n] = int(e == assigned_to)
				qparams['emailreporter%d' % n] = int(e == reporter)
				qparams['emailcc%d' % n] = int(e == cc)
				qparams['emaillongdesc%d' % n] = int(e == commenter)
		else:
			raise AssertionError('Cannot set assigned_to, cc, and '
					'reporter in the same query')

		req_params = urlencode(qparams, True)
		req_url = urljoin(self.base, config.urls['list'])
		req_url += '?' + req_params
		req = Request(req_url, None, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)
		return self.extractResults(resp)

	def namedcmd(self, cmd):
		"""Run command stored in Bugzilla by name.

		@return: Result from the stored command.
		@rtype: list of dicts
		"""

		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['namedcmd'].copy()
		# Is there a better way of getting a command with a space in its name
		# to be encoded as foo%20bar instead of foo+bar or foo%2520bar?
		qparams['namedcmd'] = quote(cmd)
		req_params = urlencode(qparams, True)
		req_params = req_params.replace('%25','%')

		req_url = urljoin(self.base, config.urls['list'])
		req_url += '?' + req_params
		req = Request(req_url, None, config.headers)
		if self.user and self.password:
			base64string = base64.encodestring('%s:%s' % (self.user, self.password))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		return self.extractResults(resp)

	def get(self, bugid):
		"""Get an ElementTree representation of a bug.

		@param bugid: bug id
		@type  bugid: int

		@rtype: ElementTree
		"""
		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['show'].copy()
		qparams['id'] = bugid

		req_params = urlencode(qparams, True)
		req_url = urljoin(self.base,  config.urls['show'])
		req_url += '?' + req_params
		req = Request(req_url, None, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		data = resp.read()
		# Get rid of control characters.
		data = re.sub('[\x00-\x08\x0e-\x1f\x0b\x0c]', '', data)
		fd = StringIO(data)

		# workaround for ill-defined XML templates in bugzilla 2.20.2
		(major_version, minor_version) = \
		    (sys.version_info[0], sys.version_info[1])
		if major_version > 2 or \
			    (major_version == 2 and minor_version >= 7):
			# If this is 2.7 or greater, then XMLTreeBuilder
			# does what we want.
			parser = ElementTree.XMLParser()
		else:
			# Running under Python 2.6, so we need to use our
			# subclass of XMLTreeBuilder instead.
			parser = ForcedEncodingXMLTreeBuilder(encoding = 'utf-8')

		etree = ElementTree.parse(fd, parser)
		bug = etree.find('.//bug')
		if bug is not None and bug.attrib.has_key('error'):
			return None
		else:
			return etree

	def modify(self, bugid, title = None, comment = None, url = None,
			status = None, resolution = None,
			assigned_to = None, duplicate = 0,
			priority = None, severity = None,
			add_cc = [], remove_cc = [],
			add_dependson = [], remove_dependson = [],
			add_blocked = [], remove_blocked = [],
			whiteboard = None, keywords = None,
			component = None):
		"""Modify an existing bug

		@param bugid: bug id
		@type  bugid: int
		@keyword title: new title for bug
		@type    title: string
		@keyword comment: comment to add
		@type    comment: string
		@keyword url: new url
		@type    url: string
		@keyword status: new status (note, if you are changing it to RESOLVED, you need to set {resolution} as well.
		@type    status: string
		@keyword resolution: new resolution (if status=RESOLVED)
		@type    resolution: string
		@keyword assigned_to: email (needs to exist in bugzilla)
		@type    assigned_to: string
		@keyword duplicate: bug id to duplicate against (if resolution = DUPLICATE)
		@type    duplicate: int
		@keyword priority: new priority for bug
		@type    priority: string
		@keyword severity: new severity for bug
		@type    severity: string
		@keyword add_cc: list of emails to add to the cc list
		@type    add_cc: list of strings
		@keyword remove_cc: list of emails to remove from cc list
		@type    remove_cc: list of string.
		@keyword add_dependson: list of bug ids to add to the depend list
		@type    add_dependson: list of strings
		@keyword remove_dependson: list of bug ids to remove from depend list
		@type    remove_dependson: list of strings
		@keyword add_blocked: list of bug ids to add to the blocked list
		@type    add_blocked: list of strings
		@keyword remove_blocked: list of bug ids to remove from blocked list
		@type    remove_blocked: list of strings

		@keyword whiteboard: set status whiteboard
		@type    whiteboard: string
		@keyword keywords: set keywords
		@type    keywords: string
		@keyword component: set component
		@type    component: string

		@return: list of fields modified.
		@rtype: list of strings
		"""
		if not self.authenticated and not self.skip_auth:
			self.auth()


		buginfo = Bugz.get(self, bugid)
		if not buginfo:
			return False

		modified = []
		qparams = config.params['modify'].copy()
		qparams['id'] = bugid
		# NOTE: knob has been removed in bugzilla 4 and 3?
		qparams['knob'] = 'none'

		# copy existing fields
		FIELDS = ('bug_file_loc', 'bug_severity', 'short_desc', 'bug_status',
				'status_whiteboard', 'keywords', 'resolution',
				'op_sys', 'priority', 'version', 'target_milestone',
				'assigned_to', 'rep_platform', 'product', 'component', 'token')

		FIELDS_MULTI = ('blocked', 'dependson')

		for field in FIELDS:
			try:
				qparams[field] = buginfo.find('.//%s' % field).text
				if qparams[field] is None:
					del qparams[field]
			except:
				pass

		for field in FIELDS_MULTI:
			qparams[field] = [d.text for d in buginfo.findall('.//%s' % field)
					if d is not None and d.text is not None]

		# set 'knob' if we are change the status/resolution
		# or trying to reassign bug.
		if status:
			status = status.upper()
		if resolution:
			resolution = resolution.upper()

		if status and status != qparams['bug_status']:
			# Bugzilla >= 3.x
			qparams['bug_status'] = status

			if status == 'RESOLVED':
				qparams['knob'] = 'resolve'
				if resolution:
					qparams['resolution'] = resolution
				else:
					qparams['resolution'] = 'FIXED'

				modified.append(('status', status))
				modified.append(('resolution', qparams['resolution']))
			elif status == 'ASSIGNED' or status == 'IN_PROGRESS':
				qparams['knob'] = 'accept'
				modified.append(('status', status))
			elif status == 'REOPENED':
				qparams['knob'] = 'reopen'
				modified.append(('status', status))
			elif status == 'VERIFIED':
				qparams['knob'] = 'verified'
				modified.append(('status', status))
			elif status == 'CLOSED':
				qparams['knob'] = 'closed'
				modified.append(('status', status))
		elif duplicate:
			# Bugzilla >= 3.x
			qparams['bug_status'] = "RESOLVED"
			qparams['resolution'] = "DUPLICATE"

			qparams['knob'] = 'duplicate'
			qparams['dup_id'] = duplicate
			modified.append(('status', 'RESOLVED'))
			modified.append(('resolution', 'DUPLICATE'))
		elif assigned_to:
			qparams['knob'] = 'reassign'
			qparams['assigned_to'] = assigned_to
			modified.append(('assigned_to', assigned_to))

		# setup modification of other bits
		if comment:
			qparams['comment'] = comment
			modified.append(('comment', ellipsis(comment, 60)))
		if title:
			qparams['short_desc'] = title or ''
			modified.append(('title', title))
		if url is not None:
			qparams['bug_file_loc'] = url
			modified.append(('url', url))
		if severity is not None:
			qparams['bug_severity'] = severity
			modified.append(('severity', severity))
		if priority is not None:
			qparams['priority'] = priority
			modified.append(('priority', priority))

		# cc manipulation
		if add_cc is not None:
			qparams['newcc'] = ', '.join(add_cc)
			modified.append(('newcc', qparams['newcc']))
		if remove_cc is not None:
			qparams['cc'] = remove_cc
			qparams['removecc'] = 'on'
			modified.append(('cc', remove_cc))

		# bug depend/blocked manipulation
		changed_dependson = False
		changed_blocked = False
		if remove_dependson:
			for bug_id in remove_dependson:
				qparams['dependson'].remove(str(bug_id))
				changed_dependson = True
		if remove_blocked:
			for bug_id in remove_blocked:
				qparams['blocked'].remove(str(bug_id))
				changed_blocked = True
		if add_dependson:
			for bug_id in add_dependson:
				qparams['dependson'].append(str(bug_id))
				changed_dependson = True
		if add_blocked:
			for bug_id in add_blocked:
				qparams['blocked'].append(str(bug_id))
				changed_blocked = True

		qparams['dependson'] = ','.join(qparams['dependson'])
		qparams['blocked'] = ','.join(qparams['blocked'])
		if changed_dependson:
			modified.append(('dependson', qparams['dependson']))
		if changed_blocked:
			modified.append(('blocked', qparams['blocked']))

		if whiteboard is not None:
			qparams['status_whiteboard'] = whiteboard
			modified.append(('status_whiteboard', whiteboard))
		if keywords is not None:
			qparams['keywords'] = keywords
			modified.append(('keywords', keywords))
		if component is not None:
			qparams['component'] = component
			modified.append(('component', component))

		req_params = urlencode(qparams, True)
		req_url = urljoin(self.base, config.urls['modify'])
		req = Request(req_url, req_params, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)

		try:
			resp = self.opener.open(req)
			re_error = re.compile(r'id="error_msg".*>([^<]+)<')
			error = re_error.search(resp.read())
			if error:
				print error.group(1)
				return []
			return modified
		except:
			return []

	def attachment(self, attachid):
		"""Get an attachment by attachment_id

		@param attachid: attachment id
		@type  attachid: int

		@return: dict with three keys, 'filename', 'size', 'fd'
		@rtype: dict
		"""
		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['attach'].copy()
		qparams['id'] = attachid

		req_params = urlencode(qparams, True)
		req_url = urljoin(self.base, config.urls['attach'])
		req_url += '?' + req_params
		req = Request(req_url, None, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		try:
			content_type = resp.info()['Content-type']
			namefield = content_type.split(';')[1]
			filename = re.search(r'name=\"(.*)\"', namefield).group(1)
			content_length = int(resp.info()['Content-length'], 0)
			return {'filename': filename, 'size': content_length, 'fd': resp}
		except:
			return {}

	def post(self, product, component, title, description, url = '', assigned_to = '', cc = '', keywords = '', version = '', dependson = '', blocked = '', priority = '', severity = ''):
		"""Post a bug

		@param product: product where the bug should be placed
		@type product: string
		@param component: component where the bug should be placed
		@type component: string
		@param title: title of the bug.
		@type  title: string
		@param description: description of the bug
		@type  description: string
		@keyword url: optional url to submit with bug
		@type url: string
		@keyword assigned_to: optional email to assign bug to
		@type assigned_to: string.
		@keyword cc: option list of CC'd emails
		@type: string
		@keyword keywords: option list of bugzilla keywords
		@type: string
		@keyword version: version of the component
		@type: string
		@keyword dependson: bugs this one depends on
		@type: string
		@keyword blocked: bugs this one blocks
		@type: string
		@keyword priority: priority of this bug
		@type: string
		@keyword severity: severity of this bug
		@type: string

		@rtype: int
		@return: the bug number, or 0 if submission failed.
		"""
		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['post'].copy()
		qparams['product'] = product
		qparams['component'] = component
		qparams['short_desc'] = title
		qparams['comment'] = description
		qparams['assigned_to']  = assigned_to
		qparams['cc'] = cc
		qparams['bug_file_loc'] = url
		qparams['dependson'] = dependson
		qparams['blocked'] = blocked
		qparams['keywords'] = keywords

		#XXX: default version is 'unspecified'
		if version != '':
			qparams['version'] = version

		#XXX: default priority is 'Normal'
		if priority != '':
			qparams['priority'] = priority

		#XXX: default severity is 'normal'
		if severity != '':
			qparams['bug_severity'] = severity

		req_params = urlencode(qparams, True)
		req_url = urljoin(self.base, config.urls['post'])
		req = Request(req_url, req_params, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		try:
			re_bug = re.compile(r'(?:\s+)?<title>.*Bug ([0-9]+) Submitted.*</title>')
			bug_match = re_bug.search(resp.read())
			if bug_match:
				return int(bug_match.group(1))
		except:
			pass

		return 0

	def attach(self, bugid, title, description, filename,
			content_type = 'text/plain', ispatch = False):
		"""Attach a file to a bug.

		@param bugid: bug id
		@type  bugid: int
		@param title: short description of attachment
		@type  title: string
		@param description: long description of the attachment
		@type  description: string
		@param filename: filename of the attachment
		@type  filename: string
		@keywords content_type: mime-type of the attachment
		@type content_type: string

		@rtype: bool
		@return: True if successful, False if not successful.
		"""
		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['attach_post'].copy()
		qparams['bugid'] = bugid
		qparams['description'] = title
		qparams['comment'] = description
		if ispatch:
			qparams['ispatch'] = '1'
			qparams['contenttypeentry'] = 'text/plain'
		else:
			qparams['contenttypeentry'] = content_type

		filedata = [('data', filename, open(filename).read())]
		content_type, body = encode_multipart_formdata(qparams.items(),
				filedata)

		req_headers = config.headers.copy()
		req_headers['Content-type'] = content_type
		req_headers['Content-length'] = len(body)
		req_url = urljoin(self.base, config.urls['attach_post'])
		req = Request(req_url, body, req_headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		# TODO: return attachment id and success?
		try:
			re_attach = re.compile(r'<title>(.+)</title>')
			# Bugzilla 3/4
			re_attach34 = re.compile(r'Attachment \d+ added to Bug \d+')
			response = resp.read()
			attach_match = re_attach.search(response)
			if attach_match:
				if attach_match.group(1) == "Changes Submitted" or re_attach34.match(attach_match.group(1)):
					return True
				else:
					return attach_match.group(1)
			else:
				return False
		except:
			pass

		return False