13 #include <string_view>
20 namespace fs = std::filesystem;
23 const char path_separator =
':';
24 const char*
const pathResolverEnvVar =
"PATHRESOLVER_DEVAREARESPONSE";
28 bool download_with_curl(
const std::string&
url,
const std::string& output_path) {
31 std::string
cmd =
"curl -L -s -o " + output_path +
" " +
url;
32 int ret = std::system(
cmd.c_str());
38 const std::string& logical_file_name) {
42 if (logical_file_name.starts_with(
"dev/")) {
44 const std::string dev_area_response =
env ?
env :
"DEFAULT";
47 if (dev_area_response ==
"SILENT") {
50 else if (dev_area_response ==
"THROW") {
51 throw std::runtime_error(
52 "Loading dev area file " + logical_file_name +
" is not allowed! "
53 "To override this error set the environment variable " +
54 pathResolverEnvVar +
" to SILENT, DEFAULT, INFO, WARNING or ERROR");
56 else if (dev_area_response ==
"DEFAULT") {
64 else if (dev_area_response ==
"WARNING")
level = MSG::WARNING;
65 else if (dev_area_response ==
"ERROR")
level = MSG::ERROR;
67 throw std::runtime_error(
std::format(
"{} set to '{}', not sure what to do. "
68 "Options are DEFAULT, THROW, INFO, WARNING, ERROR or SILENT",
69 pathResolverEnvVar, dev_area_response));
73 asgmsg.
msg(
level) <<
"Locating dev file " << logical_file_name <<
". Do not let this propagate to a release!" <<
endmsg;
80 #ifdef XAOD_STANDALONE
99 fs::file_type file_type, std::string&
result ) {
102 TString tmpString(logical_file_name);
103 gSystem->ExpandPathName(tmpString);
106 fs::path locationToDownloadTo =
".";
109 const std::string searchPath =
std::format(
"./{}{}", path_separator, search_list);
113 std::string_view
path(
r.begin(),
r.end());
114 const bool is_http =
path.starts_with(
"http//");
115 if( (is_http ||
path.starts_with(
"https//")) &&
120 const std::string fileToDownload =
std::format(
"{}://{}/{}", is_http ?
"http" :
"https",
123 const fs::path targetPath = locationToDownloadTo /
file;
125 targetDir.remove_filename();
126 msg(
MSG::DEBUG) <<
"Attempting http download of " << fileToDownload <<
" to " << targetDir <<
endmsg;
128 if (!is_directory(targetDir)) {
130 if(!fs::create_directories(targetDir)) {
131 msg(MSG::ERROR) <<
"Unable to create directories to write file to " << targetDir <<
endmsg;
136 if (!TFile::Cp(fileToDownload.c_str(), targetPath.c_str(),
false)) {
139 <<
" with ROOT, falling back to command line tools"
141 if (download_with_curl(fileToDownload, targetPath)) {
146 msg(MSG::WARNING) <<
"Unable to download file "
156 }
else if (locationToDownloadTo==
".") {
159 std::ofstream ofs(dummyFile);
161 locationToDownloadTo =
path;
173 }
catch (
const fs::filesystem_error&) {
184 const std::string& search_path) {
186 #ifndef XAOD_ANALYSIS
187 if (logical_file_name.starts_with(
'/')) {
188 msg(MSG::ERROR) <<
"Use of an absolute file name: " << logical_file_name <<
endmsg;
194 msg(MSG::ERROR) << search_path <<
" environment variable not defined!" <<
endmsg;
203 const std::string& search_list)
213 const std::string& search_path)
217 msg(MSG::ERROR) << search_path <<
" environment variable not defined!" <<
endmsg;
226 const std::string& search_list)
237 checkForDev(
asgMsg(), logical_file_name);
239 if (logical_file_name.starts_with(
"root://")) {
241 std::unique_ptr<TFile> fTmp{TFile::Open(logical_file_name.c_str())};
242 if (!fTmp || fTmp->IsZombie()) {
243 msg(MSG::WARNING) <<
"Could not open " << logical_file_name <<
endmsg;
246 return logical_file_name;
251 msg(MSG::WARNING) <<
"Could not locate " << logical_file_name <<
endmsg;
259 checkForDev(
asgMsg(), logical_file_name);
263 msg(MSG::WARNING) <<
"Could not locate " << logical_file_name <<
endmsg;