File indexing completed on 2024-09-27 07:02:34
0001 #include <DD4hep/DetFactoryHelper.h>
0002 #include <DD4hep/Primitives.h>
0003 #include <DD4hep/Factories.h>
0004 #include <DD4hep/Printout.h>
0005 #include <XML/Utilities.h>
0006
0007 #include <fmt/core.h>
0008
0009 #include <filesystem>
0010 #include <iostream>
0011 #include <cstdlib>
0012 #include <string>
0013
0014 #include "FileLoaderHelper.h"
0015
0016 using namespace dd4hep;
0017
0018 void usage(int argc, char** argv) {
0019 std::cout <<
0020 "Usage: -plugin <name> -arg [-arg] \n"
0021 " name: factory name FileLoader \n"
0022 " cache:<string> cache location (may be read-only) \n"
0023 " file:<string> file location \n"
0024 " url:<string> url location \n"
0025 " cmd:<string> download command with {0} for url, {1} for output \n"
0026 "\tArguments given: " << arguments(argc,argv) << std::endl;
0027 std::exit(EINVAL);
0028 }
0029
0030
0031 long load_file(
0032 Detector& ,
0033 int argc,
0034 char** argv
0035 ) {
0036
0037 std::string cache, file, url;
0038 std::string cmd("curl --retry 5 -f {0} -o {1}");
0039 for (int i = 0; i < argc && argv[i]; ++i) {
0040 if (0 == std::strncmp("cache:", argv[i], 6)) cache = (argv[i] + 6);
0041 else if (0 == std::strncmp("file:", argv[i], 5)) file = (argv[i] + 5);
0042 else if (0 == std::strncmp("url:", argv[i], 4)) url = (argv[i] + 4);
0043 else if (0 == std::strncmp("cmd:", argv[i], 4)) cmd = (argv[i] + 4);
0044 else usage(argc, argv);
0045 }
0046 printout(DEBUG, "FileLoader", "arg cache: " + cache);
0047 printout(DEBUG, "FileLoader", "arg file: " + file);
0048 printout(DEBUG, "FileLoader", "arg url: " + url);
0049 printout(DEBUG, "FileLoader", "arg cmd: " + cmd);
0050
0051
0052 if (file.empty()) {
0053 printout(WARNING, "FileLoader", "no file specified");
0054 return 0;
0055 }
0056 if (url.empty()) {
0057 printout(WARNING, "FileLoader", "no url specified");
0058 return 0;
0059 }
0060
0061 EnsureFileFromURLExists(url, file, cache, cmd);
0062
0063 return 0;
0064 }
0065
0066 DECLARE_APPLY(FileLoader, load_file)