Last active
July 19, 2016 13:03
-
-
Save larrycai/6828c959f57105ca93239ca6aa4fc6fa to your computer and use it in GitHub Desktop.
code sample for http://stackoverflow.com/questions/34270130/jenkins-next-execution-plugin-how-to-get-details-through-rest-api
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 urllib2 | |
import ssl | |
import lxml | |
import lxml.html | |
CTX = ssl.create_default_context() | |
CTX.check_hostname = False | |
CTX.verify_mode = ssl.CERT_NONE | |
def get_next_execution(url): | |
""" | |
Get next execution list from jenkins website by parsing the html files | |
return | |
[ | |
(datetime, jobname, url), | |
... | |
] | |
Better to use JSON API, see issue: https://issues.jenkins-ci.org/browse/JENKINS-36210 | |
""" | |
status = [] | |
try: | |
html = urllib2.urlopen(url, context=CTX).read() | |
except Exception,e: | |
print "can't handle url '%s'" % url | |
print "exception:" , e | |
return status | |
# use beautifulSoup4 instead of lxml is better, but avoid to use external module so far | |
html2 = lxml.html.fromstring(html) | |
div = html2.get_element_by_id("next-exec") | |
result = lxml.html.tostring(div) | |
tree = lxml.html.fromstring(result) # ugly, but it works | |
trs = tree.xpath('/html/body/div/div/table/tr') | |
for tr in trs: | |
tds = tr.xpath("td") | |
url = tds[0].xpath("a/@href")[0] | |
jobname = tds[0].text_content() | |
datetime = tds[1].text_content() | |
status.append((datetime, jobname, url)) | |
return status | |
if __name__== "__main__": | |
url = "http://localhost:8080" | |
print get_next_execution(url) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment