2017-03-03 02:47:22 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2008-09-20 07:32:11 +08:00
|
|
|
"""Methods for reporting bugs."""
|
|
|
|
|
|
|
|
import subprocess, sys, os
|
|
|
|
|
2008-09-20 09:43:16 +08:00
|
|
|
__all__ = ['ReportFailure', 'BugReport', 'getReporters']
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
class ReportFailure(Exception):
|
|
|
|
"""Generic exception for failures in bug reporting."""
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
2008-09-20 07:32:11 +08:00
|
|
|
|
|
|
|
# Collect information about a bug.
|
|
|
|
|
2018-12-03 20:12:48 +08:00
|
|
|
class BugReport(object):
|
2008-09-20 07:32:11 +08:00
|
|
|
def __init__(self, title, description, files):
|
|
|
|
self.title = title
|
|
|
|
self.description = description
|
|
|
|
self.files = files
|
|
|
|
|
|
|
|
# Reporter interfaces.
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import email, mimetypes, smtplib
|
|
|
|
from email import encoders
|
|
|
|
from email.message import Message
|
|
|
|
from email.mime.base import MIMEBase
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
2008-10-01 00:08:13 +08:00
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
# ReporterParameter
|
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
|
2018-12-03 20:12:48 +08:00
|
|
|
class ReporterParameter(object):
|
2008-10-01 00:08:13 +08:00
|
|
|
def __init__(self, n):
|
|
|
|
self.name = n
|
|
|
|
def getName(self):
|
|
|
|
return self.name
|
2008-10-01 00:37:50 +08:00
|
|
|
def getValue(self,r,bugtype,getConfigOption):
|
|
|
|
return getConfigOption(r.getName(),self.getName())
|
2008-10-01 00:08:13 +08:00
|
|
|
def saveConfigValue(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
class TextParameter (ReporterParameter):
|
|
|
|
def getHTML(self,r,bugtype,getConfigOption):
|
|
|
|
return """\
|
|
|
|
<tr>
|
2008-10-01 00:37:50 +08:00
|
|
|
<td class="form_clabel">%s:</td>
|
|
|
|
<td class="form_value"><input type="text" name="%s_%s" value="%s"></td>
|
2008-10-01 00:08:13 +08:00
|
|
|
</tr>"""%(self.getName(),r.getName(),self.getName(),self.getValue(r,bugtype,getConfigOption))
|
|
|
|
|
2008-10-01 00:37:50 +08:00
|
|
|
class SelectionParameter (ReporterParameter):
|
|
|
|
def __init__(self, n, values):
|
|
|
|
ReporterParameter.__init__(self,n)
|
|
|
|
self.values = values
|
|
|
|
|
|
|
|
def getHTML(self,r,bugtype,getConfigOption):
|
2008-10-01 01:12:32 +08:00
|
|
|
default = self.getValue(r,bugtype,getConfigOption)
|
2008-10-01 00:37:50 +08:00
|
|
|
return """\
|
|
|
|
<tr>
|
|
|
|
<td class="form_clabel">%s:</td><td class="form_value"><select name="%s_%s">
|
|
|
|
%s
|
|
|
|
</select></td>"""%(self.getName(),r.getName(),self.getName(),'\n'.join(["""\
|
2008-10-01 01:12:32 +08:00
|
|
|
<option value="%s"%s>%s</option>"""%(o[0],
|
|
|
|
o[0] == default and ' selected="selected"' or '',
|
|
|
|
o[1]) for o in self.values]))
|
2008-10-01 00:37:50 +08:00
|
|
|
|
2008-10-01 00:08:13 +08:00
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
# Reporters
|
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
|
2018-12-03 20:12:48 +08:00
|
|
|
class EmailReporter(object):
|
2008-09-20 07:32:11 +08:00
|
|
|
def getName(self):
|
|
|
|
return 'Email'
|
|
|
|
|
2008-10-01 00:08:13 +08:00
|
|
|
def getParameters(self):
|
2018-12-19 00:04:21 +08:00
|
|
|
return [TextParameter(x) for x in ['To', 'From', 'SMTP Server', 'SMTP Port']]
|
2008-09-20 07:32:11 +08:00
|
|
|
|
|
|
|
# Lifted from python email module examples.
|
|
|
|
def attachFile(self, outer, path):
|
|
|
|
# Guess the content type based on the file's extension. Encoding
|
|
|
|
# will be ignored, although we should check for simple things like
|
|
|
|
# gzip'd or compressed files.
|
|
|
|
ctype, encoding = mimetypes.guess_type(path)
|
|
|
|
if ctype is None or encoding is not None:
|
|
|
|
# No guess could be made, or the file is encoded (compressed), so
|
|
|
|
# use a generic bag-of-bits type.
|
|
|
|
ctype = 'application/octet-stream'
|
|
|
|
maintype, subtype = ctype.split('/', 1)
|
|
|
|
if maintype == 'text':
|
|
|
|
fp = open(path)
|
|
|
|
# Note: we should handle calculating the charset
|
|
|
|
msg = MIMEText(fp.read(), _subtype=subtype)
|
|
|
|
fp.close()
|
|
|
|
else:
|
|
|
|
fp = open(path, 'rb')
|
|
|
|
msg = MIMEBase(maintype, subtype)
|
|
|
|
msg.set_payload(fp.read())
|
|
|
|
fp.close()
|
|
|
|
# Encode the payload using Base64
|
|
|
|
encoders.encode_base64(msg)
|
|
|
|
# Set the filename parameter
|
|
|
|
msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
|
|
|
|
outer.attach(msg)
|
|
|
|
|
|
|
|
def fileReport(self, report, parameters):
|
|
|
|
mainMsg = """\
|
|
|
|
BUG REPORT
|
|
|
|
---
|
|
|
|
Title: %s
|
|
|
|
Description: %s
|
|
|
|
"""%(report.title, report.description)
|
|
|
|
|
2008-09-22 09:21:30 +08:00
|
|
|
if not parameters.get('To'):
|
|
|
|
raise ReportFailure('No "To" address specified.')
|
|
|
|
if not parameters.get('From'):
|
|
|
|
raise ReportFailure('No "From" address specified.')
|
2008-09-20 07:32:11 +08:00
|
|
|
|
|
|
|
msg = MIMEMultipart()
|
|
|
|
msg['Subject'] = 'BUG REPORT: %s'%(report.title)
|
|
|
|
# FIXME: Get config parameters
|
|
|
|
msg['To'] = parameters.get('To')
|
|
|
|
msg['From'] = parameters.get('From')
|
|
|
|
msg.preamble = mainMsg
|
|
|
|
|
|
|
|
msg.attach(MIMEText(mainMsg, _subtype='text/plain'))
|
|
|
|
for file in report.files:
|
|
|
|
self.attachFile(msg, file)
|
2008-09-22 09:21:30 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
s = smtplib.SMTP(host=parameters.get('SMTP Server'),
|
|
|
|
port=parameters.get('SMTP Port'))
|
|
|
|
s.sendmail(msg['From'], msg['To'], msg.as_string())
|
|
|
|
s.close()
|
|
|
|
except:
|
|
|
|
raise ReportFailure('Unable to send message via SMTP.')
|
|
|
|
|
|
|
|
return "Message sent!"
|
2008-09-20 07:32:11 +08:00
|
|
|
|
2018-12-03 20:12:48 +08:00
|
|
|
class BugzillaReporter(object):
|
2008-09-20 07:32:11 +08:00
|
|
|
def getName(self):
|
|
|
|
return 'Bugzilla'
|
|
|
|
|
2008-10-01 00:08:13 +08:00
|
|
|
def getParameters(self):
|
2018-12-19 00:04:21 +08:00
|
|
|
return [TextParameter(x) for x in ['URL','Product']]
|
2008-09-20 07:32:11 +08:00
|
|
|
|
|
|
|
def fileReport(self, report, parameters):
|
|
|
|
raise NotImplementedError
|
2008-10-01 01:00:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RadarClassificationParameter(SelectionParameter):
|
|
|
|
def __init__(self):
|
|
|
|
SelectionParameter.__init__(self,"Classification",
|
|
|
|
[['1', 'Security'], ['2', 'Crash/Hang/Data Loss'],
|
|
|
|
['3', 'Performance'], ['4', 'UI/Usability'],
|
|
|
|
['6', 'Serious Bug'], ['7', 'Other']])
|
|
|
|
|
|
|
|
def saveConfigValue(self):
|
|
|
|
return False
|
2008-10-01 01:12:32 +08:00
|
|
|
|
|
|
|
def getValue(self,r,bugtype,getConfigOption):
|
2008-10-25 05:23:51 +08:00
|
|
|
if bugtype.find("leak") != -1:
|
2008-10-01 01:12:32 +08:00
|
|
|
return '3'
|
2008-10-24 05:36:52 +08:00
|
|
|
elif bugtype.find("dereference") != -1:
|
|
|
|
return '2'
|
2008-10-31 01:29:54 +08:00
|
|
|
elif bugtype.find("missing ivar release") != -1:
|
|
|
|
return '3'
|
2008-10-01 01:12:32 +08:00
|
|
|
else:
|
|
|
|
return '7'
|
2008-10-01 01:00:30 +08:00
|
|
|
|
2018-12-03 20:12:48 +08:00
|
|
|
class RadarReporter(object):
|
2008-09-20 07:32:11 +08:00
|
|
|
@staticmethod
|
|
|
|
def isAvailable():
|
|
|
|
# FIXME: Find this .scpt better
|
2015-11-10 00:12:56 +08:00
|
|
|
path = os.path.join(os.path.dirname(__file__),'../share/scan-view/GetRadarVersion.scpt')
|
2008-09-30 13:45:59 +08:00
|
|
|
try:
|
|
|
|
p = subprocess.Popen(['osascript',path],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
except:
|
2008-09-20 07:32:11 +08:00
|
|
|
return False
|
|
|
|
data,err = p.communicate()
|
|
|
|
res = p.wait()
|
|
|
|
# FIXME: Check version? Check for no errors?
|
|
|
|
return res == 0
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return 'Radar'
|
|
|
|
|
2008-10-01 00:08:13 +08:00
|
|
|
def getParameters(self):
|
2008-10-01 00:37:50 +08:00
|
|
|
return [ TextParameter('Component'), TextParameter('Component Version'),
|
2008-10-01 01:00:30 +08:00
|
|
|
RadarClassificationParameter() ]
|
2008-09-20 07:32:11 +08:00
|
|
|
|
|
|
|
def fileReport(self, report, parameters):
|
|
|
|
component = parameters.get('Component', '')
|
|
|
|
componentVersion = parameters.get('Component Version', '')
|
2008-10-01 00:37:50 +08:00
|
|
|
classification = parameters.get('Classification', '')
|
2008-09-20 07:32:11 +08:00
|
|
|
personID = ""
|
|
|
|
diagnosis = ""
|
|
|
|
config = ""
|
|
|
|
|
|
|
|
if not component.strip():
|
|
|
|
component = 'Bugs found by clang Analyzer'
|
|
|
|
if not componentVersion.strip():
|
|
|
|
componentVersion = 'X'
|
|
|
|
|
2015-11-10 00:12:56 +08:00
|
|
|
script = os.path.join(os.path.dirname(__file__),'../share/scan-view/FileRadar.scpt')
|
2008-10-01 00:37:50 +08:00
|
|
|
args = ['osascript', script, component, componentVersion, classification, personID, report.title,
|
2018-12-19 00:04:21 +08:00
|
|
|
report.description, diagnosis, config] + [os.path.abspath(f) for f in report.files]
|
2008-09-20 07:32:11 +08:00
|
|
|
# print >>sys.stderr, args
|
2008-10-01 00:37:50 +08:00
|
|
|
try:
|
|
|
|
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
except:
|
2008-09-20 09:43:16 +08:00
|
|
|
raise ReportFailure("Unable to file radar (AppleScript failure).")
|
2008-09-20 07:32:11 +08:00
|
|
|
data, err = p.communicate()
|
|
|
|
res = p.wait()
|
|
|
|
|
|
|
|
if res:
|
2008-09-20 09:43:16 +08:00
|
|
|
raise ReportFailure("Unable to file radar (AppleScript failure).")
|
|
|
|
|
|
|
|
try:
|
|
|
|
values = eval(data)
|
|
|
|
except:
|
|
|
|
raise ReportFailure("Unable to process radar results.")
|
2008-09-20 07:32:11 +08:00
|
|
|
|
2008-09-20 09:43:16 +08:00
|
|
|
# We expect (int: bugID, str: message)
|
|
|
|
if len(values) != 2 or not isinstance(values[0], int):
|
|
|
|
raise ReportFailure("Unable to process radar results.")
|
|
|
|
|
|
|
|
bugID,message = values
|
|
|
|
bugID = int(bugID)
|
|
|
|
|
|
|
|
if not bugID:
|
|
|
|
raise ReportFailure(message)
|
|
|
|
|
|
|
|
return "Filed: <a href=\"rdar://%d/\">%d</a>"%(bugID,bugID)
|
2008-09-20 07:32:11 +08:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
def getReporters():
|
|
|
|
reporters = []
|
|
|
|
if RadarReporter.isAvailable():
|
|
|
|
reporters.append(RadarReporter())
|
2008-09-22 03:06:51 +08:00
|
|
|
reporters.append(EmailReporter())
|
2008-09-20 07:32:11 +08:00
|
|
|
return reporters
|
|
|
|
|