3 A script for reporting the results of an invirtibuild. Supports any
4 combination of the following reporting mechanisms.
6 Note that all strings are interpolated with a dictionary containing
9 build_id, commit, failed_stage, inserted_at, package,
10 pocket, principal, result, short_commit, traceback, version
14 To configure zephyr, add something like the following to your invirt config:
19 zephyr: &post_build_zephyr
20 class: myclass [required]
21 instance: myinstance [optional]
22 zsig: myzsig [optional]
24 zephyr: *post_build_zephyr
29 To configure email notifications, add something like the following to your invirt config:
34 mail: &post_build_mail
35 to: myemail@example.com [required]
36 from: myemail@example.com [required]
37 subject: My Subject [optional]
39 mail: *post_build_mail
42 The script chooses which configuration option to use based off the
43 name it is called with. This name also determines which command-line
44 arguments the script takes, as well as how they are formatted. When
47 post-build: uses post_build option
48 failed-build: uses failed_build option
49 post-submit: uses post_submit option
50 failed-submit: uses failed_submit option
51 post-add-repo: uses post_add_repo option
60 from email.mime import text
62 from invirt import common, database, builder
63 from invirt.config import structs as config
65 def build_completion_msg(succeeded, values, verbose=True, success=lambda x: x, failure=lambda x: x):
66 """Format a message reporting the results of a build"""
68 if not verbose and values['traceback'] is not None:
69 split = values['traceback'].split('\n')
70 # Here, have a hackish heuristic
72 for i in xrange(2, len(split)):
73 truncated = textwrap.fill('\n'.join(split[-i:]))
74 if len(truncated) >= 10:
76 values['traceback'] = truncated
79 values['result'] = success(values['result'])
80 msg = """Build of %(package)s %(version)s in %(pocket)s %(result)s.
82 Branch %(pocket)s has been advanced to %(short_commit)s.
84 (Build %(build_id)s was submitted by %(principal)s at %(inserted_at)s.)""" % values
86 values['result'] = failure(values['result'])
87 msg = """Build of %(package)s %(version)s in %(pocket)s %(result)s while %(failed_stage)s.
91 (Build %(build_id)s was submitted by %(principal)s at %(inserted_at)s.)""" % values
94 def submit_completion_msg(succeeded, values, verbose=True, success=lambda x: x, failure=lambda x: x):
97 values['result'] = success(values['result'])
99 values['result'] = failure(values['result'])
100 msg = """Submission of %(commit)s to be built in %(pocket)s %(result)s.
101 Build submitted by %(principal)s.""" % values
104 def repo_creation_msg(succeeded, values, verbose=True, success=lambda x: x, failure=lambda x: x):
105 values = dict(values)
107 msg = '%(principal)s just created a new repository, %(category)s/%(name)s.git' % values
111 POST_BUILD = 'post-build'
112 FAILED_BUILD = 'failed-build'
113 POST_SUBMIT = 'post-submit'
114 FAILED_SUBMIT = 'failed-submit'
115 POST_ADD_REPO = 'post-add-repo'
117 # Types of communication
122 message_generators = {
123 ZEPHYR : { POST_BUILD : build_completion_msg,
124 FAILED_BUILD : build_completion_msg,
125 POST_SUBMIT : submit_completion_msg,
126 FAILED_SUBMIT : submit_completion_msg,
127 POST_ADD_REPO : repo_creation_msg },
128 MAIL : { POST_BUILD : build_completion_msg,
129 FAILED_BUILD : build_completion_msg,
130 POST_SUBMIT : submit_completion_msg,
131 FAILED_SUBMIT : submit_completion_msg,
132 POST_ADD_REPO : repo_creation_msg }
135 def zephyr_escape(m):
136 m = re.sub('@', '@@', m)
137 m = re.sub('}', '@(})', m)
140 def zephyr_success(m):
141 return '}@{@color(green)%s}@{' % zephyr_escape(m)
143 def zephyr_failure(m):
144 return '}@{@color(red)%s}@{' % zephyr_escape(m)
147 parser = optparse.OptionParser('Usage: %prog [options] [arguments]')
148 opts, args = parser.parse_args()
149 prog = os.path.basename(sys.argv[0])
152 if prog == POST_BUILD:
153 hook_config = config.build.hooks.post_build
154 elif prog == FAILED_BUILD:
155 hook_config = config.build.hooks.failed_build
156 elif prog == POST_SUBMIT:
157 hook_config = config.build.hooks.post_submit
158 elif prog == FAILED_SUBMIT:
159 hook_config = config.build.hooks.failed_submit
160 elif prog == POST_ADD_REPO:
161 hook_config = config.build.hooks.post_add_repo
163 parser.error('hook script invoked with unrecognized name %s' % prog)
165 except common.InvirtConfigError:
166 print >>sys.stderr, 'No hook configuration found for %s.' % prog
169 if prog in [POST_BUILD, FAILED_BUILD]:
171 parser.set_usage('Usage: %prog [options] build_id')
175 build = database.Build.query().get(args[0])
176 short_commit = builder.canonicalize_commit(build.package, build.commit, shorten=True)
177 values = { 'build_id' : build.build_id,
178 'commit' : build.commit,
179 'failed_stage' : build.failed_stage,
180 'inserted_at' : build.inserted_at,
181 'package' : build.package,
182 'pocket' : build.pocket,
183 'principal' : build.principal,
184 'short_commit' : short_commit,
185 'traceback' : build.traceback,
186 'version' : build.version,
187 'default_instance' : 'build_%(build_id)s',
188 'default_subject' : 'XVM build %(result)s: %(package)s %(version)s in %(pocket)s'}
190 assert prog == POST_BUILD
191 values['result'] = 'succeeded'
194 assert prog == FAILED_BUILD
195 values['result'] = 'failed'
197 elif prog in [POST_SUBMIT, FAILED_SUBMIT]:
199 parser.set_usage('Usage: %prog [options] pocket package commit principal')
202 values = { 'pocket' : args[0],
205 'principal' : args[3],
206 'default_instance' : 'submission',
207 'default_subject' : 'Submission %(result)s: %(package)s %(version)s in %(pocket)s'}
208 if prog == POST_SUBMIT:
209 values['result'] = 'succeeded'
212 values['result'] = 'failed'
214 elif prog in [POST_ADD_REPO]:
216 parser.set_usage('Usage: %prog [options] category name principal')
219 values = { 'category' : args[0],
221 'principal' : args[2],
222 'default_instance' : 'new-repo',
223 'default_subject' : 'New repository %(category)s/%(name)s'}
226 raise AssertionError('Impossible state')
229 zephyr_config = hook_config.zephyr
230 klass = zephyr_config['class'] % values
231 except common.InvirtConfigError:
232 print >>sys.stderr, 'No zephyr configuration specified for %s.' % prog
234 make_msg = message_generators[ZEPHYR][prog]
235 msg = '@{%s}' % make_msg(succeeded, values, verbose=False,
236 success=zephyr_success, failure=zephyr_failure)
237 instance = zephyr_config.get('instance', values['default_instance']) % values
238 zsig = zephyr_config.get('zsig', 'XVM Buildbot') % values
239 common.captureOutput(['zwrite', '-c', klass, '-i', instance, '-s',
240 zsig, '-d', '-m', msg],
241 stdout=None, stderr=None)
244 mail_config = hook_config.mail
245 to = mail_config.to % values
246 sender = mail_config['from'] % values
247 except common.InvirtConfigError:
248 print >>sys.stderr, 'No email configuration specified for %s.' % prog
250 make_msg = message_generators[MAIL][prog]
251 msg = make_msg(succeeded, values)
252 email = text.MIMEText(msg)
253 email['To'] = to % values
254 email['From'] = sender % values
255 email['Subject'] = mail_config.get('subject', values['default_subject']) % values
256 common.captureOutput(['sendmail', '-t'], email.as_string(),
257 stdout=None, stderr=None)
259 if __name__ == '__main__':