From: Daniel Gonzalez Date: Thu, 20 Jun 2013 11:00:47 +0000 (+0200) Subject: Feature #2042 bbpConfigurator X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=9ca9baeef03df0bfaea871676ea7f8566e8124e7;p=bbtk.git Feature #2042 bbpConfigurator This application should replace the macro BBTK_CREATE_PACKAGE_INCLUDE_SCRIPT in order to organize the includes so the dependecies are taken into account. - dependency numbers fixed. - Now it checks dependency branches separately (they could have a common parent script) --- diff --git a/kernel/appli/bbpConfigurator/bbpConfigurator.cpp b/kernel/appli/bbpConfigurator/bbpConfigurator.cpp index 50c4163..8e151da 100644 --- a/kernel/appli/bbpConfigurator/bbpConfigurator.cpp +++ b/kernel/appli/bbpConfigurator/bbpConfigurator.cpp @@ -32,16 +32,17 @@ #include #include #include "boost/filesystem.hpp" -//#include "boost/filesystem/operations.hpp" -//#include "boost/filesystem/path.hpp" #include "bbtkBBPInterpreter.h" namespace bf = boost::filesystem; + typedef std::vector > Graph; -typedef std::set Dependency; -typedef std::vector Dependencies; -typedef std::vector Boxes; +typedef std::set Dependencies; +typedef std::vector DependenciesVector; +typedef std::vector BoxesVector; + +std::vector getFileList(const std::string& path); bool isCycle(const Graph& g); bool checkCycle(const Graph& g, const int& i, std::vector& v); @@ -63,105 +64,78 @@ int main(int argc, char **argv) std::string path_out(argv[3]); // Get bbs files in path_bbs - std::vector files; - - 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 + std::vector 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 << "No files to check." << std::endl; + return 0; } - // 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; - } - } - } // 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) { 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 ); + boxs.push_back( ((bbtk::BBPInterpreter*)(I.get()))->boxName ); + deps.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; -*/ +// 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; + } + // 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()); + 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; -*/ +// 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 > g(bs.size(), std::vector()); + // Create dependencies graph + std::vector > g(boxs.size(), std::vector()); std::map 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; @@ -170,11 +144,11 @@ int main(int argc, char **argv) else { std::cout << "no cycles, we keep going." << std::endl; - std::vector dep_priority(bs.size(), -1); - setPriorities(g, dep_priority); -// for (int i = 0; i < (int)dep_priority.size(); i++) + std::vector 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,16 +178,17 @@ 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) { out << "include " << package_name << "/boxes/" << files[j].filename().string() << std::endl; out << "#-----------------------------------------" << std::endl; @@ -245,6 +220,7 @@ bool isCycle(const Graph& g) //========================================================================== +// dfs search to check cycles. bool checkCycle(const Graph& g, const int& i, std::vector& v) { @@ -252,7 +228,7 @@ bool checkCycle(const Graph& g, const int& i, std::vector& 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 +236,7 @@ bool checkCycle(const Graph& g, const int& i, std::vector& v) if(checkCycle(g,d,v)) return true; } + v[i] = false; return false; } @@ -276,6 +253,8 @@ void setPriorities(const Graph& g, std::vector& p) } //========================================================================== + +// dfs search to find dependencies void setPriority(const Graph& g, const int i, std::vector& p) { int pi = -1; @@ -286,3 +265,47 @@ void setPriority(const Graph& g, const int i, std::vector& p) } p[i]=pi+1; } + +//========================================================================== + +std::vector getFileList(const std::string& path) +{ + std::vector files; + + bf::path pth(path.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 + { + std::cout<< "the path to the bbs's should be a folder and not a file."; + 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) { + if(files[j].filename().string() < files[i].filename().string()) + { + bf::path tmp = files[i]; + files[i] = files[j]; + files[j] = tmp; + } + } + } + + return files; +}