From 13bdd4083c144f96fcfd2452f143e64697926fc5 Mon Sep 17 00:00:00 2001
From: "Joseph F. Boudreau" <boudreau@pitt.edu>
Date: Thu, 24 Aug 2023 18:09:24 +0200
Subject: [PATCH] Added information from user repository

---
 GeoModelTools/GMCAT/CMakeLists.txt          |   2 +-
 GeoModelTools/GMCAT/src/gmcat.cxx           | 121 +-----------
 GeoModelTools/GMCAT/src/publishMetaData.cpp | 202 ++++++++++++++++++++
 3 files changed, 212 insertions(+), 113 deletions(-)
 create mode 100644 GeoModelTools/GMCAT/src/publishMetaData.cpp

diff --git a/GeoModelTools/GMCAT/CMakeLists.txt b/GeoModelTools/GMCAT/CMakeLists.txt
index f1e0fc257..f6cc32c52 100644
--- a/GeoModelTools/GMCAT/CMakeLists.txt
+++ b/GeoModelTools/GMCAT/CMakeLists.txt
@@ -1,7 +1,7 @@
 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 
 # Declare the package's executable.
-add_executable( gmcat src/gmcat.cxx )
+add_executable( gmcat src/gmcat.cxx src/publishMetaData.cpp)
 target_link_libraries( gmcat PRIVATE GeoModelCore::GeoModelKernel
     GeoModelIO::GeoModelRead GeoModelIO::GeoModelWrite
     GeoModelIO::GeoModelDBManager )
diff --git a/GeoModelTools/GMCAT/src/gmcat.cxx b/GeoModelTools/GMCAT/src/gmcat.cxx
index cd32cf0d6..a42141ff2 100644
--- a/GeoModelTools/GMCAT/src/gmcat.cxx
+++ b/GeoModelTools/GMCAT/src/gmcat.cxx
@@ -24,81 +24,23 @@
 #include <cstdio>
 #include <unistd.h>
 #include <stdlib.h>
-#include <sys/utsname.h>
 
+#define SYSTEM_OF_UNITS GeoModelKernelUnits // --> 'GeoModelKernelUnits::cm'
 #ifdef __APPLE__
 const std::string shared_obj_extension=".dylib";
 #else
 const std::string shared_obj_extension=".so";
 #endif
 
-#define SYSTEM_OF_UNITS GeoModelKernelUnits // --> 'GeoModelKernelUnits::cm'
-#define STR_VALUE(arg) #arg
-#define STR_NAME(name) STR_VALUE(name)
-
-std::string getCommandOutput(const std::string & cmd, bool firstLineOnly=false)
-{
-  std::string response;
-  // Ceci n'est pas une pipe:
-  FILE *ceci=popen(cmd.c_str(),"r");
-  if (ceci) {
-    static const int MSGSIZE=1024;
-    char buff[MSGSIZE];
-    while (fgets(buff,MSGSIZE,ceci)!=NULL) {
-      response+=std::string(buff);
-      if (firstLineOnly) break;
-    }
-    pclose(ceci);
-  }
-  return response;
-}
-
 
