Skip to content

Instantly share code, notes, and snippets.

@jsoriano
Created April 25, 2014 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsoriano/11286650 to your computer and use it in GitHub Desktop.
Save jsoriano/11286650 to your computer and use it in GitHub Desktop.
Retrieves duration of jenkins executions of a job as a CSV
import argparse
import getpass
import json
import requests
def parse_arguments():
parser = argparse.ArgumentParser(
description="Get duration of jenkins jobs along its executions")
parser.add_argument('job_url', metavar='JOB_URL')
parser.add_argument('--user', '-u', dest='user')
parser.add_argument('--output', '-o', dest='output')
return parser.parse_args()
def ask_pass():
return getpass.getpass('Password: ')
def get_job_execution(url, execution, auth=None):
request_url = "{url}/{execution}/api/json".format(
url=url, execution=str(execution))
response = requests.get(request_url, auth=auth)
return json.loads(response.content)
def write_line(output_file, line):
csv_line = ",".join(str(item) for item in line)
print csv_line
output_file.write(csv_line + '\n')
if __name__ == '__main__':
auth = None
arguments = parse_arguments()
if arguments.user is not None:
auth = (arguments.user, ask_pass())
try:
with open(arguments.output, 'w') as output_file:
execution_id = 0
while True:
execution_id += 1
execution = get_job_execution(arguments.job_url, execution_id, auth)
write_line(output_file, (execution_id, execution['duration']))
except ValueError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment