|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from datetime import datetime, timedelta |
| 4 | +import json |
| 5 | +import os |
| 6 | +import requests |
| 7 | +import sys |
| 8 | +import time |
| 9 | + |
| 10 | +ECHIDNA_ID_FILE = 'WD-echidna-id.txt' |
| 11 | +ECHIDNA_STATUS_URL = 'http://labs.w3.org/echidna/api/status?id=' |
| 12 | + |
| 13 | + |
| 14 | +def get_echidna_id(directory): |
| 15 | + id_file = os.path.join(directory, ECHIDNA_ID_FILE) |
| 16 | + file_time = os.path.getmtime(id_file) |
| 17 | + if datetime.fromtimestamp(file_time) < datetime.now() - timedelta(hours=1): |
| 18 | + print(f'Warning: Echidna ID is not recent: timestamp is {file_time}') |
| 19 | + with open(id_file, 'r') as f: |
| 20 | + return f.read().strip() |
| 21 | + |
| 22 | + |
| 23 | +def get_current_response(echidna_id): |
| 24 | + url = ECHIDNA_STATUS_URL + echidna_id |
| 25 | + print(f'Fetching {url}') |
| 26 | + response = requests.get(url, allow_redirects=True) |
| 27 | + if response.status_code != 200: |
| 28 | + print(f'Got status code {response.status_code}, text:') |
| 29 | + print(response.text) |
| 30 | + raise Exception('Failed to fetch echidna result') |
| 31 | + |
| 32 | + return response.json() |
| 33 | + |
| 34 | + |
| 35 | +def get_echidna_result(echidna_id): |
| 36 | + response = get_current_response(echidna_id) |
| 37 | + while response['results']['status'] == 'started': |
| 38 | + time.sleep(5) |
| 39 | + print('Echidna run in progress, retrying...') |
| 40 | + response = get_current_response(echidna_id) |
| 41 | + |
| 42 | + result = response['results']['status'] |
| 43 | + print(f'Echidna issue {echidna_id} is {result}.') |
| 44 | + print(json.dumps(response, indent=2)) |
| 45 | + return result == 'success' |
| 46 | + |
| 47 | + |
| 48 | +def main(argv): |
| 49 | + directory = os.getcwd() if len(argv) < 2 else argv[1] |
| 50 | + echidna_id = get_echidna_id(directory) |
| 51 | + print(f'Got echidna id {echidna_id}.') |
| 52 | + time.sleep(5) |
| 53 | + if not get_echidna_result(echidna_id): |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + main(sys.argv) |
0 commit comments