00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "OutputFileHandler.hpp"
00031
00032 #include <cstdlib>
00033 #include <sys/stat.h>
00034
00035 #include "PetscTools.hpp"
00036 #include "Exception.hpp"
00037 #include "ArchiveLocationInfo.hpp"
00038 #include "GetCurrentWorkingDirectory.hpp"
00039 #include "FileFinder.hpp"
00040
00041 OutputFileHandler::OutputFileHandler(const std::string &rDirectory,
00042 bool cleanOutputDirectory)
00043 {
00044
00045 if (rDirectory.find("..") != std::string::npos)
00046 {
00047 EXCEPTION("Will not create directory: " + rDirectory +
00048 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
00049 }
00050
00051 mDirectory = MakeFoldersAndReturnFullPath(rDirectory);
00052
00053
00054 if (rDirectory != "" && cleanOutputDirectory)
00055 {
00056 std::string command = "test -e " + mDirectory + ".chaste_deletable_folder";
00057 int return_value = system(command.c_str());
00058 if (return_value != 0)
00059 {
00060 EXCEPTION("Cannot delete " + mDirectory + " because signature file \".chaste_deletable_folder\" is not present.");
00061 }
00062
00063
00064 if (PetscTools::AmMaster())
00065 {
00066
00067
00068 MPIABORTIFNON0(system, "rm -rf " + mDirectory + "/*");
00069 }
00070
00071 PetscTools::Barrier("OutputFileHandler");
00072 }
00073 }
00074
00075 std::string OutputFileHandler::GetChasteTestOutputDirectory()
00076 {
00077 char *chaste_test_output = getenv("CHASTE_TEST_OUTPUT");
00078 std::string directory_root;
00079 if (chaste_test_output == NULL || *chaste_test_output == 0)
00080 {
00081
00082 directory_root = GetCurrentWorkingDirectory() + "/testoutput/";
00083 }
00084 else
00085 {
00086 if (*chaste_test_output != '/')
00087 {
00088
00089 directory_root = GetCurrentWorkingDirectory() + "/" + chaste_test_output;
00090 }
00091 else
00092 {
00093 #define COVERAGE_IGNORE
00094
00095 directory_root = std::string(chaste_test_output);
00096 #undef COVERAGE_IGNORE
00097 }
00098 }
00099 AddTrailingSlash(directory_root);
00100
00101 return directory_root;
00102 }
00103
00104
00105 std::string OutputFileHandler::MakeFoldersAndReturnFullPath(const std::string& rDirectory) const
00106 {
00107 std::string directory_root = GetChasteTestOutputDirectory();
00108 std::string directories_to_add = rDirectory;
00109 AddTrailingSlash(directories_to_add);
00110 std::string directory = directory_root + directories_to_add;
00111
00112
00113 if (PetscTools::AmMaster())
00114 {
00115
00116 std::string command = "test -d " + directory_root;
00117 int return_value = system(command.c_str());
00118 if (return_value!=0)
00119 {
00120
00121 MPIABORTIFNON0(system,"mkdir -p " + directory_root);
00122 }
00123
00124
00125 std::string remaining_directories = directories_to_add;
00126 std::string directory_to_add = "";
00127
00128
00129 while (remaining_directories.find("/") != std::string::npos)
00130 {
00131 size_t found = remaining_directories.find_first_of("/");
00132 directory_to_add += remaining_directories.substr(0,found+1);
00133 remaining_directories = remaining_directories.substr(found+1);
00134
00135 command = "test -d " + directory_root + directory_to_add;
00136 return_value = system(command.c_str());
00137 if (return_value!=0)
00138 {
00139
00140 MPIABORTIFNON0(system,"mkdir -p " + directory_root + directory_to_add);
00141
00142 MPIABORTIFNON0(system,"touch " + directory_root + directory_to_add + ".chaste_deletable_folder");
00143 }
00144 }
00145 }
00146
00147
00148 PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPath");
00149
00150 return directory;
00151 }
00152
00153
00154 std::string OutputFileHandler::GetOutputDirectoryFullPath() const
00155 {
00156 return mDirectory;
00157 }
00158
00159 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00160 std::ios_base::openmode mode) const
00161 {
00162 out_stream p_output_file(new std::ofstream((mDirectory+rFileName).c_str(), mode));
00163 if (!p_output_file->is_open())
00164 {
00165 EXCEPTION("Could not open file \"" + rFileName + "\" in " + mDirectory);
00166 }
00167 return p_output_file;
00168 }
00169
00170
00171 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00172 unsigned number,
00173 const std::string& rFileFormat,
00174 std::ios_base::openmode mode) const
00175 {
00176 std::stringstream string_stream;
00177 string_stream << rFileName << number << rFileFormat;
00178 return OpenOutputFile(string_stream.str(), mode);
00179 }
00180
00181 void OutputFileHandler::SetArchiveDirectory() const
00182 {
00183 FileFinder dir(GetOutputDirectoryFullPath(), RelativeTo::Absolute);
00184 ArchiveLocationInfo::SetArchiveDirectory(dir);
00185 }
00186
00187 void OutputFileHandler::AddTrailingSlash(std::string& rDirectory)
00188 {
00189
00190 if (rDirectory!="" && !( *(rDirectory.end()-1) == '/'))
00191 {
00192 rDirectory = rDirectory + "/";
00193 }
00194 }
00195
00196 FileFinder OutputFileHandler::CopyFileTo(const FileFinder& rSourceFile) const
00197 {
00198 if (!rSourceFile.IsFile())
00199 {
00200 EXCEPTION("Can only copy single files.");
00201 }
00202 if (PetscTools::AmMaster())
00203 {
00204 MPIABORTIFNON0(system, "cp " + rSourceFile.GetAbsolutePath() + " " + GetOutputDirectoryFullPath());
00205 }
00206 PetscTools::Barrier("OutputFileHandler::CopyFileTo");
00207 return FindFile(rSourceFile.GetLeafName());
00208 }
00209
00210 FileFinder OutputFileHandler::FindFile(std::string leafName) const
00211 {
00212 return FileFinder(GetOutputDirectoryFullPath() + leafName, RelativeTo::Absolute);
00213 }