Mantid
Loading...
Searching...
No Matches
ISISDataArchive.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
14#include "MantidKernel/Logger.h"
16#include <filesystem>
17
18namespace Mantid::DataHandling {
19namespace {
21Kernel::Logger g_log("ISISDataArchive");
22} // namespace
23
24DECLARE_ARCHIVESEARCH(ISISDataArchive, ISISDataSearch)
25
26namespace {
27#ifdef _WIN32
28constexpr std::string_view URL_PREFIX = "http://data.isis.rl.ac.uk/where.py/windir?name=";
29#else
30constexpr std::string_view URL_PREFIX = "http://data.isis.rl.ac.uk/where.py/unixdir?name=";
31#endif
32} // namespace
33
42const API::Result<std::filesystem::path> ISISDataArchive::getArchivePath(const std::set<std::string> &filenames,
43 const std::vector<std::string> &exts) const {
44 if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
45 for (const auto &filename : filenames) {
46 g_log.debug() << filename << ")\n";
47 }
48 for (const auto &ext : exts) {
49 g_log.debug() << ext << ")\n";
50 }
51 }
52
53 std::string errors = "";
54 for (const auto &filename : filenames) {
55 std::string path_without_extension = getPath(filename);
56 if (!path_without_extension.empty()) {
57#ifdef __APPLE__
58 // Replace Linux path with macOS path if we're on a Mac.
59 constexpr std::string_view unixString = "/archive/";
60 constexpr std::string_view macOSString = "/Volumes/inst$/";
61 path_without_extension.replace(0, unixString.length(), macOSString);
62#endif
63 std::string fullPath = getCorrectExtension(path_without_extension, exts);
64 if (!fullPath.empty())
66 errors += "No file found. ";
67#ifdef __linux__
68 errors +=
69 "If you are an IDAaaS user, and your file is in the ISIS archive, then check if the archive is mounted. If it is not mounted, \
70 \nthen click Applications->Data->Experiment Archive (Staff Only), and enter your federal ID credentials with 'clrc' as the domain.";
71#endif // __linux__
72 }
73 }
74 return API::Result<std::filesystem::path>("", errors);
75}
76
82std::string ISISDataArchive::getPath(const std::string &fName) const {
83 g_log.debug() << "ISISDataArchive::getPath() - fName=" << fName << "\n";
84 if (fName.empty())
85 return ""; // Avoid pointless call to service
86
87 std::ostringstream os = sendRequest(fName);
88 os << std::string(1, std::filesystem::path::preferred_separator) << fName;
89 std::string expectedPath = os.str();
90 return expectedPath;
91}
92
101std::ostringstream ISISDataArchive::sendRequest(const std::string &fName) const {
102 Kernel::InternetHelper inetHelper;
103 std::ostringstream os;
104 try {
105 inetHelper.sendRequest(URL_PREFIX.data() + fName, os);
107 g_log.warning() << "Could not access archive index." << ie.what();
108 }
109 return os;
110}
111
121std::string ISISDataArchive::getCorrectExtension(const std::string &path, const std::vector<std::string> &exts) const {
122 for (const auto &ext : exts) {
123 std::string temp_path = path + ext;
124 if (fileExists(temp_path))
125 return temp_path;
126 }
127 return "";
128}
129
137bool ISISDataArchive::fileExists(const std::string &path) const {
138 try {
139 if (std::filesystem::exists(path))
140 return true;
141 } catch (const std::filesystem::filesystem_error &) {
142 }
143 return false;
144}
145
146} // namespace Mantid::DataHandling
#define DECLARE_ARCHIVESEARCH(classname, facility)
const API::Result< std::filesystem::path > getArchivePath(const std::set< std::string > &hintstrs, const std::vector< std::string > &exts) const override
Returns the path to a filename given the list of extensions to try.
std::string getPath(const std::string &fName) const
Gets the path to the file, or most recent set of files.
virtual std::string getCorrectExtension(const std::string &path, const std::vector< std::string > &exts) const
Public and virtual for testing purposes.
virtual bool fileExists(const std::string &path) const
Checks if the given file path exists or not.
virtual std::ostringstream sendRequest(const std::string &fName) const
Queries the archive & returns the path to a single file.
Exception thrown when error occurs accessing an internet resource.
Definition Exception.h:321
const char * what() const noexcept override
Overloaded reporting method.
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
virtual HTTPStatus sendRequest(const std::string &url, std::ostream &responseStream)
Performs a request using http or https depending on the url.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
bool is(int level) const
Returns true if at least the given log level is set.
Definition Logger.cpp:177
Kernel::Logger g_log("ExperimentInfo")
static logger object