ATLAS Offline Software
Loading...
Searching...
No Matches
PathResolver Class Reference

#include <PathResolver.h>

Collaboration diagram for PathResolver:

Public Member Functions

 PathResolver ()=default

Static Public Member Functions

static std::string find_file (const std::string &logical_file_name, const std::string &search_path)
static std::string find_file_from_list (const std::string &logical_file_name, const std::string &search_list)
static std::string find_directory (const std::string &logical_file_name, const std::string &search_path)
static std::string find_directory_from_list (const std::string &logical_file_name, const std::string &search_list)
static std::string find_calib_file (const std::string &logical_file_name)
static std::string find_calib_directory (const std::string &logical_file_name)
static void setOutputLevel (MSG::Level level)
static void SetOutputLevel (int lvl)
static std::string FindCalibFile (const std::string &logical_file_name)
static std::string FindCalibDirectory (const std::string &logical_file_name)

Static Private Member Functions

static bool PR_find (const std::string &logical_file_name, const std::string &search_list, std::filesystem::file_type file_type, std::string &result)
 Main private search method used by all public methods.
static bool msgLvl (const MSG::Level lvl)
static MsgStream & msg ()
static MsgStream & msg (const MSG::Level lvl)
static asg::AsgMessagingasgMsg ()

Static Private Attributes

static std::atomic< MSG::Level > m_level {MSG::NIL}

Detailed Description

Definition at line 15 of file PathResolver.h.

Constructor & Destructor Documentation

◆ PathResolver()

PathResolver::PathResolver ( )
default

Member Function Documentation

◆ asgMsg()

asg::AsgMessaging & PathResolver::asgMsg ( )
staticprivate

Definition at line 129 of file PathResolver.cxx.

129 {
130
131#ifdef XAOD_STANDALONE
132 static thread_local asg::AsgMessaging asgMsg("PathResolver");
133#else
134 static asg::AsgMessaging asgMsg ATLAS_THREAD_SAFE ("PathResolver");
135#endif
136
137 // Set default OutputLevel unless user already set one
138 if (m_level==MSG::NIL) setOutputLevel(MSG::INFO);
139
140 return asgMsg;
141}
#define ATLAS_THREAD_SAFE
static void setOutputLevel(MSG::Level level)
static std::atomic< MSG::Level > m_level
static asg::AsgMessaging & asgMsg()

◆ find_calib_directory()

std::string PathResolver::find_calib_directory ( const std::string & logical_file_name)
static

Definition at line 294 of file PathResolver.cxx.

295{
296 checkForDev(asgMsg(), logical_file_name);
297
298 std::string out = PathResolver::find_directory (logical_file_name, "CALIBPATH");
299 if (out.empty()) {
300 msg(MSG::WARNING) << "Could not locate " << logical_file_name << endmsg;
301 }
302 return out;
303}
#define endmsg
static std::string find_directory(const std::string &logical_file_name, const std::string &search_path)
static MsgStream & msg()

◆ find_calib_file()

std::string PathResolver::find_calib_file ( const std::string & logical_file_name)
static

Definition at line 272 of file PathResolver.cxx.

273{
274 checkForDev(asgMsg(), logical_file_name);
275
276 if (logical_file_name.starts_with("root://")) {
277 //xrootd access .. try to open file ...
278 std::unique_ptr<TFile> fTmp{TFile::Open(logical_file_name.c_str())};
279 if (!fTmp || fTmp->IsZombie()) {
280 msg(MSG::WARNING) << "Could not open " << logical_file_name << endmsg;
281 return {};
282 }
283 return logical_file_name;
284 }
285
286 std::string out = PathResolver::find_file (logical_file_name, "CALIBPATH");
287 if (out.empty()) {
288 msg(MSG::WARNING) << "Could not locate " << logical_file_name << endmsg;
289 }
290 return out;
291}
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)

◆ find_directory()

std::string PathResolver::find_directory ( const std::string & logical_file_name,
const std::string & search_path )
static
  • logical_file_name the name of the directory to locate in the search path
  • search_path the name of a path-like environment variable