+void publishMetaData( GMDBManager & db,
+		      std::vector<std::string> &inputFiles,
+		      std::vector<std::string> &inputPlugins,
+		      std::string              &outputFile);
 
 int main(int argc, char ** argv) {
 
 
-  struct Metadata {
-    std::string dateString=getCommandOutput("date -Im");
-    std::string username=getlogin();
-    std::string hostname;
-    std::string os;
-    std::string gmversion=STR_NAME(GMVERSION);
-    std::string geoModelDataBranch="Undefined";      // or overwritten below
-    std::string gmdataIsClean     ="Not applicable"; // or overwritten below
-    std::string gmdataCommitHash  ="Undefined";      // or overwritten below
-  } metadata;
-
-  //  metadata.dateString=getCommandOutput("date -Im");
-  //  metadata.username=getlogin();
-  char buff[1024];
-  gethostname(buff,1024);
-  metadata.hostname=buff;
- 
-  utsname uts;
-  uname (&uts);
-  metadata.os=std::string(uts.sysname)+  "-" + std::string(uts.machine);
-
-  char *geomodel_xml_dir=getenv("GEOMODEL_XML_DIR");
-  if (geomodel_xml_dir)  {
-    {
-      metadata.geoModelDataBranch=getCommandOutput("git -C "+ std::string(geomodel_xml_dir) + " rev-parse --abbrev-ref HEAD");
-      std::string shortGitStatus=getCommandOutput("git -C "+ std::string(geomodel_xml_dir) + " status -s ");
-      if (shortGitStatus!="") {
-	metadata.gmdataIsClean="no";
-      }
-      else {
-	std::string synchedToOrigin=getCommandOutput("git -C "+ std::string(geomodel_xml_dir) + " diff origin/"+metadata.geoModelDataBranch,true);
-	if (synchedToOrigin!="") {
-	  metadata.gmdataIsClean="no";
-	}
-	else {
-	  metadata.gmdataIsClean="yes";
-	  metadata.gmdataCommitHash=getCommandOutput("git -C " + std::string(geomodel_xml_dir) + " log -1 --format=format:\"%H\"");
-	}
-      }
-    }
-  }
 
   //
   // Usage message:
@@ -271,64 +213,19 @@ int main(int argc, char ** argv) {
     std::cerr << "gmcat -- Error opening the output file: " << outputFile << std::endl;
     return 7;
   }
-  //
-  // Fill the header file with metadata
-  //
-  std::vector<std::string>                                                   gmcatColNames={"Date",
-											    "GeoModelDataBranch",
-											    "Username",
-											    "Hostname",
-											    "OS",
-											    "GeoModelVersion",
-											    "GeoModelDataIsClean",
-											    "GeoModelDataCommitHash"
-  };
-  std::vector<std::string>                                                   gmcatColTypes={"STRING",
-											    "STRING" ,
-											    "STRING",
-											    "STRING",
-											    "STRING",
-											    "STRING",
-											    "STRING",
-											    "STRING"
-  };
-  std::vector<std::vector<std::variant<int,long,float,double,std::string>>>  gmcatData    ={{
-      metadata.dateString,
-      metadata.geoModelDataBranch,
-      metadata.username,
-      metadata.hostname,
-      metadata.os,
-      metadata.gmversion,
-      metadata.gmdataIsClean,
-      metadata.gmdataCommitHash
-    }};
 
-  unsigned int pcounter(0);
-  for (std::string plugin : inputPlugins) {
-    gmcatColNames.push_back("P"+std::to_string(pcounter++));
-    gmcatColTypes.push_back("STRING");
-    gmcatData[0].push_back((plugin));
-  }
-  unsigned int fcounter(0);
-  for (std::string file : inputFiles) {
-    gmcatColNames.push_back("F"+std::to_string(fcounter++));
-    gmcatColTypes.push_back("STRING");
-    gmcatData[0].push_back(file);
-  }
-  
-  db.createCustomTable("AAHEADER", gmcatColNames,gmcatColTypes,gmcatData); 
   GeoModelIO::WriteGeoModel dumpGeoModelGraph(db);
   world->exec(&dumpGeoModelGraph);
 
-
   if (vecPluginsPublishers.size() > 0) {
-      dumpGeoModelGraph.saveToDB(vecPluginsPublishers);
+    dumpGeoModelGraph.saveToDB(vecPluginsPublishers);
   } else {
-      dumpGeoModelGraph.saveToDB();
+    dumpGeoModelGraph.saveToDB();
   }
 
   world->unref();
 
-
+  publishMetaData(db,inputFiles,inputPlugins,outputFile);
+  
   return 0;
 }
