示例#1
0
文件: mail.py 项目: sedrubal/bodhi
def get_template(update, use_template='fedora_errata_template'):
    """
    Build the update notice for a given update.

    Args:
        update (bodhi.server.models.Update): The update to generate a template about.
        use_template (basestring): The name of the variable in bodhi.server.mail that references the
            template to generate this notice with.
    Returns:
        list: A list of templates for the given update.
    """
    from bodhi.server.models import UpdateStatus, UpdateType
    use_template = read_template(use_template)
    line = six.text_type('-' * 80) + '\n'
    templates = []

    for build in update.builds:
        h = get_rpm_header(build.nvr)
        info = {}
        info['date'] = str(update.date_pushed)
        info['name'] = h['name']
        info['summary'] = h['summary']
        info['version'] = h['version']
        info['release'] = h['release']
        info['url'] = h['url']
        if update.status is UpdateStatus.testing:
            info['testing'] = ' Test'
            info['yum_repository'] = ' --enablerepo=updates-testing'
        else:
            info['testing'] = ''
            info['yum_repository'] = ''

        info['subject'] = u"%s%s%s Update: %s" % (
            update.type is UpdateType.security and '[SECURITY] '
            or '', update.release.long_name, info['testing'], build.nvr)
        info['updateid'] = update.alias
        info['description'] = h['description']
        info['product'] = update.release.long_name
        info['notes'] = ""
        if update.notes and len(update.notes):
            info['notes'] = u"Update Information:\n\n%s\n" % \
                '\n'.join(wrap(update.notes, width=80))
            info['notes'] += line

        # Add this updates referenced Bugzillas and CVEs
        i = 1
        info['references'] = ""
        if len(update.bugs) or len(update.cves):
            info['references'] = u"References:\n\n"
            parent = True in [bug.parent for bug in update.bugs]
            for bug in update.bugs:
                # Don't show any tracker bugs for security updates
                if update.type is UpdateType.security:
                    # If there is a parent bug, don't show trackers
                    if parent and not bug.parent:
                        log.debug("Skipping tracker bug %s" % bug)
                        continue
                title = (
                    bug.title != 'Unable to fetch title' and bug.title != 'Invalid bug number') \
                    and ' - %s' % bug.title or ''
                info['references'] += u"  [ %d ] Bug #%d%s\n        %s\n" % \
                                      (i, bug.bug_id, title, bug.url)
                i += 1
            for cve in update.cves:
                info['references'] += u"  [ %d ] %s\n        %s\n" % \
                                      (i, cve.cve_id, cve.url)
                i += 1
            info['references'] += line

        # Find the most recent update for this package, other than this one
        try:
            lastpkg = build.get_latest()
        except AttributeError:
            # Not all build types have the get_latest() method, such as ModuleBuilds.
            lastpkg = None

        # Grab the RPM header of the previous update, and generate a ChangeLog
        info['changelog'] = u""
        if lastpkg:
            oldh = get_rpm_header(lastpkg)
            oldtime = oldh['changelogtime']
            text = oldh['changelogtext']
            del oldh
            if not text:
                oldtime = 0
            elif len(text) != 1:
                oldtime = oldtime[0]
            info['changelog'] = u"ChangeLog:\n\n%s%s" % \
                (to_unicode(build.get_changelog(oldtime)), line)

        try:
            templates.append((info['subject'], use_template % info))
        except UnicodeDecodeError:
            # We can't trust the strings we get from RPM
            log.debug("UnicodeDecodeError! Will try again after decoding")
            for (key, value) in info.items():
                if value:
                    info[key] = to_unicode(value)
            templates.append((info['subject'], use_template % info))

    return templates
示例#2
0
def get_template(update: 'Update',
                 use_template: str = 'fedora_errata_template') -> list:
    """
    Build the update notice for a given update.

    Args:
        update: The update to generate a template about.
        use_template: The name of the variable in bodhi.server.mail that references the
            template to generate this notice with.
    Returns:
        A list of templates for the given update.
    """
    from bodhi.server.models import UpdateStatus, UpdateType
    use_template = read_template(use_template)
    line = str('-' * 80) + '\n'
    templates = []

    for build in update.builds:
        h = get_rpm_header(build.nvr)
        info = {}
        info['date'] = str(update.date_pushed)
        info['name'] = h['name']
        info['summary'] = h['summary']
        info['version'] = h['version']
        info['release'] = h['release']
        info['url'] = h['url']
        if update.status is UpdateStatus.testing:
            info['testing'] = ' Test'
            info['yum_repository'] = ' --enablerepo=updates-testing'
        else:
            info['testing'] = ''
            info['yum_repository'] = ''

        info['subject'] = "%s%s%s Update: %s" % (
            update.type is UpdateType.security and '[SECURITY] '
            or '', update.release.long_name, info['testing'], build.nvr)
        info['updateid'] = update.alias
        info['description'] = h['description']
        info['product'] = update.release.long_name
        info['notes'] = ""
        if update.notes and len(update.notes):
            info['notes'] = "Update Information:\n\n%s\n" % \
                '\n'.join(wrap(update.notes, width=80))
            info['notes'] += line

        # Add this update's referenced Bugzillas
        i = 1
        info['references'] = ""
        if update.bugs:
            info['references'] = "References:\n\n"
            parent = True in [bug.parent for bug in update.bugs]
            for bug in update.bugs:
                # Don't show any tracker bugs for security updates
                if update.type is UpdateType.security:
                    # If there is a parent bug, don't show trackers
                    if parent and not bug.parent:
                        log.debug("Skipping tracker bug %s" % bug)
                        continue
                title = (
                    bug.title != 'Unable to fetch title' and bug.title != 'Invalid bug number') \
                    and ' - %s' % bug.title or ''
                info['references'] += "  [ %d ] Bug #%d%s\n        %s\n" % \
                                      (i, bug.bug_id, title, bug.url)
                i += 1
            info['references'] += line

        # generate a ChangeLog
        info['changelog'] = ""
        changelog = build.get_changelog(lastupdate=True)
        if changelog is not None:
            info['changelog'] = "ChangeLog:\n\n%s%s" % \
                (changelog, line)

        templates.append((info['subject'], use_template % info))

    return templates
示例#3
0
 def test_rpm_header_exception(self):
     try:
         util.get_rpm_header('raise-exception')
         assert False
     except Exception:
         pass
示例#4
0
 def test_rpm_header_not_found(self):
     try:
         util.get_rpm_header("do-not-find-anything")
         assert False
     except ValueError:
         pass
示例#5
0
 def test_rpm_header(self):
     h = util.get_rpm_header('libseccomp')
     assert h['name'] == 'libseccomp', h