Pip based docutols

Change-Id: I09d51954f01d8dac5e651852b5b164ad1166e7cd
diff --git a/src/com/mirantis/mk/openstack.groovy b/src/com/mirantis/mk/openstack.groovy
index 6a0a9ce..05e8646 100644
--- a/src/com/mirantis/mk/openstack.groovy
+++ b/src/com/mirantis/mk/openstack.groovy
@@ -28,6 +28,7 @@
         'oslo.i18n>=2.3.0,<2.4.0',
         'oslo.serialization>=1.8.0,<1.9.0',
         'oslo.utils>=1.4.0,<1.5.0',
+        'docutils>=0.12'
     ]
 
     def openstack_latest_packages = openstack_kilo_packages
@@ -101,7 +102,7 @@
     def python = new com.mirantis.mk.python()
     cmd = "keystone token-get"
     outputTable = runOpenstackCommand(cmd, client, path)
-    output = python.parseTextTable(outputTable, 'item', 'prettytable')
+    output = python.parseTextTable(outputTable, 'item', 'prettytable', 'openstack', path)
     return output
 }
 
@@ -149,7 +150,7 @@
     dir("${env.WORKSPACE}/template/template") {
         outputTable = runOpenstackCommand(cmd, client, path)
     }
-    output = python.parseTextTable(outputTable, 'item', 'prettytable')
+    output = python.parseTextTable(outputTable, 'item', 'prettytable', 'openstack', path)
 
     i = 1
     while (true) {
@@ -193,7 +194,7 @@
     def python = new com.mirantis.mk.python()
     cmd = "heat stack-show ${name}"
     outputTable = runOpenstackCommand(cmd, env, path)
-    output = python.parseTextTable(outputTable, 'item', 'prettytable')
+    output = python.parseTextTable(outputTable, 'item', 'prettytable', 'openstack', path)
     return output
 }
 
@@ -222,7 +223,7 @@
     def python = new com.mirantis.mk.python()
     cmd = "heat resource-list ${name}"
     outputTable = runOpenstackCommand(cmd, env, path)
-    output = python.parseTextTable(outputTable, 'list', 'prettytable')
+    output = python.parseTextTable(outputTable, 'list', 'prettytable', 'openstack', path)
     return output
 }
 
@@ -237,7 +238,7 @@
     def python = new com.mirantis.mk.python()
     cmd = "heat resource-show ${name} ${resource}"
     outputTable = runOpenstackCommand(cmd, env, path)
-    output = python.parseTextTable(outputTable, 'item', 'prettytable')
+    output = python.parseTextTable(outputTable, 'item', 'prettytable', 'openstack', path)
     return output
 }
 
@@ -252,7 +253,7 @@
     def python = new com.mirantis.mk.python()
     cmd = "heat stack-update ${name}"
     outputTable = runOpenstackCommand(cmd, env, path)
-    output = python.parseTextTable(outputTable, 'item', 'prettytable')
+    output = python.parseTextTable(outputTable, 'item', 'prettytable', 'openstack', path)
     return output
 }
 
diff --git a/src/com/mirantis/mk/python.groovy b/src/com/mirantis/mk/python.groovy
index 88b624d..b61ea7f 100644
--- a/src/com/mirantis/mk/python.groovy
+++ b/src/com/mirantis/mk/python.groovy
@@ -42,6 +42,20 @@
     return output
 }
 
+
+/**
+ * Install docutils in isolated environment
+ *
+ * @param path        Path where virtualenv is created
+ */
+def setupDocutilsVirtualenv(path) {
+    requirements = [
+      'docutils',
+    ]
+    setupVirtualenv(path, 'python2', requirements)
+}
+
+
 @NonCPS
 def loadJson(rawData) {
     return new groovy.json.JsonSlurperClassic().parseText(rawData)
@@ -54,7 +68,7 @@
  * @param mode       Either list (1st row are keys) or item (key, value rows)
  * @param format     Format of the table
  */
-def parseTextTable(tableStr, type = 'item', format = 'rest') {
+def parseTextTable(tableStr, type = 'item', format = 'rest', path = none) {
     parserFile = "${env.WORKSPACE}/textTableParser.py"
     parserScript = """import json
 import argparse
@@ -179,10 +193,17 @@
     writeFile file: parserFile, text: parserScript
     tableFile = "${env.WORKSPACE}/prettytable.txt"
     writeFile file: tableFile, text: tableStr
-    rawData = sh (
-        script: "python ${parserFile} --file '${tableFile}' --type ${type}",
-        returnStdout: true
-    ).trim()
+
+    cmd = "python ${parserFile} --file '${tableFile}' --type ${type}"
+    if (path) {
+        rawData = python.runVirtualenvCommand(path, cmd)
+    }
+    else {
+        rawData = sh (
+            script: cmd,
+            returnStdout: true
+        ).trim()
+    }
     data = loadJson(rawData)
     echo("[Parsed table] ${data}")
     return data