From fd0f760664cdb784d65f0f0022776c1803d53983 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sat, 16 Dec 2017 18:07:56 +0100 Subject: [PATCH] Changed travis config to run our custome script to upload a image to dropbox based on how many tests passed. --- .travis.yml | 3 ++- badge.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 badge.py diff --git a/.travis.yml b/.travis.yml index e69d302..e0027ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ python: install: - pip install -r requirements.txt script: - - coverage run -m pytest + - pytest --junitxml=result.xml + - badge.py after_success: - coveralls diff --git a/badge.py b/badge.py new file mode 100755 index 0000000..173ab8c --- /dev/null +++ b/badge.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3.6 +import os, dropbox, requests +import xml.etree.ElementTree as ET +from dropbox.files import WriteMode + +from colour import Color + +def get_xml_test_results(): + tree = ET.parse('result.xml') + root = tree.getroot() + failures = int(root.attrib['failures']) + total = int(root.attrib['tests']) + return [failures, total] + +def getPicture(): + with open("progress.svg", "rb") as imageFile: + return imageFile.read() + +def download_image(image_url): + session = requests.Session() + session.headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36"} + + r = session.get(image_url, stream=True, verify=False) + with open('progress.svg', 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + f.write(chunk) + +def getColor(value): + start = Color('red') + end = Color('yellowgreen') + colors = list(start.range_to(Color(end), 10)) + hexList = [] + for color in colors: + if not str(color)[:1] is '#': + hexList.append(color.hex[1:]) + else: + hexList.append(str(color)[1:]) + + if (len(hexList[-1]) <= 4): + hexList[-1] = hexList[-1] + '000' + + return hexList[int(float(value)/10)] + +def uploadToDropbox(progress): + dbx = dropbox.Dropbox(os.environ.get('DROPBOX_ACCESS_TOKEN')) + print(dbx.users_get_current_account()) + + response = dbx.files_upload(getPicture(), '/knowit/progress.svg', mode=WriteMode('overwrite', None)) + print('response:', response) + +def main(): + failures, total = get_xml_test_results() + progress = "{0:.1f}".format((total-failures)/total * 100) + color = getColor(progress) + + path = 'https://img.shields.io/badge/Progress-{}-{}.svg'.format(progress, color) + download_image(path) + + print(progress) + uploadToDropbox(progress) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index fabcb24..bd168b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pytest>=3.3.1 coveralls>=1.2.0 coverage>=4.4.2 +dropbox>=8.5.0