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