Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:14:12

0001 #include "GeneratorInterface/SherpaInterface/interface/SherpackFetcher.h"
0002 
0003 namespace spf {
0004 
0005   SherpackFetcher::SherpackFetcher(edm::ParameterSet const &pset) {
0006     if (!pset.exists("SherpaProcess"))
0007       SherpaProcess = "";
0008     else
0009       SherpaProcess = pset.getParameter<std::string>("SherpaProcess");
0010     if (!pset.exists("SherpackLocation"))
0011       SherpackLocation = "";
0012     else
0013       SherpackLocation = pset.getParameter<std::string>("SherpackLocation");
0014     if (!pset.exists("SherpackChecksum"))
0015       SherpackChecksum = "";
0016     else
0017       SherpackChecksum = pset.getParameter<std::string>("SherpackChecksum");
0018     if (!pset.exists("FetchSherpack"))
0019       FetchSherpack = false;
0020     else
0021       FetchSherpack = pset.getParameter<bool>("FetchSherpack");
0022     if (!pset.exists("SherpaPath"))
0023       SherpaPath = "";
0024     else
0025       SherpaPath = pset.getParameter<std::string>("SherpaPath");
0026   }
0027 
0028   int SherpackFetcher::Fetch() {
0029     std::string option = "-c";
0030     std::string constr = "`cmsGetFnConnect frontier://smallfiles`";
0031     std::string sherpack = "sherpa_" + SherpaProcess + "_MASTER.tgz";
0032     std::string sherpackunzip = "sherpa_" + SherpaProcess + "_MASTER.tar";
0033     std::string path = SherpackLocation + "/" + sherpack;
0034 
0035     if (FetchSherpack == true) {
0036       std::cout << "SherpackFetcher: Trying to fetch the Sherpack " << sherpack << std::endl;
0037       int res = -1;
0038 
0039       res = CopyFile(path);
0040 
0041       if (res != 1) {
0042         throw cms::Exception("SherpaInterface")
0043             << "SherpackFetcher: Fetching of Sherpack did not succeed, terminating" << std::endl;
0044         return -1;
0045       }
0046       std::cout << "SherpackFetcher: Fetching successful" << std::endl;
0047     }
0048 
0049     std::ifstream my_file(sherpack.c_str());
0050     if (!my_file.good()) {
0051       throw cms::Exception("SherpaInterface") << "SherpackFetcher: No Sherpack found" << std::endl;
0052       return -2;
0053     }
0054     my_file.close();
0055     std::cout << "SherpackFetcher: Sherpack found" << std::endl;
0056 
0057     if (!SherpackChecksum.empty()) {
0058       char md5checksum[33];
0059       spu::md5_File(sherpack, md5checksum);
0060       for (int k = 0; k < 33; k++) {
0061         if (md5checksum[k] != SherpackChecksum[k]) {
0062           throw cms::Exception("SherpaInterface")
0063               << "SherpackFetcher: failure, calculated and specified checksums differ!" << std::endl;
0064           return -3;
0065         }
0066       }
0067       std::cout << "SherpackFetcher: Calculated checksum of the Sherpack is " << md5checksum << " and matches"
0068                 << std::endl;
0069     } else {
0070       std::cout << "SherpackFetcher: Ignoring Checksum" << std::endl;
0071     }
0072 
0073     std::cout << "SherpackFetcher: Trying to unzip the Sherpack" << std::endl;
0074     int res = spu::Unzip(sherpack, sherpackunzip);
0075     if (res != 0) {
0076       throw cms::Exception("SherpaInterface") << "SherpackFetcher: Decompressing failed " << std::endl;
0077       return -4;
0078     }
0079     std::cout << "SherpackFetcher: Decompressing successful " << std::endl;
0080 
0081     FILE *file = fopen(const_cast<char *>(sherpackunzip.c_str()), "r");
0082     if (file) {
0083       std::cout << "SherpackFetcher: Decompressed Sherpack exists with name " << sherpackunzip
0084                 << " starting to untar it" << std::endl;
0085       spu::Untar(file, SherpaPath.c_str());
0086     } else {
0087       throw cms::Exception("SherpaInterface") << "SherpackFetcher: Could not open decompressed Sherpack" << std::endl;
0088       return -5;
0089     }
0090     fclose(file);
0091     return 0;
0092   }
0093 
0094   int SherpackFetcher::CopyFile(std::string pathstring) {
0095     //No need to backwards compatibility with the FnFileGet method, throw exception if only the relative path is given
0096     if ((pathstring.find("slc6_amd64_gcc") == 0) || (pathstring.find("slc5_amd64_gcc") == 0)) {
0097       throw cms::Exception("SherpaInterface") << "Old method of sherpack retrieving used, please use /cvmfs to store "
0098                                                  "files and specify the full path to the sherpack directory";
0099     }
0100     std::cout << "Trying to copy file " << pathstring << std::endl;
0101     std::string command = "cp " + pathstring + " .";
0102     FILE *pipe = popen(command.c_str(), "r");
0103     if (!pipe)
0104       throw cms::Exception("SherpaInterface") << "failed to copy Sherpack ";
0105     pclose(pipe);
0106     return 1;
0107   }
0108 
0109   SherpackFetcher::~SherpackFetcher() {}
0110 
0111 }  // namespace spf
0112 
0113 //~ using spf::SherpackFetcher;
0114 //~ DEFINE_FWK_MODULE(SherpackFetcher);