]> Creatis software - bbtk.git/blobdiff - kernel/appli/bbpConfigurator/bbpConfigurator.cpp
#3082 BBTK Bug New Normal - not compiling in CentOS 2.6
[bbtk.git] / kernel / appli / bbpConfigurator / bbpConfigurator.cpp
index 50c41635bdc86f77e91ce1c588888de55568bcb0..381ae326cc84917293574b9a41556181f9421359 100644 (file)
 #include <map>
 #include <algorithm>
 #include "boost/filesystem.hpp"
-//#include "boost/filesystem/operations.hpp"
-//#include "boost/filesystem/path.hpp"
 #include "bbtkBBPInterpreter.h"
 
 namespace bf = boost::filesystem;
+
 typedef std::vector<std::vector<int> > Graph;
 
-typedef std::set<std::string> Dependency;
-typedef std::vector<Dependency> Dependencies;
-typedef std::vector<std::string> Boxes;
+typedef std::set<std::string> Dependencies;
+typedef std::vector<Dependencies> DependenciesVector;
+typedef std::vector<std::string> BoxesVector;
+
+std::vector<bf::path> getFileList(const std::string& path);
 
 bool isCycle(const Graph& g);
 bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v);
@@ -54,7 +55,7 @@ int main(int argc, char **argv)
   // Check arguments
   if (argc != 4)
   {
-    std::cout << "usage : bbConfigurator <path_to_bbs> <package_name> <output_path>" << std::endl;
+    std::cout << "bbpConfigurator usage: bbConfigurator <path_to_bbs> <package_name> <output_path>" << std::endl;
     return 1;
   }
 
@@ -62,119 +63,151 @@ int main(int argc, char **argv)
   std::string package_name(argv[2]);
   std::string path_out(argv[3]);
   
-  // Get bbs files in path_bbs
-  std::vector<bf::path> files;
+  std::cout << "bbpConfigurator launched with bbs path: '" << path_bbs <<
+      "', package: '" << package_name <<
+      "', output path: '" << path_out << "'." << std::endl;
 
-  bf::path pth(path_bbs.c_str());
-  if(bf::exists(pth) && bf::is_directory(pth))
-  {
-    bf::directory_iterator end_itr;
-    for(bf::directory_iterator itr(pth); itr != end_itr; ++itr)
-    {
-      if(!is_directory(itr->status()))
-      {
-        std::string nm(itr->path().filename().string());
-        if(nm.substr(nm.size()-4) == ".bbs")
-        {
-          //std::cout << itr->path().filename().string() << std::endl;
-          files.push_back(itr->path());
-        }
-      }
-    }
-  }
-  else
+  // Get bbs files in path_bbs
+  std::vector<bf::path> files = getFileList(path_bbs);
+  if(files.size() == 0)
   {
-    std::cout<< "the path to the bbs's should be a folder and not a file.";
-    return 1;
-  }
+    std::cout << "bbpConfigurator: No files to check in bbs path. "
+        "An empty bbp will be created for the package '" << package_name << "'." << std::endl;
 
-  // Order files by name
-  for (int i = 0; i < (int)files.size()-1; ++i) {
-    for (int j = i+1; j < (int)files.size(); ++j) {
-      if(files[j].filename().string() < files[i].filename().string())
-      {
-        bf::path tmp = files[i];
-        files[i] = files[j];
-        files[j] = tmp;
-      }
-    }
+    // Write results to bbp file
+    #ifdef WIN32
+      std::string fname = path_out + "\\" + package_name + ".bbp";
+    #else
+      std::string fname = path_out + "/" + package_name + ".bbp";
+    #endif
+
+      std::ofstream out(fname.c_str());
+      out << "#-----------------------------------------" << std::endl;
+      out << "# Include script for bbtk package '" << package_name << "'" << std::endl;
+      out << "# Automatically generated by bbpConfigurator" << std::endl;
+      out << "#-----------------------------------------" << std::endl;
+      out << "load "<< package_name << std::endl;
+      out << "#-----------------------------------------" << std::endl;
+      out << "package "<< package_name << std::endl;
+      out << "#-----------------------------------------" << std::endl;
+      out << "endpackage" << std::endl;
+      out << "#-- EOF ----------------------------------" << std::endl;
+
+      out.close();
+
+
+    return 0;
   }
 
+
   // Order files by dependencies
