Пример #1
0
    def render(self, req, mimetype, content, filename=None, rev=None):
        from trac.web.clearsilver import HDFWrapper

        content = content_to_unicode(self.env, content, mimetype)
        d = self._diff_to_hdf(content.splitlines(),
                              Mimeview(self.env).tab_width)
        if not d:
            raise TracError, 'Invalid unified diff content'
        hdf = HDFWrapper(loadpaths=[self.env.get_templates_dir(),
                                    self.config.get('trac', 'templates_dir')])
        hdf['diff.files'] = d

        add_stylesheet(req, 'common/css/diff.css')
        return hdf.render(hdf.parse(self.diff_cs))
Пример #2
0
    def render(self, req, mimetype, content, filename=None, rev=None):
        from trac.web.clearsilver import HDFWrapper

        content = content_to_unicode(self.env, content, mimetype)
        d = self._diff_to_hdf(content.splitlines(),
                              Mimeview(self.env).tab_width)
        if not d:
            raise TracError, u'Contenu diff unifié invalide'
        hdf = HDFWrapper(loadpaths=[self.env.get_templates_dir(),
                                    self.config.get('trac', 'templates_dir')])
        hdf['diff.files'] = d

        add_stylesheet(req, 'common/css/diff.css')
        return hdf.render(hdf.parse(self.diff_cs))
Пример #3
0
    def render(self, req, mimetype, content, filename=None, rev=None):
        from trac.web.clearsilver import HDFWrapper

        tabwidth = int(self.config.get('diff', 'tab_width'))
        d = self._diff_to_hdf(content.splitlines(), tabwidth)
        if not d:
            raise TracError, 'Invalid unified diff content'
        hdf = HDFWrapper(loadpaths=[
            self.env.get_templates_dir(),
            self.config.get('trac', 'templates_dir')
        ])
        hdf['diff.files'] = d

        add_stylesheet(req, 'common/css/diff.css')
        return hdf.render(hdf.parse(self.diff_cs))
Пример #4
0
    def render_summary(self, req, config, build, step, category):
        assert category == 'lint'
        #assert category in ('lint', 'pylint', 'nblint')

        db = self.env.get_db_cnx()
        cursor = db.cursor()
        cursor.execute("""
SELECT MAX(lint_file.value) AS file, MAX(CAST(lint_line.value as INT)) AS line,
       MAX(lint_type.value) AS type, MAX(lint_tag.value) AS tag,
       MAX(lint_msg.value) AS msg, MAX(lint_category.value) as cat
FROM bitten_report AS report
  LEFT OUTER JOIN bitten_report_item AS lint_file
    ON(lint_file.report=report.id AND lint_file.name='file')
  LEFT OUTER JOIN bitten_report_item AS lint_line
    ON(lint_line.report=report.id AND
       lint_line.item=lint_file.item AND lint_line.name='line')
  LEFT OUTER JOIN bitten_report_item AS lint_type
    ON(lint_type.report=report.id AND
       lint_type.item=lint_file.item AND lint_type.name='type')
  LEFT OUTER JOIN bitten_report_item AS lint_tag
    ON(lint_tag.report=report.id AND
       lint_tag.item=lint_file.item AND lint_tag.name='tag')
  LEFT OUTER JOIN bitten_report_item AS lint_msg
    ON(lint_msg.report=report.id AND
       lint_msg.item=lint_file.item AND lint_msg.name='msg')
  LEFT OUTER JOIN bitten_report_item AS lint_category
    ON(lint_category.report=report.id AND
       lint_category.item=lint_file.item AND lint_category.name='category')
WHERE category=%s AND build=%s AND step=%s
GROUP BY lint_file.value, lint_line.value, lint_type.value
ORDER BY file, line, type""", (category, build.id, step.name))

        data = []
        for fname, line, ltype, tag, msg, cat in cursor:
            data.append(
                {
                    'href': self.env.href.browser(config.path, fname),
                    'file': fname,
                    'line': line,
                     'type': ltype,
                     'tag': tag,
                     'msg': msg,
                     'cat': cat
                }
            )
        hdf = HDFWrapper(loadpaths=Chrome(self.env).get_all_templates_dirs())
        hdf['data'] = data
        return hdf.render('nosebitten_pylint_summary.cs')
