Created
December 21, 2016 08:24
-
-
Save w-vi/8616d2dfaed99339dc4485bffe78eebb to your computer and use it in GitHub Desktop.
Pull Circle CI build data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import logging | |
import argparse | |
import textwrap | |
import json | |
import dateutil.parser | |
import dateutil | |
import urllib.request as urllib | |
from time import mktime | |
def _do(url, req): | |
if req is None: | |
h = { | |
'Accept': "application/json" | |
} | |
else: | |
h = { | |
'Content-type': "application/json", | |
'Accept': "application/json", | |
'User-Agent': "Mozilla", | |
} | |
try: | |
r = urllib.Request(url, req, headers=h) | |
return urllib.urlopen(r).read().decode("utf-8") | |
except urllib.HTTPError as e: | |
logging.error('POST %s : Error (%d)' % | |
(url, e.code), file=sys.stderr) | |
logging.error(e.read().decode("utf-8")) | |
return None | |
except urllib.URLError as e: | |
logging.error('Host unreachable %s.' % | |
(e.reason), file=sys.stderr) | |
return None | |
def main(): | |
parser = argparse.ArgumentParser( | |
description=textwrap.dedent('''\ | |
Gather stats from CI build times of core app'''), | |
epilog=textwrap.dedent('''\ | |
Example: | |
core-app-tests.py -v 34125 500 | |
'''), | |
formatter_class=argparse.RawDescriptionHelpFormatter) | |
parser.add_argument('-V', '--version', help='print version info', | |
action='store_true', default=False) | |
parser.add_argument('-v', '--verbose', help='verbose (print file names)', | |
action='store_true', default=False) | |
parser.add_argument('start', type=int, nargs=1, default=0) | |
parser.add_argument('count', type=int, nargs=1, default=100) | |
parser.add_argument('res', type=str, nargs=1, default=100) | |
args = parser.parse_args() | |
loglevel = logging.INFO | |
if args.verbose: | |
loglevel = logging.DEBUG | |
logging.basicConfig(format='%(message)s', | |
datefmt='%I:%M:%S', level=loglevel) | |
with open(args.res[0], "w") as f: | |
for i in range(args.start[0], args.start[0] + args.count[0]): | |
url = "https://circleci.com/api/v1.1/project/github/apiaryio/apiary/%d?circle-token=<CIRCLE_CI_TOKEN>" % ( | |
i) | |
res = json.loads(_do(url, None)) | |
if res['outcome'] in ['canceled', 'infrastructure_fail']: | |
f.write("%d,%s,0,%d\n" | |
% (res['build_num'], | |
res['outcome'], | |
mktime(dateutil.parser.parse(res['stop_time']).timetuple()))) | |
logging.info("%d[%s] : 0 ms" | |
% (res['build_num'], | |
res['outcome'])) | |
else: | |
f.write("%d,%s,%d,%d\n" | |
% (res['build_num'], | |
res['outcome'], | |
res['build_time_millis'], | |
mktime(dateutil.parser.parse(res['start_time']).timetuple()))) | |
logging.info("%d[%s] : %d ms" | |
% (res['build_num'], | |
res['outcome'], | |
res['build_time_millis'])) | |
f.flush() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment