Repository: incubator-systemml
Updated Branches:
  refs/heads/master 829c4dd12 -> bcd5d96fc


[SYSTEMML-1157] Project build info via MLContext API

Add values to main jar manifest at build time and allow these
values to be read via the MLContext API. These values include
build time and project version number.

Closes #318.


Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/bcd5d96f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/bcd5d96f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/bcd5d96f

Branch: refs/heads/master
Commit: bcd5d96fc9a5bc778ca6fea2696519ae9fc7a09a
Parents: 829c4dd
Author: Deron Eriksson <[email protected]>
Authored: Fri Jan 6 13:18:46 2017 -0800
Committer: Deron Eriksson <[email protected]>
Committed: Fri Jan 6 13:18:46 2017 -0800

----------------------------------------------------------------------
 pom.xml                                         |   3 +
 .../apache/sysml/api/mlcontext/MLContext.java   | 106 +++++++++++------
 .../apache/sysml/api/mlcontext/ProjectInfo.java | 113 +++++++++++++++++++
 src/main/python/systemml/mlcontext.py           |  12 ++
 4 files changed, 201 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/bcd5d96f/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 586efe3..78148cf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -487,6 +487,9 @@
                                                                </manifest>
                                                                
<manifestEntries>
                                                                        
<Build-Time>${maven.build.timestamp}</Build-Time>
+                                                                       
<Group-Id>${project.groupId}</Group-Id>
+                                                                       
<Artifact-Id>${project.artifactId}</Artifact-Id>
+                                                                       
<Version>${project.version}</Version>
                                                                
</manifestEntries>
                                                        </archive>
                                                </configuration>

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/bcd5d96f/src/main/java/org/apache/sysml/api/mlcontext/MLContext.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/api/mlcontext/MLContext.java 
b/src/main/java/org/apache/sysml/api/mlcontext/MLContext.java
index 14637c0..641beae 100644
--- a/src/main/java/org/apache/sysml/api/mlcontext/MLContext.java
+++ b/src/main/java/org/apache/sysml/api/mlcontext/MLContext.java
@@ -100,7 +100,8 @@ public class MLContext {
        private boolean statistics = false;
 
        /**
-        * The number of heavy hitters that are printed as part of the 
statistics option
+        * The number of heavy hitters that are printed as part of the 
statistics
+        * option
         */
        private int statisticsMaxHeavyHitters = 10;
 
@@ -110,6 +111,11 @@ public class MLContext {
         */
        private ExplainLevel explainLevel = null;
 
+       /**
+        * Project information such as the version and the build time.
+        */
+       private ProjectInfo projectInfo = null;
+
        private List<String> scriptHistoryStrings = new ArrayList<String>();
        private Map<String, Script> scripts = new LinkedHashMap<String, 
Script>();
 
@@ -381,8 +387,8 @@ public class MLContext {
                                }
                        }
                }
