summaryrefslogtreecommitdiffstats
path: root/jenkins_jobs/modules/base.py
blob: ae55568b925d4bdb5599e973f921c99ad1dc5fb2 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Copyright 2012 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# Base class for a jenkins_jobs module

import pkg_resources
import yaml
import xml.etree.ElementTree as XML


def add_nonblank_xml_subelement(parent, tag, value):
    """
    Adds an XML SubElement with the name tag to parent if value is a non-empty
    string
    """
    if value is not None and value != '':
        XML.SubElement(parent, tag).text = value


class Base(object):
    """
    A base class for a Jenkins Job Builder Module.

    The module is initialized before any YAML is parsed.

    :arg ModuleRegistry registry: the global module registry.
    """

    #: The sequence number for the module.  Modules are invoked in the
    #: order of their sequence number in order to produce consistently
    #: ordered XML output.
    sequence = 10

    def __init__(self, registry):
        self.registry = registry

    def handle_data(self, parser):
        """This method is called before any XML is generated.  By
        overriding this method, the module may manipulate the YAML
        data structure on the parser however it likes before any XML
        is generated.  If it has changed the data structure at all, it
        must return ``True``, otherwise, it must return ``False``.

        :arg YAMLParser parser: the global YAML Parser
        :rtype: boolean
        """

        return False

    def gen_xml(self, parser, xml_parent, data):
        """Update the XML element tree based on YAML data.  Override
        this method to add elements to the XML output.  Create new
        Element objects and add them to the xml_parent.  The YAML data
        structure must not be modified.

        :arg YAMLParser parser: the global YAML Parser
        :arg Element xml_parent: the parent XML element
        :arg dict data: the YAML data structure
        """

        pass

    def _dispatch(self, component_type, component_list_type,
                  parser, xml_parent,
                  component, template_data={}):
        """This is a private helper method that you can call from your
        implementation of gen_xml.  It allows your module to define a
        type of component, and benefit from extensibility via Python
        entry points and Jenkins Job Builder :ref:`Macros <macro>`.

        :arg string component_type: the name of the component
          (e.g., `builder`)
        :arg string component_list_type: the plural name of the component
          type (e.g., `builders`)
        :arg YAMLParser parser: the global YMAL Parser
        :arg Element xml_parent: the parent XML element
        :arg dict template_data: values that should be interpolated into
          the component definition

        The value of `component_list_type` will be used to look up
        possible implementations of the component type via entry
        points (entry points provide a list of components, so it
        should be plural) while `component_type` will be used to look
        for macros (they are defined singularly, and should not be
        plural).

        See the Publishers module for a simple example of how to use
        this method.
        """

        if isinstance(component, dict):
            # The component is a sigleton dictionary of name: dict(args)
            name, component_data = component.items()[0]
            if template_data:
                # Template data contains values that should be interpolated
                # into the component definition
                s = yaml.dump(component_data, default_flow_style=False)
                s = s.format(**template_data)
                component_data = yaml.load(s)
        else:
            # The component is a simple string name, eg "run-tests"
            name = component
            component_data = {}

        # Look for a component function defined in an entry point
        for ep in pkg_resources.iter_entry_points(
            group='jenkins_jobs.{0}'.format(component_list_type), name=name):
            func = ep.load()
            func(parser, xml_parent, component_data)
        else:
            # Otherwise, see if it's defined as a macro
            component = parser.data.get(component_type, {}).get(name)
            if component:
                for b in component[component_list_type]:
                    # Pass component_data in as template data to this function
                    # so that if the macro is invoked with arguments,
                    # the arguments are interpolated into the real defn.
                    self._dispatch(component_type, component_list_type,
                                   parser, xml_parent, b, component_data)