36 #include "OutputFileHandler.hpp"
42 #include "ArchiveLocationInfo.hpp"
45 #include "FileFinder.hpp"
46 #include "GetCurrentWorkingDirectory.hpp"
59 void CleanFolder(
const fs::path& rPath,
bool isTop=
true);
61 void 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());
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 tmp_directory <<
"/tmp/" << getenv(
"USER") <<
"/testoutput/";
180 std::string chaste_test_output_directory = directory_root.
GetAbsolutePath();
182 return chaste_test_output_directory;
188 fs::path rel_path(rDirectory);
190 if (!rel_path.empty() && (*(--rel_path.end())) ==
".")
193 rel_path.remove_leaf();
204 fs::create_directories(output_root);
207 fs::path next_folder(output_root);
208 for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
210 next_folder /= *path_iter;
211 bool created_dir = fs::create_directory(next_folder);
221 catch (
const fs::filesystem_error& e)
223 TERMINATE(
"Error making test output folder: " << e.what());
231 std::string path_with_slash = (output_root / rel_path).
string();
233 return path_with_slash;
245 if (!relative_path.empty() && *relative_path.rbegin() ==
'/')
247 relative_path.erase(--relative_path.end());
249 return relative_path;
253 std::ios_base::openmode mode)
const
255 out_stream p_output_file(
new std::ofstream((
mDirectory+rFileName).c_str(), mode));
256 if (!p_output_file->is_open())
260 return p_output_file;
265 const std::string& rFileFormat,
266 std::ios_base::openmode mode)
const
268 std::stringstream string_stream;
269 string_stream << rFileName << number << rFileFormat;
282 if (rDirectory!=
"" && !( *(rDirectory.end()-1) ==
'/'))
284 rDirectory = rDirectory +
"/";
290 if (!rSourceFile.
IsFile())
296 to_path /= from_path.leaf();
301 fs::copy_file(from_path, to_path);
304 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)