From: Greg Brockman Date: Tue, 24 Aug 2010 04:05:18 +0000 (-0400) Subject: Further generalize the implementation of post-build X-Git-Tag: 0.1.8~12 X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-dev.git/commitdiff_plain/c97927fdb3ad5e3554d474c0b07a7ffd0651c008 Further generalize the implementation of post-build This is done in preparation for adding support for more notification types. --- diff --git a/build-hooks/post-build b/build-hooks/post-build index cd199db..10ac757 100755 --- a/build-hooks/post-build +++ b/build-hooks/post-build @@ -53,13 +53,14 @@ from email.mime import text from invirt import common, database, builder from invirt.config import structs as config -def make_msg(build, values, verbose=True, success=lambda x: x, failure=lambda x: x): +def build_completion_msg(succeeded, values, verbose=True, success=lambda x: x, failure=lambda x: x): + """Format a message reporting the results of a build""" values = dict(values) if not verbose and values['traceback'] is not None: # TODO: better heuristic values['traceback'] = textwrap.fill('\n'.join(values['traceback'].split('\n')[-2:])) - if build.succeeded: + if succeeded: values['result'] = success(values['result']) msg = """Build of %(package)s %(version)s in %(pocket)s %(result)s. @@ -75,6 +76,22 @@ Branch %(pocket)s has been advanced to %(short_commit)s. (Build %(build_id)s was submitted by %(principal)s at %(inserted_at)s.)""" % values return msg +# Names of hooks +POST_BUILD = 'post-build' +FAILED_BUILD = 'failed-build' + +# Types of communication + +ZEPHYR = 'zephyr' +MAIL = 'mail' + +message_generators = { + ZEPHYR : { POST_BUILD : build_completion_msg, + FAILED_BUILD : build_completion_msg }, + MAIL : { POST_BUILD : build_completion_msg, + FAILED_BUILD : build_completion_msg } + } + def zephyr_escape(m): m = re.sub('@', '@@', m) m = re.sub('}', '@(})', m) @@ -87,52 +104,63 @@ def zephyr_failure(m): return '}@{@color(red)%s}@{' % zephyr_escape(m) def main(): - parser = optparse.OptionParser('Usage: %prog build_id') + parser = optparse.OptionParser('Usage: %prog [options] [arguments]') opts, args = parser.parse_args() - if len(args) != 1: - parser.print_help() - return 1 prog = os.path.basename(sys.argv[0]) - database.connect() - build = database.Build.query().get(args[0]) - short_commit = builder.canonicalize_commit(build.package, build.commit, shorten=True) - values = { 'build_id' : build.build_id, - 'commit' : build.commit, - 'failed_stage' : build.failed_stage, - 'inserted_at' : build.inserted_at, - 'package' : build.package, - 'pocket' : build.pocket, - 'principal' : build.principal, - 'short_commit' : short_commit, - 'traceback' : build.traceback, - 'version' : build.version } - if build.succeeded: - values['result'] = 'succeeded' - else: - values['result'] = 'failed' - try: - if prog == 'post-build': + if prog == POST_BUILD: hook_config = config.build.hooks.post_build - elif prog == 'failed-build': + elif prog == FAILED_BUILD: hook_config = config.build.hooks.failed_build else: - print >>sys.stderr, '{post,failed}-build invoke with unrecognized name %s' % prog + parser.error('hook script invoked with unrecognized name %s' % prog) return 2 except common.InvirtConfigError: print >>sys.stderr, 'No hook configuration found for %s.' % prog return 1 + if prog in [POST_BUILD, FAILED_BUILD]: + if len(args) != 1: + parser.set_usage('Usage: %prog [options] build_id') + parser.print_help() + return 1 + database.connect() + build = database.Build.query().get(args[0]) + short_commit = builder.canonicalize_commit(build.package, build.commit, shorten=True) + values = { 'build_id' : build.build_id, + 'commit' : build.commit, + 'failed_stage' : build.failed_stage, + 'inserted_at' : build.inserted_at, + 'package' : build.package, + 'pocket' : build.pocket, + 'principal' : build.principal, + 'short_commit' : short_commit, + 'traceback' : build.traceback, + 'version' : build.version, + 'default_instance' : 'build_%(build_id)s', + 'default_subject' : 'XVM build %(result)s: %(package)s %(version)s in %(pocket)s'} + if build.succeeded: + assert prog == POST_BUILD + values['result'] = 'succeeded' + succeeded = True + else: + assert prog == FAILED_BUILD + values['result'] = 'failed' + succeeded = False + else: + raise AssertionError('Impossible state') + try: zephyr_config = hook_config.zephyr klass = zephyr_config['class'] % values except common.InvirtConfigError: print >>sys.stderr, 'No zephyr configuration specified for %s.' % prog else: - msg = '@{%s}' % make_msg(build, values, verbose=False, + make_msg = message_generators[ZEPHYR][prog] + msg = '@{%s}' % make_msg(succeeded, values, verbose=False, success=zephyr_success, failure=zephyr_failure) - instance = zephyr_config.get('instance', 'build_%(build_id)s') % values + instance = zephyr_config.get('instance', values['default_instance']) % values zsig = zephyr_config.get('zsig', 'XVM Buildbot') % values common.captureOutput(['zwrite', '-c', klass, '-i', instance, '-s', zsig, '-d', '-m', msg], @@ -145,11 +173,12 @@ def main(): except common.InvirtConfigError: print >>sys.stderr, 'No email configuration specified for %s.' % prog else: - msg = make_msg(build, values) + make_msg = message_generators[MAIL][prog] + msg = make_msg(succeeded, values) email = text.MIMEText(msg) email['To'] = to % values email['From'] = sender % values - email['Subject'] = mail_config.get('subject', 'XVM build %(result)s: %(package)s %(version)s in %(pocket)s') % values + email['Subject'] = mail_config.get('subject', values['default_subject']) % values common.captureOutput(['sendmail', '-t'], email.as_string(), stdout=None, stderr=None)