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