-  //  Get Dependencies and Box Names
-  Dependencies ds;
-  Boxes bs;
+  //  Get DependenciesVector and Box Names
+  DependenciesVector deps;
+  BoxesVector boxs;
 
 
-  for (int i = 0; i < (int)files.size(); ++i) {
+  for (int i = 0; i < (int)files.size(); ++i) 
+  {
     bbtk::BBPInterpreter::Pointer I = bbtk::BBPInterpreter::New();
     I->InterpretFile(files[i].string());
-    bs.push_back( ((bbtk::BBPInterpreter*)(I.get()))->boxName );
-    ds.push_back( ((bbtk::BBPInterpreter*)(I.get()))->dependencies );
-
-    //print box name and dependencies
-/*    std::cout << ((bbtk::BBPInterpreter*)(I.get()))->boxName << ": ";
-    for(
-      dep::iterator it = ((bbtk::BBPInterpreter*)(I.get()))->dependencies.begin();
-      it != ((bbtk::BBPInterpreter*)(I.get()))->dependencies.end();
-      it++) {
-      std::cout << *it << ", ";
-    }
-    std::cout << std::endl;
-*/
-  }
+
+
+    boxs.push_back( ((bbtk::BBPInterpreter*)(I.get()))->boxName );
+
+// EED 2016/02/19
+//    deps.push_back( ((bbtk::BBPInterpreter*)(I.get()))->dependencies );
+ //-->  Cleanning string package:boxname -> boxname
+    std::set<std::string> tmpDependencies1=  ((bbtk::BBPInterpreter*)(I.get()))->dependencies  ;
+    std::set<std::string> tmpDependencies2;
+    std::set<std::string>::const_iterator sit(tmpDependencies1.begin()), send(tmpDependencies1.end()); 
+       int pos;
+       std::string tmpStr;
+//    std::cout << ((bbtk::BBPInterpreter*)(I.get()))->boxName << ": ";
+    for(;sit!=send;++sit)
+    { 
+               pos=(*sit).find(":");   
+               if (pos== std::string::npos ) 
+               { 
+                       tmpDependencies2.insert( *sit );
+//                     std::cout << *sit << ' ';
+               } else {
+                       tmpStr=(*sit).substr(pos+1);
+                       tmpDependencies2.insert( tmpStr );
+//                     std::cout << tmpStr << ' ';
+               } // if
+
+    } // for
+//    std::cout << std::endl;
+    deps.push_back( tmpDependencies2 );
+ //<--
+
+
+
+//    //print box name and dependencies  OLD VERSION
+//    std::cout << ((bbtk::BBPInterpreter*)(I.get()))->boxName << ": ";
+//   for(
+//      Dependencies::iterator it = ((bbtk::BBPInterpreter*)(I.get()))->dependencies.begin();
+//      it != ((bbtk::BBPInterpreter*)(I.get()))->dependencies.end();
+//      it++) {
+//      std::cout << *it << ", ";
+//    }
+//    std::cout << std::endl;
+
+  } // for i
+
   // Only keep dependencies from package
-  Dependency boxesNamesSet(bs.begin(), bs.end());
+  Dependencies boxNamesSet(boxs.begin(), boxs.end());
 
