Skip to content

Instantly share code, notes, and snippets.

@hughsaunders
Created April 15, 2015 08:26
Show Gist options
  • Save hughsaunders/9264801b844a811419b6 to your computer and use it in GitHub Desktop.
Save hughsaunders/9264801b844a811419b6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import click
import re
import sys
# remember to install all the apt xml stuff - not just the pip packages.
from lxml import etree
## Jenkins Build Summary Script
# This script reads all the build.xml files specified and prints a summary of each job.
# This summary includes the cluster it ran on and all the parent jobs.
# Note that EnvVars.txt is read from the same dir as build.xml.
@click.command(help='args are paths to jenkins build.xml files')
@click.argument('builds', nargs=-1)
def summary(builds):
for build in builds:
path_groups_match = re.search('^(?P<build_folder>.*/(?P<job_name>[^/]+)/builds/(?P<job_num>[0-9]+))/',
build)
if not path_groups_match:
continue
path_groups = path_groups_match.groupdict()
env_file = '{build_folder}/injectedEnvVars.txt'.format(**path_groups)
env_vars = read_env_file(env_file)
summary = '{job_num}/{job_name} {cluster} |'.format(job_name=path_groups['job_name'],
job_num=path_groups['job_num'],
cluster=env_vars.get('CLUSTER_NAME'))
tree = etree.parse(build)
causes = tree.xpath('/build/actions/hudson.model.CauseAction/causes')
if causes:
causes_summary=""
for cause in causes[0].getchildren():
causes_summary += summarise_causes(cause)
print "{summary} {causes_summary}".format(**locals())
def read_env_file(path):
kvs = {}
with open(path) as env_file:
for line in env_file:
line = line.split('=')
kvs[line[0].strip()] = line[1].strip()
return kvs
def summarise_causes(elem):
summary=""
job_name = elem.find('./upstreamProject')
if job_name is not None:
job_name = job_name.text
job_num = elem.find('upstreamBuild').text
summary = '{job_name}/{job_num} '.format(**locals())
elif elem.find('./tEvent') is not None:
review_number = elem.find('.//number').text
summary = 'gerrit/{review_number}'.format(**locals())
upstream_causes = elem.find('./upstreamCauses')
if upstream_causes is not None:
for usc in upstream_causes.getchildren():
summary += summarise_causes(usc)
return summary
if __name__ == '__main__':
summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment