36 #include "OutputFileHandler.hpp"
40 #include "ArchiveLocationInfo.hpp"
43 #include "FileFinder.hpp"
44 #include "GetCurrentWorkingDirectory.hpp"
57 void CleanFolder(
const fs::path& rPath,
bool isTop=
true);
59 void CleanFolder(
const fs::path& rPath,
bool isTop)
61 assert(fs::is_directory(rPath));
62 fs::directory_iterator end_iter;
64 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
66 if (fs::is_directory(dir_iter->status()))
68 CleanFolder(dir_iter->path(),
false);
72 const fs::path& r_item_path(dir_iter->path());
75 fs::remove(r_item_path);
88 bool cleanOutputDirectory)
95 bool cleanOutputDirectory)
98 std::string relative_path;
105 EXCEPTION(
"The location provided to OutputFileHandler must be inside CHASTE_TEST_OUTPUT; '"
109 if (*output_root.
GetAbsolutePath().rbegin() !=
'/' && !relative_path.empty())
111 assert(*relative_path.begin() ==
'/');
112 relative_path.erase(0, 1);
119 bool cleanOutputDirectory)
122 if (rDirectory.find(
"..") != std::string::npos)
124 EXCEPTION(
"Will not create directory: " + rDirectory +
125 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
132 if (fs::path(rDirectory).has_root_path())
134 EXCEPTION(
"The constructor argument to OutputFileHandler must be a relative path; '"
135 << rDirectory <<
"' is absolute.");
141 if (rDirectory !=
"" && cleanOutputDirectory)
144 if (!signature_file.
Exists())
161 char *chaste_test_output = getenv(
"CHASTE_TEST_OUTPUT");
163 if (chaste_test_output == NULL || *chaste_test_output == 0)
174 std::string chaste_test_output_directory = directory_root.
GetAbsolutePath();
176 return chaste_test_output_directory;
182 fs::path rel_path(rDirectory);
184 if (!rel_path.empty() && (*(--rel_path.end())) ==
".")
187 rel_path.remove_leaf();
198 fs::create_directories(output_root);
201 fs::path next_folder(output_root);
202 for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
204 next_folder /= *path_iter;
205 bool created_dir = fs::create_directory(next_folder);
214 catch (
const fs::filesystem_error& e)
216 TERMINATE(
"Error making test output folder: " << e.what());
223 std::string path_with_slash = (output_root / rel_path).
string();
225 return path_with_slash;
237 if (!relative_path.empty() && *relative_path.rbegin() ==
'/')
239 relative_path.erase(--relative_path.end());
241 return relative_path;
245 std::ios_base::openmode mode)
const
247 out_stream p_output_file(
new std::ofstream((
mDirectory+rFileName).c_str(), mode));
248 if (!p_output_file->is_open())
252 return p_output_file;
257 const std::string& rFileFormat,
258 std::ios_base::openmode mode)
const
260 std::stringstream string_stream;
261 string_stream << rFileName << number << rFileFormat;
274 if (rDirectory!=
"" && !( *(rDirectory.end()-1) ==
'/'))
276 rDirectory = rDirectory +
"/";
282 if (!rSourceFile.
IsFile())
288 to_path /= from_path.leaf();
293 fs::copy_file(from_path, to_path);
295 catch (
const fs::filesystem_error& e)
std::string MakeFoldersAndReturnFullPath(const std::string &rDirectory) const
static const std::string SIG_FILE_NAME
static void AddTrailingSlash(std::string &rDirectory)
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
std::string GetAbsolutePath() const
#define EXCEPTION(message)
#define TERMINATE(message)
std::string GetOutputDirectoryFullPath() const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
void SetArchiveDirectory() const
FileFinder FindFile(std::string leafName) const
std::string GetRelativePath() const
#define PATH_LEAF_NAME(path)
std::string mDirectory
The directory to store output files in (always ends in "/")
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
static std::string GetChasteTestOutputDirectory()
static void SetArchiveDirectory(const FileFinder &rDirectory)
std::string GetRelativePath(const FileFinder &rBasePath) const
void CommonConstructor(const std::string &rDirectory, bool cleanOutputDirectory)
OutputFileHandler(const std::string &rDirectory, bool cleanOutputDirectory=true)