Пример #5
0
    def render_summary(self, req, config, build, step, category):
        self.log.debug('Category: %s', category)
        assert category == 'coverage'

        db = self.env.get_db_cnx()
        cursor = db.cursor()
        cursor.execute("""
SELECT item_name.value AS unit, item_file.value AS file,
       MAX(item_lines.value) AS loc, MAX(exec_lines.value) AS exec,
       MAX(item_percentage.value) AS cov, MAX(miss_lines.value) AS miss
FROM bitten_report AS report
 LEFT OUTER JOIN bitten_report_item AS item_name
  ON (item_name.report=report.id AND item_name.name='name')
 LEFT OUTER JOIN bitten_report_item AS item_file
  ON (item_file.report=report.id AND item_file.item=item_name.item AND
      item_file.name='file')
 LEFT OUTER JOIN bitten_report_item AS item_lines
  ON (item_lines.report=report.id AND item_lines.item=item_name.item AND
      item_lines.name='lines')
 LEFT OUTER JOIN bitten_report_item AS exec_lines
  ON (exec_lines.report=report.id AND
      exec_lines.item=item_name.item AND
      exec_lines.name='executed')
 LEFT OUTER JOIN bitten_report_item AS item_percentage
  ON (item_percentage.report=report.id AND
      item_percentage.item=item_name.item AND
      item_percentage.name='percentage')
 LEFT OUTER JOIN bitten_report_item AS miss_lines
  ON (miss_lines.report=report.id AND
      miss_lines.item=item_name.item AND
      miss_lines.name='miss')
WHERE category='coverage' AND build=%s AND step=%s
GROUP BY file, unit ORDER BY unit""", (build.id, step.name))

        data = []
        total_loc, total_cov, total_exe = 0, 0, 0
        for unit, file, loc, exe, cov, miss in cursor:
            loc, cov, exe = int(loc), float(cov), int(exe)
            if loc:
                d = {'name': unit, 'loc': loc, 'cov': int(cov), 'exe': exe} #, 'miss': miss}
                if file:
                    d['href'] = self.env.href.browser(config.path, file)
                if miss and file:
                    missed = []
                    for lines in miss.split():
                        m = {'lines': lines}
                        m['href'] = self.env.href.browser(config.path, file) + '#L%s' % \
                                lines.split('-')[0]
                        missed.append(m)
                    d['miss'] = missed
                else:
                    d['miss'] = {'lines': miss}

                self.log.debug('DATA ROW:', d)

                data.append(d)
                total_loc += loc
                total_exe += exe
                total_cov += loc * cov

        hdf = HDFWrapper(loadpaths=Chrome(self.env).get_all_templates_dirs())
        hdf['data'] = data
        hdf['totals'] = {'loc': total_loc, 'cov': int(total_cov / total_loc), 'exe': total_exe}

        return hdf.render('bittentrac_nosecoverage.cs')
