Documentation for OpenFLUID 2.2.1
WareSrcHelpers.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2021-2026, INRAE
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file WareSrcHelpers.hpp
34 
35  @author Armel THÖNI <armel.thoni@inra.fr>
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inrae.fr>
37  @author Dorian GERARDIN <dorian.gerardin@inrae.fr>
38  */
39 
40 
41 #ifndef __OPENFLUID_WARESDEV_WARESRCHELPERS_HPP__
42 #define __OPENFLUID_WARESDEV_WARESRCHELPERS_HPP__
43 
44 
45 #include <set>
46 #include <vector>
47 #include <string>
48 #include <regex>
49 
55 #include <openfluid/dllexport.hpp>
56 
57 
58 namespace openfluid { namespace waresdev {
59 
60 
62 {
63 
64  private:
65 
66  std::string m_BuildDirRegexStr = "_build-";
67  std::string ModeArg = "(release|debug)";
68  std::string VersionArg = "[0-9]+\\.[0-9]+";
69  std::string VersionMajorMinorStr;
70  std::string CurrentVersionRegex;
71 
72 
73  public:
74 
75  WarePurgeHandler(bool CurrentVersion, bool OtherVersions, bool ReleaseMode, bool DebugMode)
76  {
77 
79  CurrentVersionRegex = openfluid::tools::replace(VersionMajorMinorStr, ".", "\\.");
80 
81  if (CurrentVersion && !OtherVersions) // Current version only
82  {
83  VersionArg = CurrentVersionRegex;
84  }
85  else if (!CurrentVersion && OtherVersions) // Other versions only
86  {
87  VersionArg = "(?!"+CurrentVersionRegex+")[0-9]+\\.[0-9]+";
88  }
89 
90  if (ReleaseMode && !DebugMode) // Release mode only
91  {
92  ModeArg = "release";
93  }
94  else if (!ReleaseMode && DebugMode) // Debug mode only
95  {
96  ModeArg = "debug";
97  }
98 
99  m_BuildDirRegexStr = m_BuildDirRegexStr + ModeArg + "-" + VersionArg;
100  }
101 
102 
103  // =====================================================================
104  // =====================================================================
105 
106 
108  std::function<void(std::string, std::string)> WriteMessageFunc,
109  std::function<void(bool)> EmitSignal)
110  {
111  std::regex FilterRegExp(m_BuildDirRegexStr);
112 
113  if(!WarePath.exists())
114  {
115  WriteMessageFunc(WarePath.toGeneric() + ": Ware path does not exists ", "Error");
116  return;
117  }
118 
119  for (const auto& Entry : std::filesystem::directory_iterator(WarePath.stdPath()))
120  {
121  auto EntryFSP = openfluid::tools::FilesystemPath(Entry.path().string());
122  if (std::regex_match(EntryFSP.filename(), FilterRegExp))
123  {
124  EntryFSP.removeDirectory();
125 
126  if(EntryFSP.isDirectory())
127  {
128  WriteMessageFunc(EntryFSP.filename() + ": Error ", "Error");
129  EmitSignal(false);
130  }
131  else
132  {
133  WriteMessageFunc(EntryFSP.filename() + ": Deleted ", "Success");
134  EmitSignal(true);
135  }
136  }
137  }
138  }
139 
141  {
142 
143  }
144 };
145 
146 
147 // TODO reuse this wherever possible
148 inline static const std::vector<std::string> CppFilesExt = {"cpp","hpp","cc","hh","cxx","hxx","C","H","h"};
149 
150 
151 // =====================================================================
152 // =====================================================================
153 
154 
155 /**
156  Returns true if the given file path is a C++ source file (based on its extension)
157  @param[in] FileObj The file path
158  @return true if the file extension is a C++ extension
159 */
161 
162 
163 // =====================================================================
164 // =====================================================================
165 
166 
167 /**
168  Returns true if the given file path is a CMake file (based on its name and/or extension)
169  @param[in] FileObj The file path
170  @return true if the file a CMake file
171 */
173 
174 
175 // =====================================================================
176 // =====================================================================
177 
178 
179 /**
180  Checks if a given user can do read operations on a ware based on the RO/RW lists
181  @param[in] UserName the current user to check
182  @param[in] IsLoggedIn true if logged in to hub
183  @param[in] ROUsers the set of users with read only access
184  @param[in] RWUsers the set of users with read+write access
185  @return true if user is allowed to access the corresponding ware
186 */
187 bool OPENFLUID_API hasUserAccess(const std::string& UserName, bool IsLoggedIn,
188  const std::set<std::string>& ROUsers,
189  const std::set<std::string>& RWUsers={});
190 
191 
192 // =====================================================================
193 // =====================================================================
194 
195 
196 /**
197  Checks if a given ware id is found in current workspace.
198  Wrapper of WareSrcEnquirer function
199  @param[in] WarePath the ware path
200  @return true if ware found in workspace
201 */
202 bool OPENFLUID_API isWareInCurrentWorkspace(const std::string& WarePath);
203 
204 
205 // =====================================================================
206 // =====================================================================
207 
208 
209 /**
210  Initializes a map of CMake variables according to the OpenFLUID installation context
211  @return a map of variables
212 */
213 std::map<std::string,std::string> OPENFLUID_API initializeConfigureVariables();
214 
215 
216 // =====================================================================
217 // =====================================================================
218 
219 
220 /**
221  Clones a ware from remote git or hub URL
222  @param[in] SourceURL the ware path, the hub or git adress, may be optional
223  @param[in] SourceType either "hub" or "git" depending on source type
224  @param[in] ParentPath the path where clone will be run
225  @param[in] WareID the ware path, used for both local name and remote search on hub
226  @param[in] WareType the ware type, required for hub clone
227  @return exitcode of clone operation (0 means success)
228 */
229 int OPENFLUID_API cloneWare(const std::string& SourceURL, const std::string& SourceType="hub",
230  const std::string& ParentPath=".", const std::string& WareID="",
231  const std::string& WareType="");
232 } } // namespaces
233 
234 
235 #endif /* __OPENFLUID_WARESDEV_WARESRCHELPERS_HPP__ */
static std::string getVersionMajorMinor()
Definition: Environment.hpp:198
Definition: FilesystemPath.hpp:62
std::string toGeneric() const
const std::filesystem::path & stdPath() const
bool exists(const std::string &Path="") const
Definition: WareSrcHelpers.hpp:62
void purge(openfluid::tools::FilesystemPath WarePath, std::function< void(std::string, std::string)> WriteMessageFunc, std::function< void(bool)> EmitSignal)
Definition: WareSrcHelpers.hpp:107
WarePurgeHandler(bool CurrentVersion, bool OtherVersions, bool ReleaseMode, bool DebugMode)
Definition: WareSrcHelpers.hpp:75
~WarePurgeHandler()
Definition: WareSrcHelpers.hpp:140
#define OPENFLUID_API
Definition: dllexport.hpp:86
std::string OPENFLUID_API replace(const std::string &Str, const std::string &SearchStr, const std::string &ReplaceStr)
WareType
Definition: TypeDefs.hpp:61
int OPENFLUID_API cloneWare(const std::string &SourceURL, const std::string &SourceType="hub", const std::string &ParentPath=".", const std::string &WareID="", const std::string &WareType="")
static const std::vector< std::string > CppFilesExt
Definition: WareSrcHelpers.hpp:148
bool OPENFLUID_API IsCppFile(const openfluid::tools::FilesystemPath &FileObj)
bool OPENFLUID_API hasUserAccess(const std::string &UserName, bool IsLoggedIn, const std::set< std::string > &ROUsers, const std::set< std::string > &RWUsers={})
bool OPENFLUID_API isWareInCurrentWorkspace(const std::string &WarePath)
std::map< std::string, std::string > OPENFLUID_API initializeConfigureVariables()
bool OPENFLUID_API IsCMakeFile(const openfluid::tools::FilesystemPath &FileObj)
Definition: ApplicationException.hpp:47