diff options
author | Paul Sokolovsky <paul.sokolovsky@linaro.org> | 2013-07-24 14:51:40 +0300 |
---|---|---|
committer | Paul Sokolovsky <paul.sokolovsky@linaro.org> | 2013-07-24 14:51:40 +0300 |
commit | d504c5e11939846c3221a0f40f98ed697285d4d8 (patch) | |
tree | 53441d89fcceb9e7e57513e0a6e090a889660ec5 /utils | |
parent | 6fab1062a5e183ee478e8cafd8d73dbfbc6ec3a9 (diff) |
Start writing command-line client for android-build.
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/cmdline-client/android-build-client | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/utils/cmdline-client/android-build-client b/utils/cmdline-client/android-build-client new file mode 100755 index 0000000..ee388e0 --- /dev/null +++ b/utils/cmdline-client/android-build-client @@ -0,0 +1,109 @@ +#!/usr/bin/env python +############################################################################### +# Copyright (c) 2013 Linaro +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################### + +import base64 +from contextlib import nested +import json +import os +import sys +import copy +import re +from tempfile import NamedTemporaryFile +import urllib2 +import optparse +import getpass +from xml.dom import minidom + +from lxml.etree import fromstring, tostring + + +auth_headers = None + +def jenkins_rest(jenkins_path, data=None, extra_headers=None): + """Make an authenticated request to jenkins. + + @param jenkins_path: The path on the Jenkins instance to make the request + to. + @param data: Data to include in the request (if this is not None the + request will be a POST). + @param extra_headers: A dictionary of extra headers that will passed in + addition to Authorization. + @raises urllib2.HTTPError: If the response is not a HTTP 200. + @returns: the body of the response. + """ + headers = auth_headers.copy() + if extra_headers: + headers.update(extra_headers) + url = options.url + jenkins_path + print url + req = urllib2.Request( + url, data, headers) + resp = urllib2.urlopen(req) + return resp.read() + +def get_job_config(job_name): + return jenkins_rest('job/' + job_name + '/config.xml') + +def set_job_config(url, configXml, extra_headers=None): + headers = {'Content-Type': 'text/xml', } + if extra_headers is not None: + headers.update(extra_headers) + _authJenkins(url, configXml, headers) + +def get_csrf_headers(): + try: + crumb_data = _authJenkins('crumbIssuer/api/json') + except urllib2.HTTPError: + # Ignore errors in case CSRF protection is not enabled + return {} + data = json.loads(crumb_data) + return {data['crumbRequestField']: data['crumb']} + + +def main(): + global auth_headers + global options + optparser = optparse.OptionParser(usage="%prog <cmd> <args>...") + optparser.add_option("--url", default="https://android-build.linaro.org/jenkins", + help="Jenkins base url, default: %default") + optparser.add_option("--user", + help="Jenkins username, default: $USER") + optparser.add_option("--passwd-file", metavar="FILE", + help="File holding Jenkins password") + optparser.add_option("--cred-file", metavar="FILE", + help="File holding Jenkins username:password pair") + optparser.add_option("--really", action="store_true", + help="Actually perform changes") + + options, args = optparser.parse_args(sys.argv[1:]) + if len(args) < 1: + optparser.error("Wrong number of arguments") + + password = None + if options.cred_file: + options.user, password = open(options.cred_file).read().strip().split(":") + elif options.passwd_file: + password = open(options.passwd_file).read().strip() + else: + password = getpass.getpass("Password/API Token:") + + if options.url[-1] != '/': + options.url += '/' + + auth_headers = { + 'Authorization': 'Basic %s' % ( + base64.encodestring('%s:%s' % (options.user, password))[:-1],), + } + + job_group, job_subname = args[1].split("/") + print get_job_config("template_" + job_group) + + +if __name__ == "__main__": + main() |