Returns
the physical name of the located directory or empty string if not found

Definition at line 249 of file PathResolver.cxx.

251{
252 const char* path_list = std::getenv(search_path.c_str());
253 if(path_list == nullptr) {
254 msg(MSG::ERROR) << search_path << " environment variable not defined!" << endmsg;
255 return {};
256 }
257
258 return find_directory_from_list(logical_file_name, path_list);
259}
static std::string find_directory_from_list(const std::string &logical_file_name, const std::string &search_list)
list path_list
Definition athena.py:93

◆ find_directory_from_list()

std::string PathResolver::find_directory_from_list ( const std::string & logical_file_name,
const std::string & search_list )
static
  • logical_file_name the name of the directory to locate in the search path
  • search_list the prioritized list of possible locations separated by the usual path separator
Returns
the physical name of the located directory or empty string if not found

Definition at line 262 of file PathResolver.cxx.

264{
265 std::string result;
266 PR_find(logical_file_name, search_list, fs::file_type::directory, result);
267
268 return result;
269}
static bool PR_find(const std::string &logical_file_name, const std::string &search_list, std::filesystem::file_type file_type, std::string &result)
Main private search method used by all public methods.

◆ find_file()

std::string PathResolver::find_file ( const std::string & logical_file_name,
const std::string & search_path )
static
  • logical_file_name the name of the file to locate in the search path
  • search_path the name of a path-like environment variable
Returns
the physical name of the located file or empty string if not found

Definition at line 220 of file PathResolver.cxx.

221 {
222
223#ifndef XAOD_ANALYSIS
224 if (logical_file_name.starts_with('/')) {
225 msg(MSG::ERROR) << "Use of an absolute file name: " << logical_file_name << endmsg;
226 }
227#endif
228
229 const char* path_list = std::getenv(search_path.c_str());
230 if (path_list == nullptr) {
231 msg(MSG::ERROR) << search_path << " environment variable not defined!" << endmsg;
232 return {};
233 }
234
235 return find_file_from_list(logical_file_name, path_list);
236}
static std::string find_file_from_list(const std::string &logical_file_name, const std::string &search_list)

◆ find_file_from_list()

std::string PathResolver::find_file_from_list ( const std::string & logical_file_name,
const std::string & search_list )
static
  • logical_file_name the name of the file to locate in the search path
  • search_list the prioritized list of possible locations separated by the usual path separator
Returns
the physical name of the located file or empty string if not found

Definition at line 239 of file PathResolver.cxx.

241{
242 std::string result;
243 PR_find (logical_file_name, search_list, fs::file_type::regular, result);
244
245 return result;
246}

◆ FindCalibDirectory()

std::string PathResolver::FindCalibDirectory ( const std::string & logical_file_name)
inlinestatic

Definition at line 64 of file PathResolver.h.

64{ return find_calib_directory(logical_file_name); }
static std::string find_calib_directory(const std::string &logical_file_name)

◆ FindCalibFile()

std::string PathResolver::FindCalibFile ( const std::string & logical_file_name)
inlinestatic

Definition at line 63 of file PathResolver.h.

63{ return find_calib_file(logical_file_name); }
static std::string find_calib_file(const std::string &logical_file_name)

◆ msg() [1/2]

MsgStream & PathResolver::msg ( )
inlinestaticprivate

Definition at line 73 of file PathResolver.h.

73{ return asgMsg().msg(); }
MsgStream & msg() const
The standard message stream.

◆ msg() [2/2]

MsgStream & PathResolver::msg ( const MSG::Level lvl)
inlinestaticprivate

Definition at line 74 of file PathResolver.h.

74{ return asgMsg().msg(lvl); }

◆ msgLvl()

bool PathResolver::msgLvl ( const MSG::Level lvl)
inlinestaticprivate

Definition at line 72 of file PathResolver.h.

72{ return asgMsg().msgLvl(lvl); }
bool msgLvl(const MSG::Level lvl) const
Test the output level of the object.

◆ PR_find()

bool PathResolver::PR_find ( const std::string & logical_file_name,
const std::string & search_list,
std::filesystem::file_type file_type,
std::string & result )
staticprivate

