From 7e91f7a75401b3f5f6caa50406e5d54a781cf29c Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Fri, 11 Oct 2024 08:57:06 -0400 Subject: [PATCH] jjb: Add `to_groovy` filter to convert variables to groovy syntax To update jobs that use this filter, use recent jjb versions and execute it as follows: ``` PYTHONPATH=. jenkins-job update ./jobs/linux.yml ``` Change-Id: I09b4b5e3e57f4bfeb76eac00399f3bb885bb1b2b Signed-off-by: Kienan Stewart --- jjb_filters/__init__.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 jjb_filters/__init__.py diff --git a/jjb_filters/__init__.py b/jjb_filters/__init__.py new file mode 100644 index 0000000..93e9f19 --- /dev/null +++ b/jjb_filters/__init__.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +""" +""" +import jinja2 +@jinja2.pass_environment +def do_groovy(env, data, skip_list_wrap=False): + list_format="[{}]" + if skip_list_wrap: + list_format="{}" + if isinstance(data, str): + return '"{}"'.format(data.replace('"', '\"')) + elif isinstance(data, list) or isinstance(data, tuple): + _data = [do_groovy(env, d) for d in data] + return list_format.format(", ".join(_data)) + elif isinstance(data, dict): + _data = ["{}: {}".format(key, do_groovy(env, value)) for key, value in data.items()] + return "[{}]".format(", ".join(_data)) + elif isinstance(data, bool): + return 'true' if data else 'false' + else: + raise Exception("Unknown data type: '{}'".format(type(data))) +FILTERS = { + "to_groovy": do_groovy, +} -- 2.34.1