diff --git a/GeoModelTools/GMCAT/src/publishMetaData.cpp b/GeoModelTools/GMCAT/src/publishMetaData.cpp
new file mode 100644
index 000000000..ea04e22c0
--- /dev/null
+++ b/GeoModelTools/GMCAT/src/publishMetaData.cpp
@@ -0,0 +1,202 @@
+#include "GeoModelDBManager/GMDBManager.h"
+#include <string>
+#include <iostream>
+#include <unistd.h>
+#include <cstdio>
+#include <vector>
+#include <algorithm>
+#include <sys/utsname.h>
+#define STR_VALUE(arg) #arg
+#define STR_NAME(name) STR_VALUE(name)
+
+std::string getCommandOutput(const std::string & cmd, bool firstLineOnly=false)
+{
+  std::string response;
+  // Ceci n'est pas une pipe:
+  FILE *ceci=popen(cmd.c_str(),"r");
+  if (ceci) {
+    static const int MSGSIZE=1024;
+    char buff[MSGSIZE];
+    while (fgets(buff,MSGSIZE,ceci)!=NULL) {
+      response+=std::string(buff);
+      if (firstLineOnly) break;
+    }
+    pclose(ceci);
+  }
+  return response;
+}
+
+void publishMetaData( GMDBManager & db,
+		      std::vector<std::string> &inputFiles,
+		      std::vector<std::string> &inputPlugins,
+		      std::string              &outputFile) {
+
+  struct Metadata {
+    std::string dateString=getCommandOutput("date -Im");
+    std::string username=getlogin();
+    std::string hostname;
+    std::string os;
+    std::string wd=get_current_dir_name();
+    std::string gmversion=STR_NAME(GMVERSION);
+    std::string outputFile;
+    std::string geoModelDataBranch="Undefined";      // or overwritten below
+    std::string gmdataIsClean     ="Not applicable"; // or overwritten below
+    std::string gmdataCommitHash  ="Undefined";      // or overwritten below
+  } metadata;
+
+  struct XtraMetadata {
+    std::string repo        ="Undefined";      // or overwritten below
+    std::string branch      ="Undefined";      // or overwritten below
+    std::string isClean     ="Not applicable"; // or overwritten below
+    std::string commitHash  ="Undefined";      // or overwritten below
+  } xtraMetadata;
+  
+  metadata.outputFile=outputFile;
+  
+  char buff[1024];
+  gethostname(buff,1024);
+  metadata.hostname=buff;
+ 
+  utsname uts;
+  uname (&uts);
+  metadata.os=std::string(uts.sysname)+  "-" + std::string(uts.machine);
+
+  char *geomodel_xml_dir=getenv("GEOMODEL_XML_DIR");
+  if (geomodel_xml_dir)  {
+    {
+      metadata.geoModelDataBranch=getCommandOutput("git -C "+ std::string(geomodel_xml_dir) + " rev-parse --abbrev-ref HEAD");
+      std::string shortGitStatus=getCommandOutput("git -C "+ std::string(geomodel_xml_dir) + " status -s ");
+      if (shortGitStatus!="") {
+	metadata.gmdataIsClean="no";
+      }
+      else {
+	std::string synchedToOrigin=getCommandOutput("git -C "+ std::string(geomodel_xml_dir) + " diff origin/"+metadata.geoModelDataBranch,true);
+	if (synchedToOrigin!="") {
+	  metadata.gmdataIsClean="no";
+	}
+	else {
+	  metadata.gmdataIsClean="yes";
+	  metadata.gmdataCommitHash=getCommandOutput("git -C " + std::string(geomodel_xml_dir) + " log -1 --format=format:\"%H\"");
+	}
+      }
+    }
+  }
+  std::string isGitManaged=getCommandOutput("git rev-parse --is-inside-work-tree");
+  if (isGitManaged=="true\n") {
+    xtraMetadata.repo=getCommandOutput("git remote get-url origin");
+    xtraMetadata.branch=getCommandOutput("git rev-parse --abbrev-ref HEAD");
+    std::string status=getCommandOutput("git status --porcelain");
+    if (status.find(" M ")!=std::string::npos) {
+      xtraMetadata.isClean="no";
+    }
+    else {
+      std::string synchedToOrigin=getCommandOutput("git diff origin/"+xtraMetadata.branch,true);
+      if (synchedToOrigin!="") {
+	xtraMetadata.isClean="no";
+      }
+      else {
+	xtraMetadata.isClean="yes";
+	xtraMetadata.commitHash=getCommandOutput("git log -1 --format=format:\"%H\"");
+      }
+    }
+  }
+
+  //
+  // Fill the header file with metadata
+  //
+  std::vector<std::string>                                                   gmcatColNames={"Date",
+											    "GeoModelDataBranch",
+											    "Username",
+											    "Hostname",
+											    "OS",
+											    "WorkingDirectory",
+											    "GeoModelVersion",
+											    "OutputFile",
+											    "GeoModelDataIsClean",
+											    "GeoModelDataCommitHash"
+  };
+  std::vector<std::string>                                                   gmcatColTypes={"STRING",
+											    "STRING" ,
+											    "STRING",
+											    "STRING",
+											    "STRING",
+											    "STRING",
+											    "STRING",
+											    "STRING",
+											    "STRING",
+											    "STRING"
+  };
+  
+  // Strip extraneous \n
+  for (std::string * s : {
+      &metadata.geoModelDataBranch,
+      &metadata.dateString }) { 
+    s->erase(std::remove(s->begin(), s->end(), '\n'), s->end());
+    std::cout << s << std::endl;
+  }
+	
+	
+  
+  std::vector<std::vector<std::variant<int,long,float,double,std::string>>>  gmcatData    ={{
+      metadata.dateString,
+      metadata.geoModelDataBranch,
+      metadata.username,
+      metadata.hostname,
+      metadata.os,
+      metadata.wd,
+      metadata.gmversion,
+      metadata.outputFile,
+      metadata.gmdataIsClean,
+      metadata.gmdataCommitHash
+    }};
+
+  unsigned int pcounter(0);
+  for (std::string plugin : inputPlugins) {
+    gmcatColNames.push_back("P"+std::to_string(pcounter++));
+    gmcatColTypes.push_back("STRING");
+    gmcatData[0].push_back((plugin));
+  }
+  unsigned int fcounter(0);
+  for (std::string file : inputFiles) {
+    gmcatColNames.push_back("F"+std::to_string(fcounter++));
+    gmcatColTypes.push_back("STRING");
+    gmcatData[0].push_back(file);
+  }
+  
+  // Strip extraneous \n
+  for (std::string * s : {
+      &xtraMetadata.repo,
+      &xtraMetadata.branch }) { 
+    s->erase(std::remove(s->begin(), s->end(), '\n'), s->end());
+    std::cout << s << std::endl;
+  }
+	
+
+
+  
+  if (xtraMetadata.branch!="Undefined") {
+    std::vector<std::string>                                                   xtraColNames={
+      "UserCodeGitRepository",
+      "UserCodeGitBranch",
+      "UserCodeRepoIsClean",
+      "UserCodeRepoCommitHash" };
+    std::vector<std::string>                                                   xtraColTypes={"STRING",
+											     "STRING",
+											     "STRING",
+											     "STRING"};
+    std::vector<std::vector<std::variant<int,long,float,double,std::string>>>  xtraData    ={{
+	xtraMetadata.repo,
+	xtraMetadata.branch,
+	xtraMetadata.isClean,
+	xtraMetadata.commitHash
+    }};    
+    using std::begin, std::end;
+    gmcatColNames.insert(end(gmcatColNames), begin(xtraColNames), end(xtraColNames));
+    gmcatColTypes.insert(end(gmcatColTypes), begin(xtraColTypes), end(xtraColTypes));
+    gmcatData[0].insert(end(gmcatData[0]), begin(xtraData[0]), end(xtraData[0]));
+    db.createCustomTable("AAHEADER", gmcatColNames,gmcatColTypes,gmcatData);
+  }
+  else {
+    db.createCustomTable("AAHEADER", gmcatColNames,gmcatColTypes,gmcatData); 
+  }
+}
-- 
GitLab