]> 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   this->src = NULL;
47 }
48
49 modelCDMPackage::modelCDMPackage(const std::string& path, const std::string& name, const int& level)
50 {
51   this->type = wxDIR_DIRS;
52   this->name = name;
53   //Get Package Name
54
55   std::string pathMakeLists = path + CDMUtilities::SLASH + "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   this->level = level;
125   this->path = path;
126
127   //check all folders and files
128   wxDir dir(crea::std2wx((path).c_str()));
129   if (dir.IsOpened())
130     {
131       wxString fileName;
132
133       //folders
134       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
135       while (cont)
136         {
137           std::string stdfileName = crea::wx2std(fileName);
138           //if src, check for black boxes
139           if(stdfileName == "src")
140             {
141               this->src = new modelCDMPackageSrc(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
142               this->children.push_back(this->src);
143             }
144           else
145             {
146               this->children.push_back(new modelCDMFolder(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
147             }
148
149           cont = dir.GetNext(&fileName);
150         }
151
152       //files
153       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
154       while (cont)
155         {
156           std::string stdfileName = crea::wx2std(fileName);
157
158           //if CMakeLists, create CMakeLists
159           if(stdfileName == "CMakeLists.txt")
160             {
161               this->CMakeLists = new modelCDMCMakeListsFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
162               this->children.push_back(this->CMakeLists);
163             }
164           else
165             {
166               this->children.push_back(new modelCDMFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
167             }
168           //if is an unknown file, create file
169           cont = dir.GetNext(&fileName);
170         }
171     }
172   this->SortChildren();
173 }
174
175 modelCDMPackage::~modelCDMPackage()
176 {
177 }
178
179 const std::string& modelCDMPackage::GetNamePackage() const
180 {
181   return this->namePackage;
182 }
183
184 const std::string& modelCDMPackage::GetAuthors() const
185 {
186   return this->authors;
187 }
188
189 const std::string& modelCDMPackage::GetAuthorsEmail() const
190 {
191   return this->authorsEmail;
192 }
193
194 const std::string& modelCDMPackage::GetVersion() const
195 {
196   return this->version;
197 }
198
199 const std::string& modelCDMPackage::GetDescription() const
200 {
201   return this->description;
202 }
203
204 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
205 {
206   return this->src;
207 }
208
209 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
210 {
211   std::vector<std::string> words;
212   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
213   std::string authorsReal = words[0];
214   for (int i = 1; i < words.size(); i++)
215     {
216       authorsReal += "/" + words[i];
217     }
218
219   std::string line;
220   //opening original cmakelists
221   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
222   if( !in.is_open())
223     {
224       result = new std::string("CMakeLists.txt file failed to open.");
225       return false;
226     }
227   //opening temporal cmakelists
228   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
229   if( !out.is_open())
230     {
231       result = new std::string("CMakeLists.txt.tmp file failed to open.");
232       return false;
233     }
234   //copying contents from original to temporal and making changes
235   while (getline(in, line))
236     {
237       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
238         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
239       out << line << std::endl;
240     }
241   in.close();
242   out.close();
243   //delete old file and rename new file
244   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
245   if(system(renameCommand.c_str()))
246     {
247       result = new std::string("An error occurred while running '" + renameCommand + "'.");
248       return false;
249     }
250
251   this->authors = authorsReal;
252   return true;
253 }
254
255 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
256 {
257   //TODO: implement method
258   return true;
259 }
260
261 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
262 {
263   std::vector<std::string> vers;
264   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
265
266
267   std::string line;
268   //opening original cmakelists
269   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
270   if( !in.is_open())
271     {
272       result = new std::string("CMakeLists.txt file failed to open.");
273       return false;
274     }
275   //opening temporal cmakelists
276   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
277   if( !out.is_open())
278     {
279       result = new std::string("CMakeLists.txt.tmp file failed to open.");
280       return false;
281     }
282   //copying contents from original to temporal and making changes
283   while (getline(in, line))
284     {
285       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
286         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
287       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
288         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
289       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
290         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
291       out << line << std::endl;
292     }
293   in.close();
294   out.close();
295   //delete old file and rename new file
296   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
297   if(system(renameCommand.c_str()))
298     {
299       result = new std::string("An error occurred while running '" + renameCommand + "'.");
300       return false;
301     }
302
303   this->version = vers[0] + "." + vers[1] + "." + vers[2];
304   return true;
305 }
306
307 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
308 {
309   std::vector<std::string> words;
310   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
311   std::string descriptionReal = words[0];
312   for (int i = 1; i < words.size(); i++)
313     {
314       descriptionReal += " " + words[i];
315     }
316
317   std::string line;
318   //opening original cmakelists
319   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
320   if( !in.is_open())
321     {
322       result = new std::string("CMakeLists.txt file failed to open.");
323       return false;
324     }
325   //opening temporal cmakelists
326   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
327   if( !out.is_open())
328     {
329       result = new std::string("CMakeLists.txt.tmp file failed to open.");
330       return false;
331     }
332   //copying contents from original to temporal and making changes
333   while (getline(in, line))
334     {
335       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
336         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
337       out << line << std::endl;
338     }
339   in.close();
340   out.close();
341   //delete old file and rename new file
342   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
343   if(system(renameCommand.c_str()))
344     {
345       result = new std::string("An error occurred while running '" + renameCommand + "'.");
346       return false;
347     }
348
349   this->description = descriptionReal;
350   return true;
351 }
352
353 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
354     const std::string& name,
355     const std::string& type,
356     const std::string& format,
357     const std::string& authors,
358     const std::string& authorsEmail,
359     const std::string& categories,
360     const std::string& description
361 )
362 {
363   //TODO: implement method
364   return NULL;
365 }
366
367 const bool modelCDMPackage::Refresh(std::string*& result)
368 {
369   std::cout << "refreshing package" << std::endl;
370   this->type = wxDIR_DIRS;
371
372   //Get Package Name
373
374   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
375
376   std::ifstream confFile;
377   confFile.open((pathMakeLists).c_str());
378
379   std::string word;
380   while(confFile.is_open() && !confFile.eof())
381     {
382       //get sets
383       std::getline(confFile,word,'(');
384       std::vector<std::string> wordBits;
385       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
386
387       if(wordBits[wordBits.size()-1] == "SET")
388         {
389           //get package name
390           std::getline(confFile,word,')');
391           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
392           if(wordBits[0] == "BBTK_PACKAGE_NAME")
393             {
394               word = wordBits[1];
395               for (int i = 2; i < wordBits.size(); i++)
396                 {
397                   word += " " + wordBits[i];
398                 }
399               wordBits.clear();
400               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
401
402               this->namePackage = wordBits[0];
403             }
404           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
405             {
406               word = wordBits[1];
407               for (int i = 2; i < wordBits.size(); i++)
408                 {
409                   word += " " + wordBits[i];
410                 }
411               wordBits.clear();
412               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
413
414               this->authors = wordBits[0];
415             }
416           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
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->description = wordBits[0];
427             }
428           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
429             {
430               this->version = wordBits[1];
431             }
432           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
433             {
434               this->version += "." + wordBits[1];
435             }
436           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
437             {
438               this->version += "." + wordBits[1];
439             }
440         }
441     }
442
443
444
445   std::vector<bool> checked(this->children.size(), false);
446   bool checkedSrc = false;
447
448   //check all folders
449   wxDir dir(crea::std2wx((this->path).c_str()));
450   if (dir.IsOpened())
451     {
452       wxString fileName;
453       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
454       while (cont)
455         {
456
457           std::string stdfileName = crea::wx2std(fileName);
458
459           //detect black boxes in src
460           if(stdfileName == "src")
461             {
462               //check if box already exist
463               bool found = false;
464               if (this->src != NULL)
465                 {
466                   found = true;
467                   int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
468                   checked[pos] = true;
469                   checkedSrc = true;
470                   if(!this->src->Refresh(result))
471                     return false;
472                 }
473               else
474                 {
475                   this->src = new modelCDMPackageSrc(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
476                   this->children.push_back(this->src);
477                 }
478             }
479           else
480             {
481
482               //check if folder already exist
483               bool found = false;
484               for (int i = 0;!found && i < this->children.size(); i++)
485                 {
486                   if (this->children[i]->GetName() == stdfileName)
487                     {
488                       found = true;
489                       checked[i] = true;
490                       if(!this->children[i]->Refresh(result))
491                         return false;
492                     }
493                 }
494               if(!found)
495                 {
496                   modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
497                   this->children.push_back(folder);
498                 }
499             }
500           cont = dir.GetNext(&fileName);
501
502         }
503
504       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
505       while (cont)
506         {
507           std::string stdfileName = crea::wx2std(fileName);
508
509           //if CMakeLists, create CMakeLists
510           if(stdfileName == "CMakeLists.txt")
511             {
512               if (this->CMakeLists == NULL)
513                 {
514                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
515                   this->children.push_back(this->CMakeLists);
516                 }
517               else
518                 {
519                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
520                   checked[pos] = true;
521                   if(!this->CMakeLists->Refresh(result))
522                     return false;
523                 }
524             }
525           //if is an unknown file, create file
526           else
527             {
528               bool found = false;
529               for (int i = 0; i <!found && this->children.size(); i++)
530                 {
531                   if (this->children[i]->GetName() == stdfileName)
532                     {
533                       found = true;
534                       checked[i] = true;
535                       if(!this->children[i]->Refresh(result))
536                         return false;
537                     }
538                 }
539
540               if(!found)
541                 {
542                   modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
543                   this->children.push_back(file);
544                 }
545             }
546
547           cont = dir.GetNext(&fileName);
548         }
549     }
550
551   if(!checkedSrc)
552     {
553       this->src = NULL;
554     }
555
556   for (int i = 0; i < checked.size(); i++)
557     {
558       if(!checked[i])
559         {
560           delete this->children[i];
561           this->children.erase(this->children.begin()+i);
562           checked.erase(checked.begin()+i);
563           i--;
564         }
565     }
566   this->SortChildren();
567   return true;
568 }