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