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