Пример #6
0
    def render_summary(self, req, config, build, step, category):
        assert category == 'pylint'

        db = self.env.get_db_cnx()
        cursor = db.cursor()
        cursor.execute("""
SELECT MAX(lint_file.value) AS file, MAX(CAST(lint_line.value as INT)) AS line,
       MAX(lint_type.value) AS type, MAX(lint_tag.value) AS tag,
       MAX(lint_msg.value) AS msg, MAX(lint_category.value) as cat
FROM bitten_report AS report
  LEFT OUTER JOIN bitten_report_item AS lint_file
    ON(lint_file.report=report.id AND lint_file.name='file')
  LEFT OUTER JOIN bitten_report_item AS lint_line
    ON(lint_line.report=report.id AND
       lint_line.item=lint_file.item AND lint_line.name='line')
  LEFT OUTER JOIN bitten_report_item AS lint_type
    ON(lint_type.report=report.id AND
       lint_type.item=lint_file.item AND lint_type.name='type')
  LEFT OUTER JOIN bitten_report_item AS lint_tag
    ON(lint_tag.report=report.id AND
       lint_tag.item=lint_file.item AND lint_tag.name='tag')
  LEFT OUTER JOIN bitten_report_item AS lint_msg
    ON(lint_msg.report=report.id AND
       lint_msg.item=lint_file.item AND lint_msg.name='msg')
  LEFT OUTER JOIN bitten_report_item AS lint_category
    ON(lint_category.report=report.id AND
       lint_category.item=lint_file.item AND lint_category.name='category')
WHERE category=%s AND build=%s AND step=%s
GROUP BY lint_file.value, lint_line.value, lint_type.value
ORDER BY file, line, type""", (category, build.id, step.name))

        data = []
        totals = {
            'error': 0,
            'warning': 0,
            'refactor': 0,
            'fatal': 0,
            'convention': 0,
            'ignored': 0

        }
        for fname, line, ltype, tag, msg, cat in cursor:
            #self.log.debug("%s, %s, %s, %s, %s, %s", fname, line, ltype, tag, msg, cat)
            #self.log.debug("CAT: %r", cat)
            if cat in ('error', 'warning', 'refactor', 'fatal', 'convention', 'ignored'):
                data.append(
                    {
                        'href': self.env.href.browser(config.path, fname),
                        'file': fname,
                        'line': line,
                        'type': ltype,
                        'tag': tag,
                        'msg': msg,
                        'cat': cat
                    }
                )
                totals[cat] += 1
        hdf = HDFWrapper(loadpaths=Chrome(self.env).get_all_templates_dirs())
        hdf['data'] = data
        hdf['totals'] = totals
        return hdf.render('bittentrac_pylint.cs')
