Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Python functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Install python virtualenv |
| 11 | * |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 12 | * @param path Path to virtualenv |
| 13 | * @param python Version of Python (python/python3) |
| 14 | * @param reqs Environment requirements in list format |
| 15 | * @param reqs_path Environment requirements path in str format |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 16 | */ |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 17 | def setupVirtualenv(path, python = 'python2', reqs = [], reqs_path=null) { |
Jakub Josef | 068f0be | 2017-04-18 14:34:59 +0200 | [diff] [blame] | 18 | def virtualenv_cmd = "virtualenv ${path} --python ${python}" |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 19 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 20 | echo("[Python ${path}] Setup ${python} environment") |
| 21 | sh(returnStdout: true, script: virtualenv_cmd) |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 22 | if (reqs_path==null) { |
| 23 | def args = "" |
| 24 | for (req in reqs) { |
| 25 | args = args + "${req}\n" |
| 26 | } |
| 27 | writeFile file: "${path}/requirements.txt", text: args |
| 28 | reqs_path = "${path}/requirements.txt" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 29 | } |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 30 | runVirtualenvCommand(path, "pip install -r ${reqs_path}") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Run command in specific python virtualenv |
| 35 | * |
| 36 | * @param path Path to virtualenv |
| 37 | * @param cmd Command to be executed |
| 38 | */ |
| 39 | def runVirtualenvCommand(path, cmd) { |
Jakub Josef | 0164dc1 | 2017-03-28 16:33:46 +0200 | [diff] [blame] | 40 | virtualenv_cmd = ". ${path}/bin/activate > /dev/null; ${cmd}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 41 | echo("[Python ${path}] Run command ${cmd}") |
| 42 | output = sh( |
| 43 | returnStdout: true, |
| 44 | script: virtualenv_cmd |
| 45 | ).trim() |
| 46 | return output |
| 47 | } |
| 48 | |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Install docutils in isolated environment |
| 52 | * |
| 53 | * @param path Path where virtualenv is created |
| 54 | */ |
| 55 | def setupDocutilsVirtualenv(path) { |
| 56 | requirements = [ |
| 57 | 'docutils', |
| 58 | ] |
| 59 | setupVirtualenv(path, 'python2', requirements) |
| 60 | } |
| 61 | |
| 62 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 63 | @NonCPS |
| 64 | def loadJson(rawData) { |
| 65 | return new groovy.json.JsonSlurperClassic().parseText(rawData) |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Parse content from markup-text tables to variables |
| 70 | * |
| 71 | * @param tableStr String representing the table |
| 72 | * @param mode Either list (1st row are keys) or item (key, value rows) |
| 73 | * @param format Format of the table |
| 74 | */ |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 75 | def parseTextTable(tableStr, type = 'item', format = 'rest', path = none) { |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 76 | parserFile = "${env.WORKSPACE}/textTableParser.py" |
| 77 | parserScript = """import json |
| 78 | import argparse |
| 79 | from docutils.parsers.rst import tableparser |
| 80 | from docutils import statemachine |
| 81 | |
| 82 | def parse_item_table(raw_data): |
| 83 | i = 1 |
| 84 | pretty_raw_data = [] |
| 85 | for datum in raw_data: |
| 86 | if datum != "": |
| 87 | if datum[3] != ' ' and i > 4: |
| 88 | pretty_raw_data.append(raw_data[0]) |
| 89 | if i == 3: |
| 90 | pretty_raw_data.append(datum.replace('-', '=')) |
| 91 | else: |
| 92 | pretty_raw_data.append(datum) |
| 93 | i += 1 |
| 94 | parser = tableparser.GridTableParser() |
| 95 | block = statemachine.StringList(pretty_raw_data) |
| 96 | docutils_data = parser.parse(block) |
| 97 | final_data = {} |
| 98 | for line in docutils_data[2]: |
| 99 | key = ' '.join(line[0][3]).strip() |
| 100 | value = ' '.join(line[1][3]).strip() |
| 101 | if key != "": |
| 102 | try: |
| 103 | value = json.loads(value) |
| 104 | except: |
| 105 | pass |
| 106 | final_data[key] = value |
| 107 | i+=1 |
| 108 | return final_data |
| 109 | |
| 110 | def parse_list_table(raw_data): |
| 111 | i = 1 |
| 112 | pretty_raw_data = [] |
| 113 | for datum in raw_data: |
| 114 | if datum != "": |
| 115 | if datum[3] != ' ' and i > 4: |
| 116 | pretty_raw_data.append(raw_data[0]) |
| 117 | if i == 3: |
| 118 | pretty_raw_data.append(datum.replace('-', '=')) |
| 119 | else: |
| 120 | pretty_raw_data.append(datum) |
| 121 | i += 1 |
| 122 | parser = tableparser.GridTableParser() |
| 123 | block = statemachine.StringList(pretty_raw_data) |
| 124 | docutils_data = parser.parse(block) |
| 125 | final_data = [] |
| 126 | keys = [] |
| 127 | for line in docutils_data[1]: |
| 128 | for item in line: |
| 129 | keys.append(' '.join(item[3]).strip()) |
| 130 | for line in docutils_data[2]: |
| 131 | final_line = {} |
| 132 | key = ' '.join(line[0][3]).strip() |
| 133 | value = ' '.join(line[1][3]).strip() |
| 134 | if key != "": |
| 135 | try: |
| 136 | value = json.loads(value) |
| 137 | except: |
| 138 | pass |
| 139 | final_data[key] = value |
| 140 | i+=1 |
| 141 | return final_data |
| 142 | |
| 143 | def parse_list_table(raw_data): |
| 144 | i = 1 |
| 145 | pretty_raw_data = [] |
| 146 | for datum in raw_data: |
| 147 | if datum != "": |
| 148 | if datum[3] != ' ' and i > 4: |
| 149 | pretty_raw_data.append(raw_data[0]) |
| 150 | if i == 3: |
| 151 | pretty_raw_data.append(datum.replace('-', '=')) |
| 152 | else: |
| 153 | pretty_raw_data.append(datum) |
| 154 | i += 1 |
| 155 | parser = tableparser.GridTableParser() |
| 156 | block = statemachine.StringList(pretty_raw_data) |
| 157 | docutils_data = parser.parse(block) |
| 158 | final_data = [] |
| 159 | keys = [] |
| 160 | for line in docutils_data[1]: |
| 161 | for item in line: |
| 162 | keys.append(' '.join(item[3]).strip()) |
| 163 | for line in docutils_data[2]: |
| 164 | final_line = {} |
| 165 | i = 0 |
| 166 | for item in line: |
| 167 | value = ' '.join(item[3]).strip() |
| 168 | try: |
| 169 | value = json.loads(value) |
| 170 | except: |
| 171 | pass |
| 172 | final_line[keys[i]] = value |
| 173 | i += 1 |
| 174 | final_data.append(final_line) |
| 175 | return final_data |
| 176 | |
| 177 | def read_table_file(file): |
| 178 | table_file = open(file, 'r') |
Ales Komarek | c000c15 | 2016-12-23 15:32:54 +0100 | [diff] [blame] | 179 | raw_data = table_file.read().split('\\n') |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 180 | table_file.close() |
| 181 | return raw_data |
| 182 | |
| 183 | parser = argparse.ArgumentParser() |
| 184 | parser.add_argument('-f','--file', help='File with table data', required=True) |
| 185 | parser.add_argument('-t','--type', help='Type of table (list/item)', required=True) |
| 186 | args = vars(parser.parse_args()) |
| 187 | |
| 188 | raw_data = read_table_file(args['file']) |
| 189 | |
| 190 | if args['type'] == 'list': |
| 191 | final_data = parse_list_table(raw_data) |
| 192 | else: |
| 193 | final_data = parse_item_table(raw_data) |
| 194 | |
| 195 | print json.dumps(final_data) |
| 196 | """ |
| 197 | writeFile file: parserFile, text: parserScript |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 198 | tableFile = "${env.WORKSPACE}/prettytable.txt" |
| 199 | writeFile file: tableFile, text: tableStr |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 200 | |
| 201 | cmd = "python ${parserFile} --file '${tableFile}' --type ${type}" |
| 202 | if (path) { |
Ales Komarek | c6d28dd | 2016-12-28 12:59:38 +0100 | [diff] [blame] | 203 | rawData = runVirtualenvCommand(path, cmd) |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 204 | } |
| 205 | else { |
| 206 | rawData = sh ( |
| 207 | script: cmd, |
| 208 | returnStdout: true |
| 209 | ).trim() |
| 210 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 211 | data = loadJson(rawData) |
| 212 | echo("[Parsed table] ${data}") |
| 213 | return data |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Install cookiecutter in isolated environment |
| 218 | * |
| 219 | * @param path Path where virtualenv is created |
| 220 | */ |
| 221 | def setupCookiecutterVirtualenv(path) { |
| 222 | requirements = [ |
| 223 | 'cookiecutter', |
Jakub Josef | 4df7827 | 2017-04-26 14:36:36 +0200 | [diff] [blame] | 224 | 'jinja2==2.8.1', |
| 225 | 'PyYAML==3.12' |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 226 | ] |
| 227 | setupVirtualenv(path, 'python2', requirements) |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Generate the cookiecutter templates with given context |
| 232 | * |
Jakub Josef | 4e10c37 | 2017-04-26 14:13:50 +0200 | [diff] [blame] | 233 | * @param template template |
| 234 | * @param context template context |
| 235 | * @param path Path where virtualenv is created (optional) |
| 236 | * @param templatePath path to cookiecutter template repo (optional) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 237 | */ |
Jakub Josef | 4e10c37 | 2017-04-26 14:13:50 +0200 | [diff] [blame] | 238 | def buildCookiecutterTemplate(template, context, outputDir = '.', path = null, templatePath = ".") { |
Tomáš Kukrál | dad7b46 | 2017-03-27 13:53:05 +0200 | [diff] [blame] | 239 | configFile = "default_config.yaml" |
| 240 | configString = "default_context:\n" |
Tomáš Kukrál | 6de8504 | 2017-04-12 17:49:05 +0200 | [diff] [blame] | 241 | writeFile file: configFile, text: context |
Jakub Josef | 4e61cc0 | 2017-04-26 14:29:09 +0200 | [diff] [blame] | 242 | command = ". ${path}/bin/activate; if [ -f ${templatePath}/generate.py ]; then python ${templatePath}/generate.py --config-file ${configFile} --template ${template} --output-dir ${outputDir}; else cookiecutter --config-file ${configFile} --output-dir ${outputDir} --overwrite-if-exists --verbose --no-input ${template}; fi" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 243 | output = sh (returnStdout: true, script: command) |
| 244 | echo("[Cookiecutter build] Output: ${output}") |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Install jinja rendering in isolated environment |
| 249 | * |
| 250 | * @param path Path where virtualenv is created |
| 251 | */ |
| 252 | def setupJinjaVirtualenv(path) { |
| 253 | requirements = [ |
| 254 | 'jinja2-cli', |
| 255 | 'pyyaml', |
| 256 | ] |
| 257 | setupVirtualenv(path, 'python2', requirements) |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Generate the Jinja templates with given context |
| 262 | * |
| 263 | * @param path Path where virtualenv is created |
| 264 | */ |
| 265 | def jinjaBuildTemplate (template, context, path = none) { |
| 266 | contextFile = "jinja_context.yml" |
| 267 | contextString = "" |
| 268 | for (parameter in context) { |
| 269 | contextString = "${contextString}${parameter.key}: ${parameter.value}\n" |
| 270 | } |
| 271 | writeFile file: contextFile, text: contextString |
| 272 | cmd = "jinja2 ${template} ${contextFile} --format=yaml" |
| 273 | data = sh (returnStdout: true, script: cmd) |
| 274 | echo(data) |
| 275 | return data |
| 276 | } |