1#!/usr/bin/python 2 3import os 4import sys 5import urllib2 6import json 7 8# This is pretty constant, but allow it to be overriden via env-var 9url = os.getenv('TRAVIS_API_URL', 'https://api.travis-ci.org') 10 11if (not url.lower().startswith("https://")): 12 print (0) 13 sys.exit(0) 14 15ci_token = os.getenv('TRAVIS_API_TOKEN') 16build_id = os.getenv('TRAVIS_BUILD_ID') 17 18headers = { 19 'Content-Type': 'application/json', 20 'Accept': 'application/json', 21 'Travis-API-Version': "3", 22 'Authorization': "token {0}".format(ci_token) 23} 24 25# Codacy's bandit linter may complain that we haven't validated 26# this URL for permitted schemes; we have validated this a few lines above 27req = urllib2.Request("{0}/build/{1}/jobs".format(url, build_id), 28 headers=headers) 29 30response = urllib2.urlopen(req).read() 31json_r = json.loads(response.decode('utf-8')) 32 33jobs_running = 0 34for job in json_r['jobs']: 35 # bump number of jobs higher, so nothing triggers 36 if (job['state'] in [ 'canceled', 'failed' ]): 37 jobs_running += 99 38 break 39 if (job['state'] in [ 'started', 'created', 'queued', 'received' ]): 40 jobs_running += 1 41 42print (jobs_running) 43