-  //std::cout << "after: " << std::endl;
-  for (Dependencies::iterator it = ds.begin(); it != ds.end(); it++) {
-    Boxes tmp;
-    std::set_intersection(it->begin(), it->end(), boxesNamesSet.begin(), boxesNamesSet.end(),std::back_inserter(tmp));
-    Dependency tmp1(tmp.begin(),tmp.end());
+//std::cout << "after: " << std::endl;
+  for (DependenciesVector::iterator it = deps.begin(); it != deps.end(); it++) 
+  {
+    BoxesVector tmp;
+    std::set_intersection(it->begin(), it->end(), boxNamesSet.begin(), boxNamesSet.end(),std::back_inserter(tmp));
+    Dependencies tmp1(tmp.begin(),tmp.end());
     it->swap( tmp1 );
-    //print clean dependencies
-/*    for(
-      dep::iterator it1 = it->begin();
-      it1 != it->end();
-      it1++) {
-      std::cout << *it1 << ", ";
-    }
-    std::cout << std::endl;
-*/
+//    //print clean dependencies
+//    for( Dependencies::iterator it1 = it->begin();  it1 != it->end(); it1++) 
+//    {
+//      std::cout << *it1 << ", ";
+//    }
+//    std::cout << std::endl;
+
   }
 
-  // Check there are no cycles in graph
-  std::vector<std::vector<int> > g(bs.size(), std::vector<int>());
+  // Create dependencies graph
+  std::vector<std::vector<int> > g(boxs.size(), std::vector<int>());
   std::map<std::string, int> idxs;
 
 
-  for (int i = 0; i < (int)bs.size(); ++i)
+  for (int i = 0; i < (int)boxs.size(); ++i)
   {
-    idxs[bs[i]] = i;
+    idxs[boxs[i]] = i;
   }
 
   int boxit = 0;
-  for (Dependencies::iterator dit = ds.begin(); dit != ds.end(); dit++, boxit++)
+  for (DependenciesVector::iterator dit = deps.begin(); dit != deps.end(); dit++, boxit++)
   {
-    for (Dependency::iterator ddit = dit->begin(); ddit != dit->end(); ddit++)
+    for (Dependencies::iterator ddit = dit->begin(); ddit != dit->end(); ddit++)
     {
       g[boxit].push_back(idxs[*ddit]);
     }
   }
 
+  // Check there are no cycles in graph
   if(isCycle(g))
   {
-    std::cout << "there are dependency cycles, please check your scripts." << std::endl;
+    std::cout << "bbpConfigurator: There are dependency cycles, please check your scripts in '" <<
+        path_bbs << "'. No bbp file created." << std::endl;
     return 2;
   }
   else
   {
-    std::cout << "no cycles, we keep going." << std::endl;
-    std::vector<int> dep_priority(bs.size(), -1);
-    setPriorities(g, dep_priority);
-//    for (int i = 0; i < (int)dep_priority.size(); i++)
+    std::cout << "bbpConfigurator: No cycles detected in dependency graph." << std::endl;
+    std::vector<int> priorities(boxs.size(), -1);
+    setPriorities(g, priorities);
+//    for (int i = 0; i < (int)priorities.size(); i++)
 //    {
-//      std::cout << dep_priority[i] << " ";
+//      std::cout << priorities[i] << " ";
 //    }
 //    std::cout << std::endl;
 
@@ -204,18 +237,27 @@ int main(int argc, char **argv)
   //    out << "#-----------------------------------------" << std::endl;
   //  }
 
-    // for each priority level print scripts in that level.
-    int m_priority = 0;
-    for (int i = 0; i < (int)dep_priority.size(); i++)
-      m_priority = std::max(m_priority, dep_priority[i]);
+    // find max priority level
+    int mx_priority = 0;
+    for (int i = 0; i < (int)priorities.size(); i++)
+    {
+      mx_priority = std::max(mx_priority, priorities[i]);
+    }
 
-    for (int i = 0; i <= m_priority; i++)
+    // for each priority level print scripts in that level.
+    for (int i = 0; i <= mx_priority; i++)
     {
-      for (int j = 0; j < (int)dep_priority.size(); j++)
+      for (int j = 0; j < (int)priorities.size(); j++)
       {
-        if(dep_priority[j] == i)
+        if(priorities[j] == i)
         {
+//EED 2017-03-28
+#if BOOST_MAJOR_VERSION <= 1 || BOOST_MINOR_VERSION <=41 
+          out << "include " << package_name << "/boxes/" << files[j].filename() << std::endl;
+#else
           out << "include " << package_name << "/boxes/" << files[j].filename().string() << std::endl;
+#endif
+
           out << "#-----------------------------------------" << std::endl;
         }
       }
@@ -224,6 +266,8 @@ int main(int argc, char **argv)
     out << "#-- EOF ----------------------------------" << std::endl;
   
     out.close();
+
+    std::cout << "bbpConfigurator: bbp file created at '" << fname << "'." << std::endl;
   }
   return 0;
 }
@@ -245,6 +289,7 @@ bool isCycle(const Graph& g)
 
 //==========================================================================
 
+// dfs search to check cycles.
 bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v)
 {
 
@@ -252,7 +297,7 @@ bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v)
   for(int dit = 0; dit < (int)g[i].size(); dit++)
   {
     int d = g[i][dit];
-    if(d < 0 || d >= (int)g[i].size() || v[d])
+    if(d < 0 || d >= (int)g.size() || v[d])
     {
       //std::cout << "Checking " << i << " dependency " << dit << "=" << d << std::endl;
       return true;
@@ -260,6 +305,7 @@ bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v)
     if(checkCycle(g,d,v))
       return true;
   }
+  v[i] = false;
   return false;
 }
 
@@ -276,6 +322,8 @@ void setPriorities(const Graph& g, std::vector<int>& p)
 }
 
 //==========================================================================
+
+// dfs search to find dependencies
 void setPriority(const Graph& g, const int i, std::vector<int>& p)
 {
   int pi = -1;
@@ -286,3 +334,60 @@ void setPriority(const Graph& g, const int i, std::vector<int>& p)
   }
   p[i]=pi+1;
 }
+
+//==========================================================================
+
+std::vector<bf::path> getFileList(const std::string& path)
+{
+  std::vector<bf::path> files;
+
+  bf::path pth(path.c_str());
+  if(bf::exists(pth) && bf::is_directory(pth))
+  {
+    bf::directory_iterator end_itr;
+    bf::directory_iterator itr(pth);
+    for(itr; itr != end_itr; ++itr)
+    {
+      if(!is_directory(itr->status()))
+      {
+//EED 2017-03-28
+#if BOOST_MAJOR_VERSION <= 1 || BOOST_MINOR_VERSION <=41 
+        std::string nm(itr->path().filename());
+#else
+        std::string nm(itr->path().filename().string());
+#endif
+        if(nm.substr(nm.size()-4) == ".bbs")
+        {
+          //std::cout << itr->path().filename().string() << std::endl;
+          files.push_back(itr->path());
+        }
+      }
+    }
+  }
+  else
+  {
+    std::cout<< "bbpConfigurator: The path to the bbs's doesn't exist or is not a folder. ('" << path << "')" << std::endl;
+    return files;
+  }
+
+  // Order files by name
+  for (int i = 0; i < (int)files.size()-1; ++i) {
+    for (int j = i+1; j < (int)files.size(); ++j) {
+
+//EED 2017-03-28
+#if BOOST_MAJOR_VERSION <= 1 || BOOST_MINOR_VERSION <=41 
+      if(files[j].filename() < files[i].filename())
+#else
+      if(files[j].filename().string() < files[i].filename().string())
+#endif
+
+      {
+        bf::path tmp = files[i];
+        files[i] = files[j];
+        files[j] = tmp;
+      }
+    }
+  }
+
+  return files;
+}