aboutsummaryrefslogtreecommitdiff
path: root/control/mangle-jobs
blob: ae841cce14826a25b69ffa125f45cc96d9fb9e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python
"""Helper to mass-edit jobs in jenkins.

"""

###############################################################################
# Copyright (c) 2011 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
from tempfile import NamedTemporaryFile
import urllib2


from lxml.etree import fromstring, tostring

really_change = False
if '--really' in sys.argv:
    sys.argv.remove('--really')
    really_change = True

username = sys.argv[1]
password = open(sys.argv[2]).read().strip()
d = {}
execfile(sys.argv[3], d, d)
mangler = d['mangle']

auth_headers = {
    'Authorization': 'Basic %s' % (
        base64.encodestring('%s:%s' % (username, password))[:-1],),
    }

def _authJenkins(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)
    req = urllib2.Request(
        'http://localhost:9090/jenkins/' + jenkins_path, data, headers)
    resp = urllib2.urlopen(req)
    return resp.read()

def getJobConfig(job_name):
    return _authJenkins('job/' + job_name + '/config.xml')

def postConfig(url, configXml):
    _authJenkins(url, configXml, {'Content-Type': 'text/xml'})


jobs = json.load(urllib2.urlopen('http://localhost:9090/jenkins/api/json?tree=jobs[name]'))
names = [job['name'] for job in jobs['jobs']]
names = [name for name in names if name == 'blank' or '_' in name]
for name in names:
    print name
    text = getJobConfig(name)
    tree = fromstring(text)
    mangler(tree)
    if not really_change:
        with nested(NamedTemporaryFile(), NamedTemporaryFile()) as (a, b):
            a.write(text)
            b.write(tostring(tree))
            a.flush(); b.flush()
            os.system('xmldiff %s %s' % (a.name, b.name))
    else:
        postConfig('job/' + name + '/config.xml', tostring(tree))