]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackage.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMPackage.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sant�)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26  */
27
28 /*
29  * modelCDMPackage.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMPackage.h"
36
37 #include <fstream>
38 #include <algorithm>
39
40 #include "creaWx.h"
41 #include "wx/dir.h"
42 #include "CDMUtilities.h"
43
44 modelCDMPackage::modelCDMPackage()
45 {
46 }
47
48 modelCDMPackage::modelCDMPackage(const std::string& path, const int& level)
49 {
50   this->type = wxDIR_DIRS;
51   //Get Package Name
52
53   //TODO: set pathMakeLists for windows
54   std::string pathMakeLists = path + "/CMakeLists.txt";
55
56   std::ifstream confFile;
57   confFile.open((pathMakeLists).c_str());
58
59   std::string word;
60   while(confFile.is_open() && !confFile.eof())
61     {
62       //get sets
63       std::getline(confFile,word,'(');
64       std::vector<std::string> wordBits;
65       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
66
67       if(wordBits[wordBits.size()-1] == "SET")
68         {
69           //get package name
70           std::getline(confFile,word,')');
71           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
72           if(wordBits[0] == "BBTK_PACKAGE_NAME")
73             {
74               word = wordBits[1];
75               for (int i = 2; i < wordBits.size(); i++)
76                 {
77                   word += " " + wordBits[i];
78                 }
79               wordBits.clear();
80               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
81
82               this->namePackage = wordBits[0];
83             }
84           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
85             {
86               word = wordBits[1];
87               for (int i = 2; i < wordBits.size(); i++)
88                 {
89                   word += " " + wordBits[i];
90                 }
91               wordBits.clear();
92               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
93
94               this->authors = wordBits[0];
95             }
96           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
97             {
98               word = wordBits[1];
99               for (int i = 2; i < wordBits.size(); i++)
100                 {
101                   word += " " + wordBits[i];
102                 }
103               wordBits.clear();
104               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
105
106               this->description = wordBits[0];
107             }
108           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
109             {
110               this->version = wordBits[1];
111             }
112           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
113             {
114               this->version += "." + wordBits[1];
115             }
116           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
117             {
118               this->version += "." + wordBits[1];
119             }
120         }
121     }
122
123   std::vector<std::string> words;
124   std::string delimiters;
125   //TODO::fix for windows
126   delimiters = "/";
127   CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
128   this->name = words[words.size()-1];
129   this->level = level;
130   this->path = path;
131
132   //check all folders and files
133   wxDir dir(crea::std2wx((path).c_str()));
134   if (dir.IsOpened())
135     {
136       wxString fileName;
137
138       //folders
139       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
140       while (cont)
141         {
142           std::string stdfileName = crea::wx2std(fileName);
143           //if src, check for black boxes
144           if(stdfileName == "src")
145             {
146               wxDir srcF(crea::std2wx((path + delimiters + "src").c_str()));
147               if (srcF.IsOpened())
148                 {
149                   wxString srcName;
150                   bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
151                   while (innerCont)
152                     {
153                       if(crea::wx2std(srcName.substr(0,2)) == "bb")
154                         {
155                           modelCDMBlackBox* blackbox = new modelCDMBlackBox(crea::wx2std(srcName), path + delimiters + "src", this->level + 1);
156                           this->blackBoxes.push_back(blackbox);
157                           this->children.push_back(blackbox);
158                         }
159                       innerCont = srcF.GetNext(&srcName);
160                     }
161                 }
162             }
163           //if is an unknown folder, create folder
164           else
165             {
166               this->children.push_back(new modelCDMFolder(path + delimiters + stdfileName, this->level + 1));
167             }
168           cont = dir.GetNext(&fileName);
169         }
170
171       //files
172       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
173       while (cont)
174         {
175           std::string stdfileName = crea::wx2std(fileName);
176
177           //if CMakeLists, create CMakeLists
178           if(stdfileName == "CMakeLists.txt")
179             {
180               this->CMakeLists = new modelCDMCMakeListsFile(path + delimiters + stdfileName, this->level + 1);
181               this->children.push_back(this->CMakeLists);
182             }
183           else
184             {
185               this->children.push_back(new modelCDMFile(path + delimiters + stdfileName, this->level + 1));
186             }
187           //if is an unknown file, create file
188           cont = dir.GetNext(&fileName);
189         }
190     }
191   this->SortChildren();
192 }
193
194 modelCDMPackage::~modelCDMPackage()
195 {
196 }
197
198 const std::string& modelCDMPackage::GetNamePackage() const
199 {
200   return this->namePackage;
201 }
202
203 const std::string& modelCDMPackage::GetAuthors() const
204 {
205   return this->authors;
206 }
207
208 const std::string& modelCDMPackage::GetAuthorsEmail() const
209 {
210   return this->authorsEmail;
211 }
212
213 const std::string& modelCDMPackage::GetVersion() const
214 {
215   return this->version;
216 }
217
218 const std::string& modelCDMPackage::GetDescription() const
219 {
220   return this->description;
221 }
222
223 const std::vector<modelCDMBlackBox*>& modelCDMPackage::GetBlackBoxes() const
224 {
225   return this->blackBoxes;
226 }
227
228 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
229 {
230   std::vector<std::string> words;
231   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
232   std::string authorsReal = words[0];
233   for (int i = 1; i < words.size(); i++)
234     {
235       authorsReal += "/" + words[i];
236     }
237
238   std::string line;
239   //opening original cmakelists
240   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
241   if( !in.is_open())
242     {
243       result = new std::string("CMakeLists.txt file failed to open.");
244       return false;
245     }
246   //opening temporal cmakelists
247   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
248   if( !out.is_open())
249     {
250       result = new std::string("CMakeLists.txt.tmp file failed to open.");
251       return false;
252     }
253   //copying contents from original to temporal and making changes
254   while (getline(in, line))
255     {
256       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
257         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
258       out << line << std::endl;
259     }
260   in.close();
261   out.close();
262   //delete old file and rename new file
263   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
264   if(system(renameCommand.c_str()))
265     {
266       result = new std::string("An error occurred while running '" + renameCommand + "'.");
267       return false;
268     }
269
270   this->authors = authorsReal;
271   return true;
272 }
273
274 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
275 {
276   //TODO: implement method
277   return true;
278 }
279
280 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
281 {
282   std::vector<std::string> vers;
283   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
284
285
286   std::string line;
287   //opening original cmakelists
288   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
289   if( !in.is_open())
290     {
291       result = new std::string("CMakeLists.txt file failed to open.");
292       return false;
293     }
294   //opening temporal cmakelists
295   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
296   if( !out.is_open())
297     {
298       result = new std::string("CMakeLists.txt.tmp file failed to open.");
299       return false;
300     }
301   //copying contents from original to temporal and making changes
302   while (getline(in, line))
303     {
304       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
305         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
306       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
307         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
308       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
309         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
310       out << line << std::endl;
311     }
312   in.close();
313   out.close();
314   //delete old file and rename new file
315   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
316   if(system(renameCommand.c_str()))
317     {
318       result = new std::string("An error occurred while running '" + renameCommand + "'.");
319       return false;
320     }
321
322   this->version = vers[0] + "." + vers[1] + "." + vers[2];
323   return true;
324 }
325
326 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
327 {
328   std::vector<std::string> words;
329   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
330   std::string descriptionReal = words[0];
331   for (int i = 1; i < words.size(); i++)
332     {
333       descriptionReal += " " + words[i];
334     }
335
336   std::string line;
337   //opening original cmakelists
338   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
339   if( !in.is_open())
340     {
341       result = new std::string("CMakeLists.txt file failed to open.");
342       return false;
343     }
344   //opening temporal cmakelists
345   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
346   if( !out.is_open())
347     {
348       result = new std::string("CMakeLists.txt.tmp file failed to open.");
349       return false;
350     }
351   //copying contents from original to temporal and making changes
352   while (getline(in, line))
353     {
354       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
355         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
356       out << line << std::endl;
357     }
358   in.close();
359   out.close();
360   //delete old file and rename new file
361   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
362   if(system(renameCommand.c_str()))
363     {
364       result = new std::string("An error occurred while running '" + renameCommand + "'.");
365       return false;
366     }
367
368   this->description = descriptionReal;
369   return true;
370 }
371
372 bool modelCDMPackage::CreateBlackBox(
373     const std::string& name,
374     const std::string& authors,
375     const std::string& authorsEmail,
376     const std::string& categories,
377     const std::string& description
378 )
379 {
380   //TODO: implement method
381   return true;
382 }
383
384 const bool modelCDMPackage::Refresh(std::string*& result)
385 {
386   std::cout << "refreshing package" << std::endl;
387   this->type = wxDIR_DIRS;
388   this->name = name;
389   this->level = level;
390   this->path = path;
391
392   //Get Package Name
393
394   //TODO: set pathMakeLists for windows
395   std::string pathMakeLists = path + "/CMakeLists.txt";
396
397   std::ifstream confFile;
398   confFile.open((pathMakeLists).c_str());
399
400   std::string word;
401   while(confFile.is_open() && !confFile.eof())
402     {
403       //get sets
404       std::getline(confFile,word,'(');
405       std::vector<std::string> wordBits;
406       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
407
408       if(wordBits[wordBits.size()-1] == "SET")
409         {
410           //get package name
411           std::getline(confFile,word,')');
412           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
413           if(wordBits[0] == "BBTK_PACKAGE_NAME")
414             {
415               word = wordBits[1];
416               for (int i = 2; i < wordBits.size(); i++)
417                 {
418                   word += " " + wordBits[i];
419                 }
420               wordBits.clear();
421               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
422
423               this->namePackage = wordBits[0];
424             }
425           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
426             {
427               word = wordBits[1];
428               for (int i = 2; i < wordBits.size(); i++)
429                 {
430                   word += " " + wordBits[i];
431                 }
432               wordBits.clear();
433               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
434
435               this->authors = wordBits[0];
436             }
437           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
438             {
439               word = wordBits[1];
440               for (int i = 2; i < wordBits.size(); i++)
441                 {
442                   word += " " + wordBits[i];
443                 }
444               wordBits.clear();
445               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
446
447               this->description = wordBits[0];
448             }
449           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
450             {
451               this->version = wordBits[1];
452             }
453           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
454             {
455               this->version += "." + wordBits[1];
456             }
457           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
458             {
459               this->version += "." + wordBits[1];
460             }
461         }
462     }
463
464
465
466   std::vector<bool> checked(this->children.size(), false);
467   std::vector<bool> checkedBlackBoxes(this->blackBoxes.size(), false);
468
469   //check all folders
470   wxDir dir(crea::std2wx((this->path).c_str()));
471   if (dir.IsOpened())
472     {
473       wxString fileName;
474       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
475       while (cont)
476         {
477
478           std::string stdfileName = crea::wx2std(fileName);
479
480
481           if(stdfileName == "src")
482             {
483               wxDir srcF(crea::std2wx((path + "/" + "src").c_str()));
484               if (srcF.IsOpened())
485                 {
486                   wxString srcName;
487                   bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
488                   while (innerCont)
489                     {
490                       std::string blackBoxName = crea::wx2std(srcName);
491                       if(crea::wx2std(srcName.substr(0,2)) == "bb")
492                         {
493                           //check if box already exist
494                           bool found = false;
495                           for (int i = 0;!found && i < this->blackBoxes.size(); i++)
496                             {
497                               if (this->blackBoxes[i]->GetName() == blackBoxName)
498                                 {
499                                   found = true;
500                                   int pos = std::find(this->children.begin(), this->children.end(), this->blackBoxes[i]) - this->children.begin();
501                                   checked[pos] = true;
502                                   checkedBlackBoxes[i] = true;
503                                   if(!this->blackBoxes[i]->Refresh(result))
504                                     return false;
505                                 }
506                             }
507                           if(!found)
508                             {
509                               modelCDMBlackBox* blackBox = new modelCDMBlackBox(blackBoxName, path + "/" + "src", this->level +1);
510                               this->blackBoxes.push_back(blackBox);
511                               this->children.push_back(blackBox);
512                             }
513                         }
514                       innerCont = srcF.GetNext(&srcName);
515                     }
516                 }
517
518             }
519           //if not src
520           else
521             {
522               //check if they already exist
523               bool found = false;
524               for (int i = 0;!found && i < this->children.size(); i++)
525                 {
526                   if (this->children[i]->GetName() == stdfileName)
527                     {
528                       found = true;
529                       checked[i] = true;
530                       if(!this->children[i]->Refresh(result))
531                         return false;
532                     }
533                 }
534               if(!found)
535                 {
536                   modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
537                   this->children.push_back(folder);
538                 }
539               cont = dir.GetNext(&fileName);
540             }
541         }
542
543       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
544       while (cont)
545         {
546           std::string stdfileName = crea::wx2std(fileName);
547
548           //if CMakeLists, create CMakeLists
549           if(stdfileName == "CMakeLists.txt")
550             {
551               if (this->CMakeLists == NULL)
552                 {
553                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
554                   this->children.push_back(this->CMakeLists);
555                 }
556               else
557                 {
558                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
559                   checked[pos] = true;
560                   if(!this->CMakeLists->Refresh(result))
561                     return false;
562                 }
563             }
564           //if is an unknown file, create file
565           else
566             {
567               bool found = false;
568               for (int i = 0; i <!found && this->children.size(); i++)
569                 {
570                   if (this->children[i]->GetName() == stdfileName)
571                     {
572                       found = true;
573                       checked[i] = true;
574                       if(!this->children[i]->Refresh(result))
575                         return false;
576                     }
577                 }
578
579               if(!found)
580                 {
581                   modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
582                   this->children.push_back(file);
583                 }
584             }
585
586           cont = dir.GetNext(&fileName);
587         }
588     }
589
590   for (int i = 0; i < checkedBlackBoxes.size(); i++)
591     {
592       if(!checkedBlackBoxes[i])
593         {
594           this->blackBoxes.erase(this->blackBoxes.begin()+i);
595           checkedBlackBoxes.erase(checkedBlackBoxes.begin()+i);
596           i--;
597         }
598     }
599   for (int i = 0; i < checked.size(); i++)
600     {
601       if(!checked[i])
602         {
603           delete this->children[i];
604           this->children.erase(this->children.begin()+i);
605           checked.erase(checked.begin()+i);
606           i--;
607         }
608     }
609   this->SortChildren();
610   return true;
611 }