| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from collections import namedtuple |
| 14 |
from dataclasses import dataclass, field |
| 15 |
from typing import List |
| 16 |
|
| 17 |
from .constants import MAGIC_MANAGE_STRING |
| 18 |
from .errors import Context, JenkinsJobsException |
| 19 |
from .loc_loader import LocDict, LocString |
| 20 |
from .position import Pos |
| 21 |
from .formatter import enum_str_format_required_params, enum_str_format_param_defaults |
| 22 |
from .expander import Expander, expand_parameters |
| 23 |
from .defaults import Defaults |
| 24 |
from .dimensions import enum_dimensions_params, is_point_included |
| 25 |
|
| 26 |
|
| 27 |
@dataclass |
| 28 |
class JobViewData: |
| 29 |
"""Expanded job or view data, with added source context. Fed into xml generator""" |
| 30 |
|
| 31 |
data: LocDict |
| 32 |
context: List[Context] = field(default_factory=list) |
| 33 |
|
| 34 |
@property |
| 35 |
def name(self): |
| 36 |
return self.data["name"] |
| 37 |
|
| 38 |
def with_context(self, message, pos): |
| 39 |
return JobViewData(self.data, [Context(message, pos), *self.context]) |
| 40 |
|
| 41 |
|
| 42 |
@dataclass |
| 43 |
class ElementBase: |
| 44 |
"""Base class for YAML elements - job, view, template, or macro""" |
| 45 |
|
| 46 |
_defaults: dict |
| 47 |
params: dict |
| 48 |
|
| 49 |
@property |
| 50 |
def title(self): |
| 51 |
return str(self).capitalize() |
| 52 |
|
| 53 |
def _pick_defaults(self, name): |
| 54 |
try: |
| 55 |
defaults = self._defaults[name] |
| 56 |
except KeyError: |
| 57 |
if name == "global": |
| 58 |
return Defaults.empty() |
| 59 |
raise JenkinsJobsException( |
| 60 |
f"{self.title} wants defaults {name!r}, but it was never defined", |
| 61 |
pos=name.pos, |
| 62 |
) |
| 63 |
if name == "global": |
| 64 |
return defaults |
| 65 |
return defaults.merged_with_global(self._pick_defaults("global")) |
| 66 |
|
| 67 |
|
| 68 |
@dataclass |
| 69 |
class RootBase(ElementBase): |
| 70 |
"""Base class for YAML root elements - job, view or template""" |
| 71 |
|
| 72 |
_expander: Expander |
| 73 |
_keep_descriptions: bool |
| 74 |
_id: str |
| 75 |
name: str |
| 76 |
pos: Pos |
| 77 |
description: str |
| 78 |
defaults_name: str |
| 79 |
_contents: dict |
| 80 |
|
| 81 |
@property |
| 82 |
def id(self): |
| 83 |
if self._id: |
| 84 |
return self._id |
| 85 |
else: |
| 86 |
return self.name |
| 87 |
|
| 88 |
@property |
| 89 |
def contents(self): |
| 90 |
contents = self._contents.copy() |
| 91 |
if self.description is not None: |
| 92 |
contents["description"] = self.description |
| 93 |
return contents |
| 94 |
|
| 95 |
def _expand_contents(self, contents, params): |
| 96 |
expanded_contents = self._expander.expand(contents, params) |
| 97 |
description = expanded_contents.get("description") |
| 98 |
if description is not None or not self._keep_descriptions: |
| 99 |
amended_description = (description or "") + MAGIC_MANAGE_STRING |
| 100 |
expanded_contents["description"] = amended_description |
| 101 |
return expanded_contents |
| 102 |
|
| 103 |
|
| 104 |
class NonTemplateRootMixin: |
| 105 |
def top_level_generate_items(self): |
| 106 |
try: |
| 107 |
defaults = self._pick_defaults(self.defaults_name) |
| 108 |
item_params = LocDict.merge( |
| 109 |
defaults.params, |
| 110 |
self.params, |
| 111 |
) |
| 112 |
contents = LocDict.merge( |
| 113 |
defaults.contents, |
| 114 |
self.contents, |
| 115 |
pos=self.pos, |
| 116 |
) |
| 117 |
expanded_contents = self._expand_contents(contents, item_params) |
| 118 |
context = [Context(f"In {self}", self.pos)] |
| 119 |
yield JobViewData(expanded_contents, context) |
| 120 |
except JenkinsJobsException as x: |
| 121 |
raise x.with_context(f"In {self}", pos=self.pos) |
| 122 |
|
| 123 |
def generate_items(self, defaults_name, params): |
| 124 |
# Do not produce jobs/views from under project - they are produced when |
| 125 |
# processed directly from roots, by top_level_generate_items. |
| 126 |
return [] |
| 127 |
|
| 128 |
|
| 129 |
class TemplateRootMixin: |
| 130 |
def generate_items(self, defaults_name, params): |
| 131 |
try: |
| 132 |
defaults = self._pick_defaults(defaults_name or self.defaults_name) |
| 133 |
item_params = LocDict.merge( |
| 134 |
defaults.params, |
| 135 |
self.params, |
| 136 |
params, |
| 137 |
{"template-name": self.name}, |
| 138 |
) |
| 139 |
if self._id: |
| 140 |
item_params["id"] = self._id |
| 141 |
contents = LocDict.merge( |
| 142 |
defaults.contents, |
| 143 |
self.contents, |
| 144 |
pos=self.pos, |
| 145 |
) |
| 146 |
axes = list(enum_str_format_required_params(self.name, self.name.pos)) |
| 147 |
axes_defaults = dict(enum_str_format_param_defaults(self.name)) |
| 148 |
for dim_params in enum_dimensions_params(axes, item_params, axes_defaults): |
| 149 |
instance_params = LocDict.merge( |
| 150 |
item_params, |
| 151 |
dim_params, |
| 152 |
) |
| 153 |
expanded_params = expand_parameters(self._expander, instance_params) |
| 154 |
if not is_point_included( |
| 155 |
exclude_list=expanded_params.get("exclude"), |
| 156 |
params=expanded_params, |
| 157 |
key_pos=expanded_params.key_pos.get("exclude"), |
| 158 |
): |
| 159 |
continue |
| 160 |
expanded_contents = self._expand_contents(contents, expanded_params) |
| 161 |
context = [Context(f"In {self}", self.pos)] |
| 162 |
yield JobViewData(expanded_contents, context) |
| 163 |
except JenkinsJobsException as x: |
| 164 |
raise x.with_context(f"In {self}", pos=self.pos) |
| 165 |
|
| 166 |
|
| 167 |
class GroupBase: |
| 168 |
Spec = namedtuple("Spec", "name params pos") |
| 169 |
|
| 170 |
def __repr__(self): |
| 171 |
return f"<{self}>" |
| 172 |
|
| 173 |
@classmethod |
| 174 |
def _specs_from_list(cls, spec_list=None): |
| 175 |
if spec_list is None: |
| 176 |
return [] |
| 177 |
return [ |
| 178 |
cls._spec_from_dict(item, spec_list.value_pos[idx]) |
| 179 |
for idx, item in enumerate(spec_list) |
| 180 |
] |
| 181 |
|
| 182 |
@classmethod |
| 183 |
def _spec_from_dict(cls, d, pos): |
| 184 |
if isinstance(d, (str, LocString)): |
| 185 |
return cls.Spec(d, params={}, pos=pos) |
| 186 |
if not isinstance(d, dict): |
| 187 |
raise JenkinsJobsException( |
| 188 |
"Job/view spec should name or dict," |
| 189 |
f" but is {type(d)} ({d!r}). Missing indent?", |
| 190 |
pos=pos, |
| 191 |
) |
| 192 |
if len(d) != 1: |
| 193 |
raise JenkinsJobsException( |
| 194 |
"Job/view dict should be single-item," |
| 195 |
f" but have keys {list(d.keys())}. Missing indent?", |
| 196 |
pos=d.pos, |
| 197 |
) |
| 198 |
name, params = next(iter(d.items())) |
| 199 |
if params is None: |
| 200 |
params = {} |
| 201 |
else: |
| 202 |
if not isinstance(params, dict): |
| 203 |
raise JenkinsJobsException( |
| 204 |
f"Job/view {name!r} params type should be dict," |
| 205 |
f" but is {params!r}.", |
| 206 |
pos=params.pos, |
| 207 |
) |
| 208 |
return cls.Spec(name, params, pos) |
| 209 |
|
| 210 |
def _generate_items(self, root_dicts, spec_list, defaults_name, params): |
| 211 |
try: |
| 212 |
for spec in spec_list: |
| 213 |
item = self._pick_spec_item(root_dicts, spec) |
| 214 |
item_params = LocDict.merge( |
| 215 |
params, |
| 216 |
self.params, |
| 217 |
self._my_params, |
| 218 |
spec.params, |
| 219 |
) |
| 220 |
for job_data in item.generate_items(defaults_name, item_params): |
| 221 |
yield ( |
| 222 |
job_data.with_context("Defined here", spec.pos).with_context( |
| 223 |
f"In {self}", self.pos |
| 224 |
) |
| 225 |
) |
| 226 |
except JenkinsJobsException as x: |
| 227 |
raise x.with_context(f"In {self}", self.pos) |
| 228 |
|
| 229 |
@property |
| 230 |
def _my_params(self): |
| 231 |
return {} |
| 232 |
|
| 233 |
def _pick_spec_item(self, root_dict_list, spec): |
| 234 |
for roots_dict in root_dict_list: |
| 235 |
try: |
| 236 |
return roots_dict[spec.name] |
| 237 |
except KeyError: |
| 238 |
pass |
| 239 |
raise JenkinsJobsException( |
| 240 |
f"Failed to find suitable job/view/template named '{spec.name}'", |
| 241 |
pos=spec.pos, |
| 242 |
) |
| 243 |
|
| 244 |
|
| 245 |
@dataclass |
| 246 |
class Group(GroupBase): |
| 247 |
name: str |
| 248 |
pos: Pos |
| 249 |
specs: list # list[Spec] |
| 250 |
params: dict |
| 251 |
|
| 252 |
def generate_items(self, defaults_name, params): |
| 253 |
return self._generate_items(self._root_dicts, self.specs, defaults_name, params) |