Пример #7
0
class CodeReviewNotifyEmail(NotifyEmail):
    template_name = "code_review_notify_email.cs"
    from_email = "codereview@localhost"
    ticket_pattern = re.compile('#([0-9]+)')

    def __init__(self, env):
        NotifyEmail.__init__(self, env)
        self.hdf = HDFWrapper(loadpaths=self.get_templates_dirs())
        populate_hdf(self.hdf, env)

    def get_templates_dirs(self):
        from pkg_resources import resource_filename
        return [resource_filename(__name__, 'templates')]

    def notify(self, cr_dict):
        self.cr_dict = cr_dict
        cr = CodeReview(self.env, cr_dict['cr_id'])
        
        cursor = self.env.get_db_cnx().cursor()
        cursor.execute("SELECT author, message FROM revision WHERE rev='%s'" % cr_dict['cr_id'])

        recordset = cursor.fetchall()

        if not recordset:
            return
        cs_author = recordset[0][0]
        cs_message = recordset[0][1]

        if cs_author == "anonymous" and cr_dict['cr_author'] == "anonymous":
            return

        subject = "[TracNotify] ChangeSet r%s by %s reviewed by %s" % \
                  (cr_dict['cr_id'], cs_author, ','.join(cr.get_reviewers()))
        if cr_dict['priority'] == 'critical':
            subject = '[Critical]' + subject

        ticket_info = self.get_ticket_info(cs_message, cr_dict['cr_message'])

        self.hdf['trac_name'] = self.env.config.get('project', 'name')
        absurl = self.env.config.get('codereview', 'absurl')
        self.hdf['absurl'] = absurl
        self.hdf['r_content'] = wiki_to_html(cr_dict['cr_message'], self.env, cr_dict['req'], absurls = absurl)
        self.hdf['ticket_len'] = len(ticket_info)
        self.hdf['t_info'] = ticket_info
        self.hdf['cs_id'] = cr_dict['cr_id']
        self.hdf['cs_author'] = cs_author
        self.hdf['cs_message'] = wiki_to_html(cs_message, self.env, cr_dict['req'], absurls = absurl, escape_newlines=True)
        self.hdf['r_author'] = ', '.join(cr.get_reviewers())
        self.hdf['r_priority'] = cr_dict['priority']

        self.subject = subject

        self.smtp_server = self.config['notification'].get('smtp_server')
        self.smtp_port = self.config['notification'].getint('smtp_port')
        self.from_email = self.config['notification'].get('smtp_from')
        self.replyto_email = self.config['notification'].get('smtp_replyto')
        self.from_email = self.from_email or self.replyto_email
        if not self.from_email and not self.replyto_email:
            raise TracError(Markup('Unable to send email due to identity '
                                   'crisis.<p>Neither <b>notification.from</b> '
                                   'nor <b>notification.reply_to</b> are '
                                   'specified in the configuration.</p>'),
                            'SMTP Notification Error')

        # Authentication info (optional)
        self.user_name = self.config['notification'].get('smtp_user')
        self.password = self.config['notification'].get('smtp_password')

        Notify.notify(self, cr_dict['cr_id'])

    def get_reviewer_team(self):
        reviewer_team = self.env.config.get("codereview", "team_list", "")
        if reviewer_team:
            return [reviewer.strip() for reviewer in reviewer_team.split(',')]
        else:
            return []

    def get_recipients(self, cr_id):
        cr = CodeReview(self.env, cr_id)
        reviewers = list(set(self.get_reviewer_team() + cr.get_reviewers()))

	cs_author = self.hdf.get('cs_author', None)
	if cs_author is None:
            cursor = self.env.get_db_cnx().cursor()
            cursor.execute("SELECT author FROM revision WHERE rev='%s'" % cr_id)
            cs_author = cursor.fetchone()
            if cs_author:
                cs_author = cs_author[0]
            else:
	        cs_author = ''

        if cs_author in reviewers:
            reviewers.remove(cs_author)
        return ([cs_author,], reviewers)

    def get_ticket_info(self, cs_message, cr_message):
        cursor = self.env.get_db_cnx().cursor()
        ticket_ids = set(self.ticket_pattern.findall(cs_message or '') + self.ticket_pattern.findall(cr_message or ''))
        ticket_info = []
        if(ticket_ids):
            cursor.execute('SELECT id, summary FROM ticket WHERE id in (%s) ORDER BY id' % ','.join(ticket_ids))
            for id, summary in cursor.fetchall():
                ticket_info.append({'id': id, 'summary': summary})
        return ticket_info

    def send(self, torcpts, ccrcpts):
        from email.MIMEText import MIMEText
        from email.Utils import formatdate, formataddr
        body = self.hdf.render(self.template_name)
        projname = self.config.get('project', 'name')
        headers = {}
        headers['X-Mailer'] = 'Trac %s, by Edgewall Software' % __version__
        headers['X-Trac-Version'] =  __version__
        headers['X-Trac-Project'] =  projname
        headers['X-URL'] = self.config.get('project', 'url')
        headers['Subject'] = self.subject
        headers['From'] = (projname, self.from_email)
        headers['Sender'] = self.from_email
        headers['Reply-To'] = self.replyto_email

        def build_addresses(rcpts):
            """Format and remove invalid addresses"""
            return filter(lambda x: x, \
                          [self.get_smtp_address(addr) for addr in rcpts])
        def remove_dup(rcpts, all):
            """Remove duplicates"""
            tmp = []
            for rcpt in rcpts:
                if not rcpt in all:
                    tmp.append(rcpt)
                    all.append(rcpt)
            return (tmp, all)

        toaddrs = build_addresses(torcpts)
        ccaddrs = build_addresses(ccrcpts)

        recipients = []
        (toaddrs, recipients) = remove_dup(toaddrs, recipients)
        (ccaddrs, recipients) = remove_dup(ccaddrs, recipients)
        
        if len(recipients) < 1:
            self.env.log.info('no recipient for a ticket notification')
            return

        headers['To'] = ', '.join(toaddrs)
        headers['Cc'] = ', '.join(ccaddrs)
        headers['Date'] = formatdate()
        if not self._charset.body_encoding:
            try:
                dummy = body.encode('ascii')
            except UnicodeDecodeError:
                raise TracError, "CodeReview contains non-Ascii chars. " \
                                 "Please change encoding setting"
        msg = MIMEText(body)
        msg.set_type("text/html")

        del msg['Content-Transfer-Encoding']
        msg.set_charset(self._charset)
        self.add_headers(msg, headers)
        self.env.log.debug("Sending SMTP notification to %s on port %d to %s"
                           % (self.smtp_server, self.smtp_port, recipients))
        msgtext = msg.as_string()
        # Ensure the message complies with RFC2822: use CRLF line endings
        recrlf = re.compile("\r?\n")
        msgtext = "\r\n".join(recrlf.split(msgtext))
        self.server.sendmail(msg['From'], recipients, msgtext)
