35 using namespace msgGridTools;
42 typedef std::chrono::steady_clock clock;
47 std::recursive_mutex mutex;
50 bool haveProxy =
false;
53 decltype(clock::now()) proxyExpiration;
57 std::lock_guard<std::recursive_mutex> lock (mutex);
59 if (haveProxy ==
false)
70 std::istringstream
str (output);
73 if (!(str >> seconds))
76 std::istringstream str2 (
output.substr(
output.rfind(
'\n',
output.size()-2)+1,std::string::npos));
78 if (!(str2 >> seconds)){
79 ANA_MSG_INFO (
"failed to parse command output: " << output);
82 proxyExpiration = clock::now() + std::chrono::seconds (seconds);
88 proxyExpiration = clock::now() + std::chrono::seconds (seconds);
95 proxyExpiration > clock::now() + std::chrono::minutes (20);
100 std::lock_guard<std::recursive_mutex> lock (mutex);
119 ProxyData& proxyData ()
130 std::vector<std::string>
131 readLineList (
const std::string& text,
132 const std::string& begin)
134 std::vector<std::string>
result;
136 for (std::string::size_type
split = 0;
144 if (split2 == std::string::npos)
145 split2 =
text.size();
147 while (isspace (subresult.front()))
148 subresult = subresult.substr (1);
149 while (isspace (subresult.back()))
150 subresult.pop_back();
151 result.push_back (std::move (subresult));
161 std::string readLine (
const std::string& text,
162 const std::string& begin)
164 auto lines = readLineList (text, begin);
166 RCU_THROW_MSG (
"failed to find line starting with: " + begin);
167 if (
lines.size() > 1)
176 unsigned readLineUnsigned (
const std::string& text,
177 const std::string& begin)
179 const auto line = readLine (text, begin);
180 std::istringstream
str (line);
182 if (!(str >> result) || !
str.eof())
183 RCU_THROW_MSG (
"failed to convert " + line +
" into an unsigned");
190 std::string rucioSetupCommand ()
192 return "source $ATLAS_LOCAL_ROOT_BASE/user/atlasLocalSetup.sh -q && lsetup --force 'rucio -w'";
200 static const std::string
result =
"SAMPLEHANDLER_RUCIO_DOWNLOAD";
208 return proxyData().checkVomsProxy();
215 proxyData().ensureVomsProxy();
220 std::vector<std::string>
223#pragma GCC diagnostic push
224#pragma GCC diagnostic ignored "-Wpragmas"
225#pragma GCC diagnostic ignored "-Wunknown-pragmas"
226#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
228#pragma GCC diagnostic pop
233 std::vector<std::string>
242 static const std::string separator =
"------- SampleHandler Split -------";
243 std::vector<std::string>
result;
246 std::string output =
sh::exec_read (
"source $ATLAS_LOCAL_ROOT_BASE/user/atlasLocalSetup.sh -q && lsetup --force fax && echo " + separator +
" && fax-get-gLFNs " +
sh::quote (name));
247 auto split = output.rfind (separator +
"\n");
248 if (
split == std::string::npos)
251 std::istringstream
str (output.substr (
split + separator.size() + 1));
252 std::regex pattern (filter);
254 while (std::getline (
str, line))
258 if (!line.starts_with (
"root:"))
261 std::string::size_type split1 = line.rfind (
":");
262 std::string::size_type split2 = line.rfind (
"/");
265 if (split1 != std::string::npos)
274 RCU_WARN_MSG (
"dataset " + name +
" did not contain any files. this is likely not right");
280 std::vector<std::string>
282 const std::string& selectOptions)
290 std::vector<std::string>
292 const std::string& selectOptions)
300 static const std::string separator =
"------- SampleHandler Split -------";
303 std::string output =
sh::exec_read (rucioSetupCommand() +
" && echo " + separator +
" && rucio list-file-replicas --pfns --protocols root " + selectOptions +
" " +
sh::quote (name));
304 auto split = output.rfind (separator +
"\n");
305 if (
split == std::string::npos)
307 std::istringstream
str (output.substr (
split + separator.size() + 1));
312 std::map<std::string,std::string> resultMap;
314 std::regex urlPattern (
"^root://.*");
315 std::regex pattern (filter);
317 while (std::getline (
str, line))
327 std::string::size_type
split = line.rfind (
"/");
328 if (
split != std::string::npos)
330 std::string filename = line.substr (
split+1);
332 resultMap[filename] = line;
338 std::vector<std::string>
result;
339 for (
const auto&
file : resultMap)
342 ANA_MSG_WARNING (
"dataset " + name +
" did not contain any files. this is likely not right");
354 static const std::string separator =
"------- SampleHandler Split -------";
355 std::vector<RucioListDidsEntry>
result;
359 auto split = output.rfind (separator +
"\n");
360 if (
split == std::string::npos)
363 std::istringstream
str (output.substr (
split + separator.size() + 1));
364 std::regex pattern (
"^\\| ([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+) +\\| ([a-zA-Z0-9_.-]+) +\\| *$");
366 while (std::getline (
str, line))
369 if (std::regex_match (line, what, pattern))
372 entry.scope = what[1];
373 entry.name = what[2];
374 entry.type = what[3];
383 std::vector<RucioListFileReplicasEntry>
390 static const std::string separator =
"------- SampleHandler Split -------";
391 std::vector<RucioListFileReplicasEntry>
result;
393 std::string command = rucioSetupCommand() +
" && echo " + separator +
" && rucio list-file-replicas --protocols root " +
sh::quote (
dataset);
397 auto split = output.rfind (separator +
"\n");
398 if (
split == std::string::npos)
401 std::istringstream
str (output.substr (
split + separator.size() + 1));
402 std::regex pattern (
"^\\| +([^ ]+) +\\| +([^ ]+) +\\| +([^ ]+ [^ ]+) +\\| +([^ ]+) +\\| +([^: ]+): ([^ ]+) +\\| *$");
404 while (std::getline (
str, line))
407 if (std::regex_match (line, what, pattern) &&
411 entry.scope = what[1];
412 entry.name = what[2];
413 entry.filesize = what[3];
414 entry.adler32 = what[4];
415 entry.disk = what[5];
416 entry.replica = what[6];
425 std::map<std::string,std::unique_ptr<MetaObject> >
432 static const std::string separator =
"------- SampleHandler Split -------";
433 std::map<std::string,std::unique_ptr<MetaObject> >
result;
435 std::string command = rucioSetupCommand() +
" && echo " + separator +
" && rucio get-metadata";
444 auto split = output.rfind (separator +
"\n");
445 if (
split == std::string::npos)
448 std::istringstream
str (output.substr (
split + separator.size() + 1));
449 std::regex pattern (
"^([^:]+): *(.+)$");
453 auto addMeta = [&] ()
455 std::string name =
meta->castString (
"scope") +
":" +
meta->castString (
"name");
461 while (std::getline (
str, line))
464 if (line ==
"------")
468 }
else if (std::regex_match (line, what, pattern))
470 if (
meta->get (what[1]))
471 throw std::runtime_error (std::string(
"duplicate entry: ") + what[1].
str());
472 meta->setString (what[1], what[2]);
473 }
else if (!line.empty())
480 for (
auto& subresult :
result)
482 if (datasets.find (subresult.first) == datasets.end())
483 RCU_THROW_MSG (
"received result for dataset not requested: " + subresult.first);
501 const std::string separator =
"------- SampleHandler Split -------";
502 std::string command = rucioSetupCommand() +
" && echo " + separator +
" && cd " +
sh::quote (location) +
" && rucio download " +
sh::quote (
dataset) +
" 2>&1";
506 auto split = output.rfind (separator +
"\n");
507 if (
split == std::string::npos)
509 output = output.substr (
split + separator.size() + 1);
512 result.did = readLine (output,
"DID ");
513 result.totalFiles = readLineUnsigned (output,
"Total files (DID): ");
514 result.downloadedFiles = readLineUnsigned (output,
"Downloaded files: ");
515 result.alreadyLocal = readLineUnsigned (output,
"Files already found locally: ");
516 result.notDownloaded = readLineUnsigned (output,
"Files that cannot be downloaded: ");
522 std::vector<RucioDownloadResult>
524 const std::vector<std::string>& datasets)
526 std::vector<RucioDownloadResult>
result;
534 std::vector<std::string>
537 const std::string& fileGlob)
539 std::vector<std::string>
result;
541 std::string path = location;
542 if (path.back() !=
'/')
544 if (
dataset.find (
':') != std::string::npos)
548 const std::string finished {
553 if (gSystem->AccessPathName (finished.c_str()) != 0)
556 if (status.downloadedFiles + status.alreadyLocal < status.totalFiles)
557 throw std::runtime_error (
"failed to download all files of " +
dataset);
559 std::ofstream (finished.c_str());
563 std::istringstream
str (output);
565 while (std::getline (
str, line))
#define RCU_REQUIRE_SOFT(x)
#define RCU_THROW_MSG(message)
#define RCU_WARN_MSG(message)
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
std::vector< std::string > split(const std::string &s, const std::string &t=":")
std::string exec_read(const std::string &cmd)
effects: execute the given command and return the output returns: the output of the command guarantee...
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
bool match_expr(const std::regex &expr, const std::string &str)
returns: whether we can match the entire string with the regular expression guarantee: strong failure...
std::string glob_to_regexp(const std::string &glob)
returns: a string that is the regular expression equivalent of the given glob expression guarantee: s...
This module provides a lot of global definitions, forward declarations and includes that are used by ...
std::vector< RucioDownloadResult > rucioDownloadList(const std::string &location, const std::vector< std::string > &datasets)
run rucio-download with multiple datasets
RucioDownloadResult rucioDownload(const std::string &location, const std::string &dataset)
run rucio-download
const std::string & downloadStageEnvVar()
the name of the environment variable containing the directory for staging files from the grid
std::vector< std::string > faxListFilesGlob(const std::string &name, const std::string &filter)
list the FAX URLs for all the files in the dataset or dataset container matching the given filter (as...
std::vector< RucioListFileReplicasEntry > rucioListFileReplicas(const std::string &dataset)
run rucio-list-file-replicas for the given dataset
std::vector< RucioListDidsEntry > rucioListDids(const std::string &dataset)
run rucio-list-dids for the given dataset
void ensureVomsProxy()
ensure that we have a valid VOMS proxy available
std::map< std::string, std::unique_ptr< MetaObject > > rucioGetMetadata(const std::set< std::string > &datasets)
run rucio-get-metadata for the given list of datasets
std::vector< std::string > rucioDirectAccessRegex(const std::string &name, const std::string &filter, const std::string &selectOptions)
list the rucio URLs for all the files in the dataset or dataset container matching the given filter (...
std::vector< std::string > rucioCacheDatasetGlob(const std::string &location, const std::string &dataset, const std::string &fileGlob)
download the dataset, and return a list matching the pattern
bool checkVomsProxy()
return whether we have a valid VOMS proxy available
std::vector< std::string > faxListFilesRegex(const std::string &name, const std::string &filter)
list the FAX URLs for all the files in the dataset or dataset container matching the given filter (as...
std::vector< std::string > rucioDirectAccessGlob(const std::string &name, const std::string &filter, const std::string &selectOptions)
list the rucio URLs for all the files in the dataset or dataset container matching the given filter (...
the result from rucio_download
one entry from the rucio-list-dids command
one entry from the rucio-list-file-replicas command