Add mk.common.GetBaseName function

Change-Id: I8bc1544e47ccb9883e9aa6a212785fd5c15efe9f
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 7167b08..8dc2bf6 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -564,39 +564,39 @@
 
  */
 def diffCheckMultidir(diffData) {
-  common = new com.mirantis.mk.Common()
-  // Some global constants. Don't change\move them!
-  keyNew = 'new'
-  keyRemoved = 'removed'
-  keyDiff = 'diff'
-  def output = [
-      new    : [],
-      removed: [],
-      diff   : [],
-  ]
-  String pathSep = '/'
-  diffData.each { line ->
-    def job_file = ''
-    def job_type = ''
-    if (line.startsWith('Files old/')) {
-      job_file = new File(line.replace('Files old/', '').tokenize()[0])
-      job_type = keyDiff
-    } else if (line.startsWith('Only in new/')) {
-      // get clean normalized filepath, under new/
-      job_file = new File(line.replace('Only in new/', '').replace(': ', pathSep)).toString()
-      job_type = keyNew
-    } else if (line.startsWith('Only in old/')) {
-      // get clean normalized filepath, under old/
-      job_file = new File(line.replace('Only in old/', '').replace(': ', pathSep)).toString()
-      job_type = keyRemoved
-    } else {
-      common.warningMsg("Not parsed diff line: ${line}!")
+    common = new com.mirantis.mk.Common()
+    // Some global constants. Don't change\move them!
+    keyNew = 'new'
+    keyRemoved = 'removed'
+    keyDiff = 'diff'
+    def output = [
+        new    : [],
+        removed: [],
+        diff   : [],
+    ]
+    String pathSep = '/'
+    diffData.each { line ->
+        def job_file = ''
+        def job_type = ''
+        if (line.startsWith('Files old/')) {
+            job_file = new File(line.replace('Files old/', '').tokenize()[0])
+            job_type = keyDiff
+        } else if (line.startsWith('Only in new/')) {
+            // get clean normalized filepath, under new/
+            job_file = new File(line.replace('Only in new/', '').replace(': ', pathSep)).toString()
+            job_type = keyNew
+        } else if (line.startsWith('Only in old/')) {
+            // get clean normalized filepath, under old/
+            job_file = new File(line.replace('Only in old/', '').replace(': ', pathSep)).toString()
+            job_type = keyRemoved
+        } else {
+            common.warningMsg("Not parsed diff line: ${line}!")
+        }
+        if (job_file != '') {
+            output[job_type].push(job_file)
+        }
     }
-    if (job_file != '') {
-      output[job_type].push(job_file)
-    }
-  }
-  return output
+    return output
 }
 
 /**
@@ -614,88 +614,101 @@
  * TODO: implement proper regex?
  **/
 def comparePillars(compRoot, b_url, findRegEx) {
-  common = new com.mirantis.mk.Common()
-  //  findRegEx = '.*.infra/secrets.yml'
-  //  if (findRegEx) {
-  //    withEnv(["S_REGEX=${findRegEx}"]) {
-  //      sh(script: """
-  //        find ${dir1} ${dir2} -type f \\(  -regex '${findRegEx}' \\) > diff_exclude.list
-  //
-  //        """)
-  //      cmdline = '--exclude-from=diff_exclude.list'
-  //    }
-  //  }
-  // Some global constants. Don't change\move them!
-  keyNew = 'new'
-  keyRemoved = 'removed'
-  keyDiff = 'diff'
-  def diff_status = 0
-  // FIXME
-  httpWS = b_url + '/artifact/'
-  dir(compRoot) {
-    diff_status = sh(
-        // If diff empty - exit 0
-        script: """
+    common = new com.mirantis.mk.Common()
+    //  findRegEx = '.*.infra/secrets.yml'
+    //  if (findRegEx) {
+    //    withEnv(["S_REGEX=${findRegEx}"]) {
+    //      sh(script: """
+    //        find ${dir1} ${dir2} -type f \\(  -regex '${findRegEx}' \\) > diff_exclude.list
+    //
+    //        """)
+    //      cmdline = '--exclude-from=diff_exclude.list'
+    //    }
+    //  }
+    // Some global constants. Don't change\move them!
+    keyNew = 'new'
+    keyRemoved = 'removed'
+    keyDiff = 'diff'
+    def diff_status = 0
+    // FIXME
+    httpWS = b_url + '/artifact/'
+    dir(compRoot) {
+        diff_status = sh(
+            // If diff empty - exit 0
+            script: """
           diff -q -r old/ new/  > pillar.diff
           """,
-        returnStatus: true,
-    )
-  }
-  // Set job description
-  String description = ''
-  if (diff_status == 1) {
-    // Analyse output file and prepare array with results
-    String data_ = readFile file: "${compRoot}/pillar.diff"
-    def diff_list = diffCheckMultidir(data_.split("\\r?\\n"))
-    common.infoMsg(diff_list)
-    dir(compRoot) {
-      if (diff_list[keyDiff].size() > 0) {
-        if (!fileExists('diff')) {
-          sh('mkdir -p diff')
-        }
-        description += '<b>CHANGED</b><ul>'
-        common.infoMsg('Changed items:')
-        for (item in diff_list[keyDiff]) {
-          // We don't want to handle sub-dirs structure. So, simply make diff 'flat'
-          item_f = item.toString().replace('/', '_')
-          description += "<li><a href=\"${httpWS}/diff/${item_f}/*view*/\">${item}</a></li>"
-          // Generate diff file
-          def diff_exit_code = sh([
-              script      : "diff -U 50 old/${item} new/${item} > diff/${item_f}",
-              returnStdout: false,
-              returnStatus: true,
-          ])
-          // catch normal errors, diff should always return 1
-          if (diff_exit_code != 1) {
-            error 'Error with diff file generation'
-          }
-        }
-      }
-      if (diff_list[keyNew].size() > 0) {
-        description += '<b>ADDED</b><ul>'
-        for (item in diff_list[keyNew]) {
-          description += "<li><a href=\"${httpWS}/new/${item}/*view*/\">${item}</a></li>"
-        }
-      }
-      if (diff_list[keyRemoved].size() > 0) {
-        description += '<b>DELETED</b><ul>'
-        for (item in diff_list[keyRemoved]) {
-          description += "<li><a href=\"${httpWS}/old/${item}/*view*/\">${item}</a></li>"
-        }
-      }
-
+            returnStatus: true,
+        )
     }
-  }
+    // Set job description
+    String description = ''
+    if (diff_status == 1) {
+        // Analyse output file and prepare array with results
+        String data_ = readFile file: "${compRoot}/pillar.diff"
+        def diff_list = diffCheckMultidir(data_.split("\\r?\\n"))
+        common.infoMsg(diff_list)
+        dir(compRoot) {
+            if (diff_list[keyDiff].size() > 0) {
+                if (!fileExists('diff')) {
+                    sh('mkdir -p diff')
+                }
+                description += '<b>CHANGED</b><ul>'
+                common.infoMsg('Changed items:')
+                for (item in diff_list[keyDiff]) {
+                    // We don't want to handle sub-dirs structure. So, simply make diff 'flat'
+                    item_f = item.toString().replace('/', '_')
+                    description += "<li><a href=\"${httpWS}/diff/${item_f}/*view*/\">${item}</a></li>"
+                    // Generate diff file
+                    def diff_exit_code = sh([
+                        script      : "diff -U 50 old/${item} new/${item} > diff/${item_f}",
+                        returnStdout: false,
+                        returnStatus: true,
+                    ])
+                    // catch normal errors, diff should always return 1
+                    if (diff_exit_code != 1) {
+                        error 'Error with diff file generation'
+                    }
+                }
+            }
+            if (diff_list[keyNew].size() > 0) {
+                description += '<b>ADDED</b><ul>'
+                for (item in diff_list[keyNew]) {
+                    description += "<li><a href=\"${httpWS}/new/${item}/*view*/\">${item}</a></li>"
+                }
+            }
+            if (diff_list[keyRemoved].size() > 0) {
+                description += '<b>DELETED</b><ul>'
+                for (item in diff_list[keyRemoved]) {
+                    description += "<li><a href=\"${httpWS}/old/${item}/*view*/\">${item}</a></li>"
+                }
+            }
 
-  if (description != '') {
-    dir(compRoot) {
-      archiveArtifacts([
-          artifacts        : '**',
-          allowEmptyArchive: true,
-      ])
+        }
     }
-    return description.toString()
-  } else {
-    return 'No job changes'
-  }
+
+    if (description != '') {
+        dir(compRoot) {
+            archiveArtifacts([
+                artifacts        : '**',
+                allowEmptyArchive: true,
+            ])
+        }
+        return description.toString()
+    } else {
+        return 'No job changes'
+    }
 }
+
+/**
+ * Simple function, to get basename from string.
+ * line - path-string
+ * remove_ext - string, optionl. Drop file extenstion.
+ **/
+def GetBaseName(line, remove_ext) {
+    filename = line.toString().split('/').last()
+    if (remove_ext && filename.endsWith(remove_ext.toString())) {
+        filename = filename.take(filename.lastIndexOf(remove_ext.toString()))
+    }
+    return filename
+}
\ No newline at end of file