]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackage.cpp
097c360c11265b9db3b73dcbfae25cf744925516
[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   this->src = NULL;
47 }
48
49 modelCDMPackage::modelCDMPackage(const std::string& path, const int& level)
50 {
51   this->type = wxDIR_DIRS;
52   //Get Package Name
53
54   //TODO: set pathMakeLists for windows
55   std::string pathMakeLists = path + "/CMakeLists.txt";
56
57   std::ifstream confFile;
58   confFile.open((pathMakeLists).c_str());
59
60   std::string word;
61   while(confFile.is_open() && !confFile.eof())
62     {
63       //get sets
64       std::getline(confFile,word,'(');
65       std::vector<std::string> wordBits;
66       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
67
68       if(wordBits[wordBits.size()-1] == "SET")
69         {
70           //get package name
71           std::getline(confFile,word,')');
72           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
73           if(wordBits[0] == "BBTK_PACKAGE_NAME")
74             {
75               word = wordBits[1];
76               for (int i = 2; i < wordBits.size(); i++)
77                 {
78                   word += " " + wordBits[i];
79                 }
80               wordBits.clear();
81               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
82
83               this->namePackage = wordBits[0];
84             }
85           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
86             {
87               word = wordBits[1];
88               for (int i = 2; i < wordBits.size(); i++)
89                 {
90                   word += " " + wordBits[i];
91                 }
92               wordBits.clear();
93               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
94
95               this->authors = wordBits[0];
96             }
97           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
98             {
99               word = wordBits[1];
100               for (int i = 2; i < wordBits.size(); i++)
101                 {
102                   word += " " + wordBits[i];
103                 }
104               wordBits.clear();
105               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
106
107               this->description = wordBits[0];
108             }
109           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
110             {
111               this->version = wordBits[1];
112             }
113           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
114             {
115               this->version += "." + wordBits[1];
116             }
117           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
118             {
119               this->version += "." + wordBits[1];
120             }
121         }
122     }
123
124   std::vector<std::string> words;
125   std::string delimiters;
126   //TODO::fix for windows
127   delimiters = "/";
128   CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
129   this->name = words[words.size()-1];
130   this->level = level;
131   this->path = path;
132
133   //check all folders and files
134   wxDir dir(crea::std2wx((path).c_str()));
135   if (dir.IsOpened())
136     {
137       wxString fileName;
138
139       //folders
140       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
141       while (cont)
142         {
143           std::string stdfileName = crea::wx2std(fileName);
144           //if src, check for black boxes
145           if(stdfileName == "src")
146             {
147               this->src = new modelCDMPackageSrc(path + delimiters + "src", this->level + 1);
148               this->children.push_back(this->src);
149               /*wxDir srcF(crea::std2wx((path + delimiters + "src").c_str()));
150               if (srcF.IsOpened())
151                 {
152                   wxString srcName;
153                   bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
154                   while (innerCont)
155                     {
156                       if(crea::wx2std(srcName.substr(0,2)) == "bb")
157                         {
158                           modelCDMBlackBox* blackbox = new modelCDMBlackBox(crea::wx2std(srcName), path + delimiters + "src", this->level + 1);
159                           this->blackBoxes.push_back(blackbox);
160                           this->children.push_back(blackbox);
161                         }
162                       innerCont = srcF.GetNext(&srcName);
163                     }
164                 }*/
165             }
166           else
167             {
168               this->children.push_back(new modelCDMFolder(path + delimiters + stdfileName, this->level + 1));
169             }
170
171           cont = dir.GetNext(&fileName);
172         }
173
174       //files
175       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
176       while (cont)
177         {
178           std::string stdfileName = crea::wx2std(fileName);
179
180           //if CMakeLists, create CMakeLists
181           if(stdfileName == "CMakeLists.txt")
182             {
183               this->CMakeLists = new modelCDMCMakeListsFile(path + delimiters + stdfileName, this->level + 1);
184               this->children.push_back(this->CMakeLists);
185             }
186           else
187             {
188               this->children.push_back(new modelCDMFile(path + delimiters + stdfileName, this->level + 1));
189             }
190           //if is an unknown file, create file
191           cont = dir.GetNext(&fileName);
192         }
193     }
194   this->SortChildren();
195 }
196
197 modelCDMPackage::~modelCDMPackage()
198 {
199 }
200
201 const std::string& modelCDMPackage::GetNamePackage() const
202 {
203   return this->namePackage;
204 }
205
206 const std::string& modelCDMPackage::GetAuthors() const
207 {
208   return this->authors;
209 }
210
211 const std::string& modelCDMPackage::GetAuthorsEmail() const
212 {
213   return this->authorsEmail;
214 }
215
216 const std::string& modelCDMPackage::GetVersion() const
217 {
218   return this->version;
219 }
220
221 const std::string& modelCDMPackage::GetDescription() const
222 {
223   return this->description;
224 }
225
226 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
227 {
228   return this->src;
229 }
230
231 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
232 {
233   std::vector<std::string> words;
234   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
235   std::string authorsReal = words[0];
236   for (int i = 1; i < words.size(); i++)
237     {
238       authorsReal += "/" + words[i];
239     }
240
241   std::string line;
242   //opening original cmakelists
243   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
244   if( !in.is_open())
245     {
246       result = new std::string("CMakeLists.txt file failed to open.");
247       return false;
248     }
249   //opening temporal cmakelists
250   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
251   if( !out.is_open())
252     {
253       result = new std::string("CMakeLists.txt.tmp file failed to open.");
254       return false;
255     }
256   //copying contents from original to temporal and making changes
257   while (getline(in, line))
258     {
259       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
260         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
261       out << line << std::endl;
262     }
263   in.close();
264   out.close();
265   //delete old file and rename new file
266   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
267   if(system(renameCommand.c_str()))
268     {
269       result = new std::string("An error occurred while running '" + renameCommand + "'.");
270       return false;
271     }
272
273   this->authors = authorsReal;
274   return true;
275 }
276
277 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
278 {
279   //TODO: implement method
280   return true;
281 }
282
283 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
284 {
285   std::vector<std::string> vers;
286   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
287
288
289   std::string line;
290   //opening original cmakelists
291   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
292   if( !in.is_open())
293     {
294       result = new std::string("CMakeLists.txt file failed to open.");
295       return false;
296     }
297   //opening temporal cmakelists
298   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
299   if( !out.is_open())
300     {
301       result = new std::string("CMakeLists.txt.tmp file failed to open.");
302       return false;
303     }
304   //copying contents from original to temporal and making changes
305   while (getline(in, line))
306     {
307       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
308         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
309       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
310         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
311       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
312         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
313       out << line << std::endl;
314     }
315   in.close();
316   out.close();
317   //delete old file and rename new file
318   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
319   if(system(renameCommand.c_str()))
320     {
321       result = new std::string("An error occurred while running '" + renameCommand + "'.");
322       return false;
323     }
324
325   this->version = vers[0] + "." + vers[1] + "." + vers[2];
326   return true;
327 }
328
329 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
330 {
331   std::vector<std::string> words;
332   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
333   std::string descriptionReal = words[0];
334   for (int i = 1; i < words.size(); i++)
335     {
336       descriptionReal += " " + words[i];
337     }
338
339   std::string line;
340   //opening original cmakelists
341   std::ifstream in((this->path + "/CMakeLists.txt").c_str());
342   if( !in.is_open())
343     {
344       result = new std::string("CMakeLists.txt file failed to open.");
345       return false;
346     }
347   //opening temporal cmakelists
348   std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
349   if( !out.is_open())
350     {
351       result = new std::string("CMakeLists.txt.tmp file failed to open.");
352       return false;
353     }
354   //copying contents from original to temporal and making changes
355   while (getline(in, line))
356     {
357       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
358         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
359       out << line << std::endl;
360     }
361   in.close();
362   out.close();
363   //delete old file and rename new file
364   std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
365   if(system(renameCommand.c_str()))
366     {
367       result = new std::string("An error occurred while running '" + renameCommand + "'.");
368       return false;
369     }
370
371   this->description = descriptionReal;
372   return true;
373 }
374
375 bool modelCDMPackage::CreateBlackBox(
376     const std::string& name,
377     const std::string& authors,
378     const std::string& authorsEmail,
379     const std::string& categories,
380     const std::string& description
381 )
382 {
383   //TODO: implement method
384   return true;
385 }
386
387 const bool modelCDMPackage::Refresh(std::string*& result)
388 {
389   std::cout << "refreshing package" << std::endl;
390   this->type = wxDIR_DIRS;
391   this->name = name;
392   this->level = level;
393   this->path = path;
394
395   //Get Package Name
396
397   //TODO: set pathMakeLists for windows
398   std::string pathMakeLists = path + "/CMakeLists.txt";
399
400   std::ifstream confFile;
401   confFile.open((pathMakeLists).c_str());
402
403   std::string word;
404   while(confFile.is_open() && !confFile.eof())
405     {
406       //get sets
407       std::getline(confFile,word,'(');
408       std::vector<std::string> wordBits;
409       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
410
411       if(wordBits[wordBits.size()-1] == "SET")
412         {
413           //get package name
414           std::getline(confFile,word,')');
415           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
416           if(wordBits[0] == "BBTK_PACKAGE_NAME")
417             {
418               word = wordBits[1];
419               for (int i = 2; i < wordBits.size(); i++)
420                 {
421                   word += " " + wordBits[i];
422                 }
423               wordBits.clear();
424               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
425
426               this->namePackage = wordBits[0];
427             }
428           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
429             {
430               word = wordBits[1];
431               for (int i = 2; i < wordBits.size(); i++)
432                 {
433                   word += " " + wordBits[i];
434                 }
435               wordBits.clear();
436               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
437
438               this->authors = wordBits[0];
439             }
440           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
441             {
442               word = wordBits[1];
443               for (int i = 2; i < wordBits.size(); i++)
444                 {
445                   word += " " + wordBits[i];
446                 }
447               wordBits.clear();
448               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
449
450               this->description = wordBits[0];
451             }
452           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
453             {
454               this->version = wordBits[1];
455             }
456           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
457             {
458               this->version += "." + wordBits[1];
459             }
460           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
461             {
462               this->version += "." + wordBits[1];
463             }
464         }
465     }
466
467
468
469   std::vector<bool> checked(this->children.size(), false);
470   bool checkedSrc = false;
471
472   //check all folders
473   wxDir dir(crea::std2wx((this->path).c_str()));
474   if (dir.IsOpened())
475     {
476       wxString fileName;
477       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
478       while (cont)
479         {
480
481           std::string stdfileName = crea::wx2std(fileName);
482
483           //detect black boxes in src
484           if(stdfileName == "src")
485             {
486               //check if box already exist
487               bool found = false;
488               if (this->src != NULL)
489                 {
490                   found = true;
491                   int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
492                   checked[pos] = true;
493                   checkedSrc = true;
494                   if(!this->src->Refresh(result))
495                     return false;
496                 }
497               else
498                 {
499                   this->src = new modelCDMPackageSrc(path + "/" + "src", this->level +1);
500                   this->children.push_back(this->src);
501                 }
502               /*wxDir srcF(crea::std2wx((path + "/" + "src").c_str()));
503               if (srcF.IsOpened())
504                 {
505                   wxString srcName;
506                   bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
507                   while (innerCont)
508                     {
509                       std::string blackBoxName = crea::wx2std(srcName);
510                       if(crea::wx2std(srcName.substr(0,2)) == "bb")
511                         {
512                           //check if box already exist
513                           bool found = false;
514                           for (int i = 0;!found && i < this->blackBoxes.size(); i++)
515                             {
516                               if (this->blackBoxes[i]->GetName() == blackBoxName)
517                                 {
518                                   found = true;
519                                   int pos = std::find(this->children.begin(), this->children.end(), this->blackBoxes[i]) - this->children.begin();
520                                   checked[pos] = true;
521                                   checkedBlackBoxes[i] = true;
522                                   if(!this->blackBoxes[i]->Refresh(result))
523                                     return false;
524                                 }
525                             }
526                           if(!found)
527                             {
528                               modelCDMBlackBox* blackBox = new modelCDMBlackBox(blackBoxName, path + "/" + "src", this->level +1);
529                               this->blackBoxes.push_back(blackBox);
530                               this->children.push_back(blackBox);
531                             }
532                         }
533                       innerCont = srcF.GetNext(&srcName);
534                     }
535                 }*/
536
537             }
538           else
539             {
540
541               //check if folder already exist
542               bool found = false;
543               for (int i = 0;!found && i < this->children.size(); i++)
544                 {
545                   if (this->children[i]->GetName() == stdfileName)
546                     {
547                       found = true;
548                       checked[i] = true;
549                       if(!this->children[i]->Refresh(result))
550                         return false;
551                     }
552                 }
553               if(!found)
554                 {
555                   modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
556                   this->children.push_back(folder);
557                 }
558             }
559           cont = dir.GetNext(&fileName);
560
561         }
562
563       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
564       while (cont)
565         {
566           std::string stdfileName = crea::wx2std(fileName);
567
568           //if CMakeLists, create CMakeLists
569           if(stdfileName == "CMakeLists.txt")
570             {
571               if (this->CMakeLists == NULL)
572                 {
573                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
574                   this->children.push_back(this->CMakeLists);
575                 }
576               else
577                 {
578                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
579                   checked[pos] = true;
580                   if(!this->CMakeLists->Refresh(result))
581                     return false;
582                 }
583             }
584           //if is an unknown file, create file
585           else
586             {
587               bool found = false;
588               for (int i = 0; i <!found && this->children.size(); i++)
589                 {
590                   if (this->children[i]->GetName() == stdfileName)
591                     {
592                       found = true;
593                       checked[i] = true;
594                       if(!this->children[i]->Refresh(result))
595                         return false;
596                     }
597                 }
598
599               if(!found)
600                 {
601                   modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
602                   this->children.push_back(file);
603                 }
604             }
605
606           cont = dir.GetNext(&fileName);
607         }
608     }
609
610   if(!checkedSrc)
611     {
612       this->src = NULL;
613     }
614
615   for (int i = 0; i < checked.size(); i++)
616     {
617       if(!checked[i])
618         {
619           delete this->children[i];
620           this->children.erase(this->children.begin()+i);
621           checked.erase(checked.begin()+i);
622           i--;
623         }
624     }
625   this->SortChildren();
626   return true;
627 }