36#include "OutputFileHandler.hpp"
43#include "ArchiveLocationInfo.hpp"
45#include "FileFinder.hpp"
46#include "GetCurrentWorkingDirectory.hpp"
59void CleanFolder(
const fs::path& rPath,
bool isTop=
true);
61void CleanFolder(
const fs::path& rPath,
bool isTop)
63 assert(fs::is_directory(rPath));
64 fs::directory_iterator end_iter;
66 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
68 if (fs::is_directory(dir_iter->status()))
70 CleanFolder(dir_iter->path(),
false);
74 const fs::path& r_item_path(dir_iter->path());
75 if (!isTop || (r_item_path.filename().string())[0] !=
'.')
77 fs::remove(r_item_path);
90 bool cleanOutputDirectory)
97 bool cleanOutputDirectory)
100 std::string relative_path;
107 EXCEPTION(
"The location provided to OutputFileHandler must be inside CHASTE_TEST_OUTPUT; '"
111 if (*output_root.
GetAbsolutePath().rbegin() !=
'/' && !relative_path.empty())
113 assert(*relative_path.begin() ==
'/');
114 relative_path.erase(0, 1);
121 bool cleanOutputDirectory)
124 if (rDirectory.find(
"..") != std::string::npos)
126 EXCEPTION(
"Will not create directory: " + rDirectory +
127 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
134 if (fs::path(rDirectory).has_root_path())
136 EXCEPTION(
"The constructor argument to OutputFileHandler must be a relative path; '"
137 << rDirectory <<
"' is absolute.");
143 if (rDirectory !=
"" && cleanOutputDirectory)
146 if (!signature_file.
Exists())
163 char* chaste_test_output = getenv(
"CHASTE_TEST_OUTPUT");
165 if (chaste_test_output ==
nullptr || *chaste_test_output == 0)
168 std::stringstream tmp_directory;
169 if (getenv(
"USER")!=NULL)
171 tmp_directory <<
"/tmp/" << getenv(
"USER") <<
"/testoutput/";
176 tmp_directory <<
"/tmp/chaste/testoutput/";
188 std::string chaste_test_output_directory = directory_root.
GetAbsolutePath();
190 return chaste_test_output_directory;
196 fs::path rel_path(rDirectory);
204 if (!rel_path.empty())
206 assert( (*(--rel_path.end())) !=
".");
217 fs::create_directories(output_root);
220 fs::path next_folder(output_root);
221 for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
223 next_folder /= *path_iter;
224 bool created_dir = fs::create_directory(next_folder);
234 catch (
const fs::filesystem_error& e)
236 TERMINATE(
"Error making test output folder: " << e.what());
244 std::string path_with_slash = (output_root / rel_path).
string();
246 return path_with_slash;
258 if (!relative_path.empty() && *relative_path.rbegin() ==
'/')
260 relative_path.erase(--relative_path.end());
262 return relative_path;
266 std::ios_base::openmode mode)
const
268 out_stream p_output_file(
new std::ofstream((
mDirectory+rFileName).c_str(), mode));
269 if (!p_output_file->is_open())
273 return p_output_file;
278 const std::string& rFileFormat,
279 std::ios_base::openmode mode)
const
281 std::stringstream string_stream;
282 string_stream << rFileName << number << rFileFormat;
295 if (rDirectory!=
"" && !( *(rDirectory.end()-1) ==
'/'))
297 rDirectory = rDirectory +
"/";
303 if (!rSourceFile.
IsFile())
309 to_path /= from_path.filename();
314 fs::copy_file(from_path, to_path);
317 catch (
const fs::filesystem_error& e)
#define TERMINATE(message)
#define EXCEPTION(message)
static void SetArchiveDirectory(const FileFinder &rDirectory)
std::string GetRelativePath(const FileFinder &rBasePath) const
std::string GetAbsolutePath() const
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
static void AddTrailingSlash(std::string &rDirectory)
static std::string GetChasteTestOutputDirectory()
static const std::string SIG_FILE_NAME
std::string GetOutputDirectoryFullPath() const
void SetArchiveDirectory() const
FileFinder FindFile(std::string leafName) const
std::string MakeFoldersAndReturnFullPath(const std::string &rDirectory) const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
void CommonConstructor(const std::string &rDirectory, bool cleanOutputDirectory)
std::string mDirectory
The directory to store output files in (always ends in "/")
std::string GetRelativePath() const
OutputFileHandler(const std::string &rDirectory, bool cleanOutputDirectory=true)