-               throw new MLContextException("Failed to parse explain level: "+ 
explainLevel + " " +
-                               "(valid types: hops, runtime, recompile_hops, 
recompile_runtime).");
+               throw new MLContextException("Failed to parse explain level: " 
+ explainLevel + " "
+                               + "(valid types: hops, runtime, recompile_hops, 
recompile_runtime).");
        }
 
        /**
@@ -399,14 +405,15 @@ public class MLContext {
                                // Do not check metadata file for registered 
reads
                                exp.setCheckMetadata(false);
 
-                               //Value retured from getVarParam is of type 
stringidentifier at runtime, but at compile type its Expression
-                               //Could not find better way to compare this 
condition.
-                               Expression datatypeExp = 
((DataExpression)source).getVarParam("data_type");
+                               // Value retured from getVarParam is of type 
stringidentifier at
+                               // runtime, but at compile type its Expression
+                               // Could not find better way to compare this 
condition.
+                               Expression datatypeExp = ((DataExpression) 
source).getVarParam("data_type");
                                String datatype = "matrix";
-                               if(datatypeExp != null)
+                               if (datatypeExp != null)
                                        datatype = datatypeExp.toString();
 
-                               if(datatype.compareToIgnoreCase("frame") != 0) {
+                               if (datatype.compareToIgnoreCase("frame") != 0) 
{
                                        MatrixObject mo = 
getMatrixObject(target);
                                        if (mo != null) {
                                                int blp = source.getBeginLine();
@@ -419,28 +426,29 @@ public class MLContext {
                                                                new 
IntIdentifier(mo.getNumColumns(), source.getFilename(), blp, bcp, elp, ecp));
                                                
exp.addVarParam(DataExpression.READNUMNONZEROPARAM,
                                                                new 
IntIdentifier(mo.getNnz(), source.getFilename(), blp, bcp, elp, ecp));
-                                               
exp.addVarParam(DataExpression.DATATYPEPARAM, new StringIdentifier("matrix", 
source.getFilename(),
-                                                               blp, bcp, elp, 
ecp));
-                                               
exp.addVarParam(DataExpression.VALUETYPEPARAM, new StringIdentifier("double", 
source.getFilename(),
-                                                               blp, bcp, elp, 
ecp));
+                                               
exp.addVarParam(DataExpression.DATATYPEPARAM,
+                                                               new 
StringIdentifier("matrix", source.getFilename(), blp, bcp, elp, ecp));
+                                               
exp.addVarParam(DataExpression.VALUETYPEPARAM,
+                                                               new 
StringIdentifier("double", source.getFilename(), blp, bcp, elp, ecp));
 
                                                if (mo.getMetaData() instanceof 
MatrixFormatMetaData) {
                                                        MatrixFormatMetaData 
metaData = (MatrixFormatMetaData) mo.getMetaData();
                                                        if 
(metaData.getOutputInfo() == OutputInfo.CSVOutputInfo) {
-                                                               
exp.addVarParam(DataExpression.FORMAT_TYPE, new StringIdentifier(
-                                                                               
DataExpression.FORMAT_TYPE_VALUE_CSV, source.getFilename(), blp, bcp, elp, 
ecp));
+                                                               
exp.addVarParam(DataExpression.FORMAT_TYPE,
+                                                                               
new StringIdentifier(DataExpression.FORMAT_TYPE_VALUE_CSV, source.getFilename(),
+                                                                               
                blp, bcp, elp, ecp));
                                                        } else if 
(metaData.getOutputInfo() == OutputInfo.TextCellOutputInfo) {
-                                                               
exp.addVarParam(DataExpression.FORMAT_TYPE, new StringIdentifier(
-                                                                               
DataExpression.FORMAT_TYPE_VALUE_TEXT, source.getFilename(), blp, bcp, elp, 
ecp));
+                                                               
exp.addVarParam(DataExpression.FORMAT_TYPE,
+                                                                               
new StringIdentifier(DataExpression.FORMAT_TYPE_VALUE_TEXT,
+                                                                               
                source.getFilename(), blp, bcp, elp, ecp));
                                                        } else if 
(metaData.getOutputInfo() == OutputInfo.BinaryBlockOutputInfo) {
-                                                               exp.addVarParam(
-                                                                               
DataExpression.ROWBLOCKCOUNTPARAM,
-                                                                               
new IntIdentifier(mo.getNumRowsPerBlock(), source.getFilename(), blp, bcp, elp, 
ecp));
-                                                               
exp.addVarParam(DataExpression.COLUMNBLOCKCOUNTPARAM,
-                                                                               
new IntIdentifier(mo.getNumColumnsPerBlock(), source.getFilename(), blp, bcp, 
elp,
-                                                                               
                ecp));
-                                                               
exp.addVarParam(DataExpression.FORMAT_TYPE, new StringIdentifier(
-                                                                               
DataExpression.FORMAT_TYPE_VALUE_BINARY, source.getFilename(), blp, bcp, elp, 
ecp));
+                                                               
exp.addVarParam(DataExpression.ROWBLOCKCOUNTPARAM, new IntIdentifier(
+                                                                               
mo.getNumRowsPerBlock(), source.getFilename(), blp, bcp, elp, ecp));
+                                                               
exp.addVarParam(DataExpression.COLUMNBLOCKCOUNTPARAM, new IntIdentifier(
+                                                                               
mo.getNumColumnsPerBlock(), source.getFilename(), blp, bcp, elp, ecp));
+                                                               
exp.addVarParam(DataExpression.FORMAT_TYPE,
+                                                                               
new StringIdentifier(DataExpression.FORMAT_TYPE_VALUE_BINARY,
+                                                                               
                source.getFilename(), blp, bcp, elp, ecp));
                                                        } else {
                                                                throw new 
MLContextException("Unsupported format through MLContext");
                                                        }
@@ -480,7 +488,7 @@ public class MLContext {
                public ArrayList<Instruction> 
performCleanupAfterRecompilation(ArrayList<Instruction> instructions) {
                        if (executingScript == null || 
executingScript.getOutputVariables() == null)
                                return instructions;
-                       
+
                        Set<String> outputVariableNames = 
executingScript.getOutputVariables();
                        return 
JMLCUtils.cleanupRuntimeInstructions(instructions, 
outputVariableNames.toArray(new String[0]));
                }
@@ -520,9 +528,11 @@ public class MLContext {
        }
 
        /**
-        * Sets the maximum number of heavy hitters that are printed out as 
part of the statistics.
+        * Sets the maximum number of heavy hitters that are printed out as 
part of
+        * the statistics.
         *
-        * @param maxHeavyHitters maximum number of heavy hitters to print
+        * @param maxHeavyHitters
+        *            maximum number of heavy hitters to print
         */
        public void setStatisticsMaxHeavyHitters(int maxHeavyHitters) {
                DMLScript.STATISTICS_COUNT = maxHeavyHitters;
@@ -575,20 +585,50 @@ public class MLContext {
 
                // cleanup scratch space and buffer pool
                try {
-                       DMLScript.cleanupHadoopExecution(
-                                       ConfigurationManager.getDMLConfig());
-               }
-               catch(Exception ex) {
+                       
DMLScript.cleanupHadoopExecution(ConfigurationManager.getDMLConfig());
+               } catch (Exception ex) {
                        throw new MLContextException("Failed to cleanup working 
directories.", ex);
                }
-               
+
                // clear local status, but do not stop sc as it
                // may be used or stopped externally
-               for (Script script : scripts.values()) 
+               for (Script script : scripts.values())
                        script.clearAll();
                scripts.clear();
                scriptHistoryStrings.clear();
                resetConfig();
                sc = null;
        }
+
+       /**
+        * Obtain information about the project such as version and build time 
from
+        * the manifest in the SystemML jar file.
+        * 
+        * @return information about the project
+        */
+       public ProjectInfo info() {
+               if (projectInfo == null) {
+                       projectInfo = new ProjectInfo();
+               }
+               return projectInfo;
+       }
+
+       /**
+        * Obtain the SystemML version number.
+        * 
+        * @return the SystemML version number
+        */
+       public String version() {
+               return info().version();
+       }
+
+       /**
+        * Obtain the SystemML jar file build time.
+        * 
+        * @return the SystemML jar file build time
+        */
+       public String buildTime() {
+               return info().buildTime();
+       }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/bcd5d96f/src/main/java/org/apache/sysml/api/mlcontext/ProjectInfo.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/api/mlcontext/ProjectInfo.java 
b/src/main/java/org/apache/sysml/api/mlcontext/ProjectInfo.java
new file mode 100644
index 0000000..7d5a6c1
--- /dev/null
+++ b/src/main/java/org/apache/sysml/api/mlcontext/ProjectInfo.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sysml.api.mlcontext;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.jar.Attributes;
+import java.util.jar.Attributes.Name;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+/**
+ * Obtains information that is stored in the manifest when the SystemML jar is
+ * built.
+ *
+ */
+public class ProjectInfo {
+
+       SortedMap<String, String> properties = null;
+
+       public ProjectInfo() {
+               JarFile systemMlJar = null;
+               try {
+                       String path = 
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
+                       systemMlJar = new JarFile(path);
+                       Manifest manifest = systemMlJar.getManifest();
+                       Attributes mainAttributes = 
manifest.getMainAttributes();
+                       properties = new TreeMap<String, String>();
+                       for (Object key : mainAttributes.keySet()) {
+                               String value = mainAttributes.getValue((Name) 
key);
+                               properties.put(key.toString(), value);
+                       }
+               } catch (Exception e) {
+                       throw new MLContextException("Error trying to read from 
manifest in SystemML jar file", e);
+               } finally {
+                       if (systemMlJar != null) {
+                               try {
+                                       systemMlJar.close();
+                               } catch (IOException e) {
+                                       throw new MLContextException("Error 
closing SystemML jar file", e);
+                               }
+                       }
+               }
+       }
+
+       @Override
+       public String toString() {
+               StringBuffer sb = new StringBuffer();
+               Set<String> keySet = properties.keySet();
+               for (String key : keySet) {
+                       sb.append(key + ": " + properties.get(key) + "\n");
+               }
+               return sb.toString();
+       }
+
+       /**
+        * Obtain a manifest property value based on the key.
+        * 
+        * @param key
+        *            the property key
+        * @return the property value
+        */
+       public String property(String key) {
+               return properties.get(key);
+       }
+
+       /**
+        * Obtain the project version from the manifest.
+        * 
+        * @return the project version
+        */
+       public String version() {
+               return property("Version");
+       }
+
+       /**
+        * Object the artifact build time from the manifest.
+        * 
+        * @return the artifact build time
+        */
+       public String buildTime() {
+               return property("Build-Time");
+       }
+
+       /**
+        * Obtain all the properties from the manifest as a sorted map.
+        * 
+        * @return the manifest properties as a sorted map
+        */
+       public SortedMap<String, String> properties() {
+               return properties;
+       }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/bcd5d96f/src/main/python/systemml/mlcontext.py
----------------------------------------------------------------------
diff --git a/src/main/python/systemml/mlcontext.py 
b/src/main/python/systemml/mlcontext.py
index 4f769d5..babfe5b 100644
--- a/src/main/python/systemml/mlcontext.py
+++ b/src/main/python/systemml/mlcontext.py
@@ -342,3 +342,15 @@ class MLContext(object):
         """
         self._ml.setExplainLevel(explainLevel)
         return self
+
+    def version(self):
+        """Display the project version."""
+        return self._ml.version()
+
+    def buildTime(self):
+        """Display the project build time."""
+        return self._ml.buildTime()
+
+    def info(self):
+        """Display the project information."""
+        return self._ml.info().toString()

Reply via email to