Пример #8
0
class CodeReviewNotifyEmail(NotifyEmail):
    template_name = "code_review_notify_email.cs"
    from_email = "codereview@localhost"
    ticket_pattern = re.compile('#([0-9]+)')

    def __init__(self, env):
        NotifyEmail.__init__(self, env)
        self.hdf = HDFWrapper(loadpaths=self.get_templates_dirs())
        populate_hdf(self.hdf, env)

    def get_templates_dirs(self):
        from pkg_resources import resource_filename
        return [resource_filename(__name__, 'templates')]

    def notify(self, cr_dict):
        self.cr_dict = cr_dict
        cr = CodeReview(self.env, cr_dict['cr_id'])

        cursor = self.env.get_db_cnx().cursor()
        cursor.execute("SELECT author, message FROM revision WHERE rev='%s'" %
                       cr_dict['cr_id'])

        recordset = cursor.fetchall()

        if not recordset:
            return
        cs_author = recordset[0][0]
        cs_message = recordset[0][1]

        if cs_author == "anonymous" and cr_dict['cr_author'] == "anonymous":
            return

        subject = "[TracNotify] ChangeSet r%s by %s reviewed by %s" % \
                  (cr_dict['cr_id'], cs_author, ','.join(cr.get_reviewers()))
        if cr_dict['priority'] == 'critical':
            subject = '[Critical]' + subject

        ticket_info = self.get_ticket_info(cs_message, cr_dict['cr_message'])

        self.hdf['trac_name'] = self.env.config.get('project', 'name')
        absurl = self.env.config.get('codereview', 'absurl')
        self.hdf['absurl'] = absurl
        self.hdf['r_content'] = wiki_to_html(cr_dict['cr_message'],
                                             self.env,
                                             cr_dict['req'],
                                             absurls=absurl)
        self.hdf['ticket_len'] = len(ticket_info)
        self.hdf['t_info'] = ticket_info
        self.hdf['cs_id'] = cr_dict['cr_id']
        self.hdf['cs_author'] = cs_author
        self.hdf['cs_message'] = wiki_to_html(cs_message,
                                              self.env,
                                              cr_dict['req'],
                                              absurls=absurl,
                                              escape_newlines=True)
        self.hdf['r_author'] = ', '.join(cr.get_reviewers())
        self.hdf['r_priority'] = cr_dict['priority']

        self.subject = subject

        self.smtp_server = self.config['notification'].get('smtp_server')
        self.smtp_port = self.config['notification'].getint('smtp_port')
        self.from_email = self.config['notification'].get('smtp_from')
        self.replyto_email = self.config['notification'].get('smtp_replyto')
        self.from_email = self.from_email or self.replyto_email
        if not self.from_email and not self.replyto_email:
            raise TracError(
                Markup('Unable to send email due to identity '
                       'crisis.<p>Neither <b>notification.from</b> '
                       'nor <b>notification.reply_to</b> are '
                       'specified in the configuration.</p>'),
                'SMTP Notification Error')

        # Authentication info (optional)
        self.user_name = self.config['notification'].get('smtp_user')
        self.password = self.config['notification'].get('smtp_password')

        Notify.notify(self, cr_dict['cr_id'])

    def get_reviewer_team(self):
        reviewer_team = self.env.config.get("codereview", "team_list", "")
        if reviewer_team:
            return [reviewer.strip() for reviewer in reviewer_team.split(',')]
        else:
            return []

    def get_recipients(self, cr_id):
        cr = CodeReview(self.env, cr_id)
        reviewers = list(set(self.get_reviewer_team() + cr.get_reviewers()))

        cs_author = self.hdf.get('cs_author', None)
        if cs_author is None:
            cursor = self.env.get_db_cnx().cursor()
            cursor.execute("SELECT author FROM revision WHERE rev='%s'" %
                           cr_id)
            cs_author = cursor.fetchone()
            if cs_author:
                cs_author = cs_author[0]
            else:
                cs_author = ''

        if cs_author in reviewers:
            reviewers.remove(cs_author)
        return ([
            cs_author,
        ], reviewers)

    def get_ticket_info(self, cs_message, cr_message):
        cursor = self.env.get_db_cnx().cursor()
        ticket_ids = set(
            self.ticket_pattern.findall(cs_message or '') +
            self.ticket_pattern.findall(cr_message or ''))
        ticket_info = []
        if (ticket_ids):
            cursor.execute(
                'SELECT id, summary FROM ticket WHERE id in (%s) ORDER BY id' %
                ','.join(ticket_ids))
            for id, summary in cursor.fetchall():
                ticket_info.append({'id': id, 'summary': summary})
        return ticket_info

    def send(self, torcpts, ccrcpts):
        from email.MIMEText import MIMEText
        from email.Utils import formatdate, formataddr
        body = self.hdf.render(self.template_name)
        projname = self.config.get('project', 'name')
        headers = {}
        headers['X-Mailer'] = 'Trac %s, by Edgewall Software' % __version__
        headers['X-Trac-Version'] = __version__
        headers['X-Trac-Project'] = projname
        headers['X-URL'] = self.config.get('project', 'url')
        headers['Subject'] = self.subject
        headers['From'] = (projname, self.from_email)
        headers['Sender'] = self.from_email
        headers['Reply-To'] = self.replyto_email

        def build_addresses(rcpts):
            """Format and remove invalid addresses"""
            return filter(lambda x: x, \
                          [self.get_smtp_address(addr) for addr in rcpts])

        def remove_dup(rcpts, all):
            """Remove duplicates"""
            tmp = []
            for rcpt in rcpts:
                if not rcpt in all:
                    tmp.append(rcpt)
                    all.append(rcpt)
            return (tmp, all)

        toaddrs = build_addresses(torcpts)
        ccaddrs = build_addresses(ccrcpts)

        recipients = []
        (toaddrs, recipients) = remove_dup(toaddrs, recipients)
        (ccaddrs, recipients) = remove_dup(ccaddrs, recipients)

        if len(recipients) < 1:
            self.env.log.info('no recipient for a ticket notification')
            return

        headers['To'] = ', '.join(toaddrs)
        headers['Cc'] = ', '.join(ccaddrs)
        headers['Date'] = formatdate()
        if not self._charset.body_encoding:
            try:
                dummy = body.encode('ascii')
            except UnicodeDecodeError:
                raise TracError, "CodeReview contains non-Ascii chars. " \
                                 "Please change encoding setting"
        msg = MIMEText(body)
        msg.set_type("text/html")

        del msg['Content-Transfer-Encoding']
        msg.set_charset(self._charset)
        self.add_headers(msg, headers)
        self.env.log.debug("Sending SMTP notification to %s on port %d to %s" %
                           (self.smtp_server, self.smtp_port, recipients))
        msgtext = msg.as_string()
        # Ensure the message complies with RFC2822: use CRLF line endings
        recrlf = re.compile("\r?\n")
        msgtext = "\r\n".join(recrlf.split(msgtext))
        self.server.sendmail(msg['From'], recipients, msgtext)