Main private search method used by all public methods.

Definition at line 147 of file PathResolver.cxx.

148 {
149
150 // expand filename before finding
151 TString tmpString(logical_file_name);
152 gSystem->ExpandPathName(tmpString);
153
154 fs::path file(tmpString.Data());
155 fs::path locationToDownloadTo = "."; // will replace with first search location
156
157 // First always search for filename as given in local directory
158 const std::string searchPath = std::format("./{}{}", path_separator, search_list);
159
160 // iterate through search list
161 for (const auto r : searchPath | std::views::split(path_separator)) {
162 std::string_view path(r.begin(), r.end());
163 const bool is_http = path.starts_with("http//");
164 if( (is_http || path.starts_with("https//")) &&
165 file_type==fs::file_type::regular && std::getenv("PATHRESOLVER_ALLOWHTTPDOWNLOAD") ) { // only http download files, not directories
166
167 // Try to do an http download to the local location.
168 // Need to restore the proper http protocol (cannot use ":" in search paths)
169 const std::string fileToDownload = std::format("{}://{}/{}", is_http ? "http" : "https",
170 path.substr(6), file.string());
171
172 const fs::path targetPath = locationToDownloadTo / file;
173 fs::path targetDir = targetPath;
174 targetDir.remove_filename();
175 if (msgLvl(MSG::DEBUG)) msg(MSG::DEBUG) << "Attempting http download of " << fileToDownload << " to " << targetDir << endmsg;
176
177 if (!is_directory(targetDir)) {
178 if (msgLvl(MSG::DEBUG)) msg(MSG::DEBUG) << "Creating directory " << targetDir << endmsg;
179 if(!fs::create_directories(targetDir)) {
180 msg(MSG::ERROR) << "Unable to create directories to write file to " << targetDir << endmsg;
181 return false;
182 }
183 }
184
185 if (!download_file(fileToDownload, targetPath, asgMsg())) {
186 msg(MSG::WARNING) << "Unable to download file " << fileToDownload << endmsg;
187 } else {
188 if (msgLvl(MSG::DEBUG)) msg(MSG::DEBUG) << "Successfully downloaded " << fileToDownload << endmsg;
189 result = targetPath;
190 return true;
191 }
192
193 } else if (locationToDownloadTo==".") {
194 // Prefer first non-pwd location (usually local build area) for downloading to.
195 fs::path dummyFile = fs::path(path) / "._pathresolver_dummy";
196 std::ofstream ofs(dummyFile); // check if writable
197 if (ofs.is_open()) {
198 locationToDownloadTo = path;
199 ofs.close();
200 fs::remove(dummyFile);
201 }
202 }
203
204 fs::path fp = path / file;
205 try {
206 if (fs::status(fp).type() == file_type) {
207 result = fs::absolute(fp).string();
208 return true;
209 }
210 } catch (const fs::filesystem_error&) {
211 // file not accessible or does not exist
212 }
213
214 }
215
216 return false; // not found
217}
static bool msgLvl(const MSG::Level lvl)
int r
Definition globals.cxx:22
path
python interpreter configuration --------------------------------------—
Definition athena.py:128
TFile * file

◆ SetOutputLevel()

void PathResolver::SetOutputLevel ( int lvl)
inlinestatic

Definition at line 61 of file PathResolver.h.

61{ setOutputLevel(MSG::Level(lvl)); }

◆ setOutputLevel()

void PathResolver::setOutputLevel ( MSG::Level level)
static

Definition at line 306 of file PathResolver.cxx.

306 {
307 m_level = level;
308#ifndef XAOD_ANALYSIS
310#else
311 asgMsg().msg().setLevel(m_level);
312#endif
313}
void setLevel(MSG::Level lvl)
Change the current logging level.

Member Data Documentation

◆ m_level

std::atomic<MSG::Level> PathResolver::m_level {MSG::NIL}
inlinestaticprivate

Definition at line 71 of file PathResolver.h.

71{MSG::NIL};

The documentation for this class was generated from the following files: