CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SherpackFetcher.cc
Go to the documentation of this file.
1 /* Based on the cmsGetFnConnect and fn-fileget implementation by Dave Dykstra
2  * CMSSW/ FWCore/ Services/ bin/ cmsGetFnConnect.cc
3  * http://cdcvs.fnal.gov/cgi-bin/public-cvs/cvsweb-public.cgi/~checkout~/frontier/client/fn-fileget.c?rev=1.1&content-type=text/plain
4 */
5 
6 
7 //~ #include "SherpackFetcher.h"
9 
10 #include <memory>
11 
12 //~ #include <libtar.h>
13 namespace spf {
14 
16 {
17  if (!pset.exists("SherpaProcess")) SherpaProcess="";
18  else SherpaProcess=pset.getParameter<std::string>("SherpaProcess");
19  if (!pset.exists("SherpackLocation")) SherpackLocation="";
20  else SherpackLocation=pset.getParameter<std::string>("SherpackLocation");
21  if (!pset.exists("SherpackChecksum")) SherpackChecksum="";
22  else SherpackChecksum=pset.getParameter<std::string>("SherpackChecksum");
23  if (!pset.exists("FetchSherpack")) FetchSherpack=false;
24  else FetchSherpack=pset.getParameter<bool>("FetchSherpack");
25  if (!pset.exists("SherpaPath")) SherpaPath="";
26  else SherpaPath=pset.getParameter<std::string>("SherpaPath");
27 
28 }
29 
31 {
32 
33  std::string option = "-c";
34  std::string constr = "`cmsGetFnConnect frontier://smallfiles`";
35  std::string sherpack = "sherpa_" + SherpaProcess + "_MASTER.tgz";
36  std::string sherpackunzip = "sherpa_" + SherpaProcess + "_MASTER.tar";
37  std::string path = SherpackLocation + "/" + sherpack;
38 
39  //create the command line
40 
41  //~ //fn-fileget -c "`cmsGetFnConnect frontier://smallfiles`" slc5_ia32_gcc434/sherpa/1.2.2-cms3/8TeV/EWK/sherpa_8TeV_ewk_Zleptons5jetsincl_50_mll_8000_MASTER.tgz
42  if(FetchSherpack == true){
43  std::cout << "SherpackFetcher: Trying to fetch the Sherpack " << sherpack << std::endl;
44  int res =FnFileGet(path);
45  if (res!=1) {
46  std::cout <<"SherpackFetcher: Fetching of Sherpack did not succeed, terminating" << std::endl;
47  return -1;
48  }
49  std::cout << "SherpackFetcher: Fetching successful" << std::endl;
50  }
51 
52  std::ifstream my_file(sherpack.c_str());
53  if (!my_file.good())
54  {
55  std::cout << "SherpackFetcher: No Sherpack found" << std::endl;
56  return -2;
57  }
58  my_file.close();
59  std::cout << "SherpackFetcher: Sherpack found" << std::endl;
60 
61  if(SherpackChecksum!=""){
62  char md5checksum[33];
63  spu::md5_File(sherpack, md5checksum);
64  for (int k=0; k<33; k++){
65  if (md5checksum[k]!= SherpackChecksum[k]) {
66  std::cout << "SherpackFetcher: failure, calculated and specified checksums differ!" << std::endl;
67  return -3;
68  }
69  }
70  std::cout << "SherpackFetcher: Calculated checksum of the Sherpack is " << md5checksum << " and matches" << std::endl;
71  } else {
72  std::cout << "SherpackFetcher: Ignoring Checksum" << std::endl;
73  }
74 
75 
76  std::cout << "SherpackFetcher: Trying to unzip the Sherpack" << std::endl;
77  int res=spu::Unzip(sherpack,sherpackunzip);
78  if (res!=0) {
79  std::cout << "SherpackFetcher: Decompressing failed " << std::endl;
80  return -4;
81  }
82  std::cout << "SherpackFetcher: Decompressing successful " << std::endl;
83 
84  FILE *file = fopen(const_cast<char*>(sherpackunzip.c_str()),"r");
85  if( file ) {
86  std::cout << "SherpackFetcher: Decompressed Sherpack exists with name " << sherpackunzip << " starting to untar it"<<std::endl;
87  spu::Untar(file,SherpaPath.c_str());
88  } else {
89  std::cout << "SherpackFetcher: Could not open decompressed Sherpack" << std::endl;
90  return -5;
91  }
92  fclose(file);
93  return 0;
94 }
95 
97 {
98  int ec;
99  unsigned long channel;
100  FrontierConfig *config;
101 
103  try {
104  std::auto_ptr<edm::SiteLocalConfig> slcptr(new edm::service::SiteLocalConfigService(edm::ParameterSet()));
105  auto slc = std::make_shared<edm::serviceregistry::ServiceWrapper<edm::SiteLocalConfig> >(slcptr);
108 
109  edm::Service<edm::SiteLocalConfig> localconfservice;
110  localconfservice->lookupCalibConnect("frontier://smallfiles");
111  connectstr=localconfservice->lookupCalibConnect("frontier://smallfiles");
112  } catch(cms::Exception const& e) {
113  std::cerr << e.explainSelf() << std::endl;
114  return 2;
115  }
116 
117  if(frontier_init(malloc,free)!=0)
118  {
119  fprintf(stderr,"Error initializing frontier client: %s\n",frontier_getErrorMsg());
120  return 2;
121  }
122  ec=FRONTIER_OK;
123  //~ config=frontierConfig_get(connectstr,"",&ec);
124  config=frontierConfig_get(connectstr.c_str(),"",&ec);
125  if(ec!=FRONTIER_OK)
126  {
127  fprintf(stderr,"Error getting frontierConfig object: %s\n",frontier_getErrorMsg());
128  return 2;
129  }
130  channel=frontier_createChannel2(config,&ec);
131  if(ec!=FRONTIER_OK)
132  {
133  fprintf(stderr,"Error creating frontier channel: %s\n",frontier_getErrorMsg());
134  return 2;
135  }
136 
137 
138  char uribuf[4096];
139  FrontierRSBlob *frsb;
140  int fd;
141  int n;
142  char *p;
143  const char *localname;
144 
145  const char *path=pathstring.c_str();
146  snprintf(uribuf,sizeof(uribuf)-1, "Frontier/type=frontier_file:1:DEFAULT&encoding=BLOB&p1=%s",path);
147  ec=frontier_getRawData(channel,uribuf);
148  if(ec!=FRONTIER_OK)
149  {
150  fprintf(stderr,"Error getting data for %s: %s\n",path,frontier_getErrorMsg());
151  return 3;
152  }
153  frsb=frontierRSBlob_open(channel,0,1,&ec);
154  if(ec!=FRONTIER_OK)
155  {
156  fprintf(stderr,"Error opening result blob for %s: %s\n",path,frontier_getErrorMsg());
157  return 3;
158  }
159  // ignore the result type, will always be an array
160  (void)frontierRSBlob_getByte(frsb,&ec);
161  if(ec!=FRONTIER_OK)
162  {
163  fprintf(stderr,"Error getting result type for %s: %s\n",path,frontier_getErrorMsg());
164  return 3;
165  }
166  n=frontierRSBlob_getInt(frsb,&ec);
167  if(ec!=FRONTIER_OK)
168  {
169  fprintf(stderr,"Error getting result size for %s: %s\n",path,frontier_getErrorMsg());
170  return 3;
171  }
172  p=frontierRSBlob_getByteArray(frsb,n,&ec);
173  if(ec!=FRONTIER_OK)
174  {
175  fprintf(stderr,"Error getting result data for %s: %s\n",path,frontier_getErrorMsg());
176  return 3;
177  }
178  localname=strrchr(path,'/');
179  if(localname==NULL)
180  localname=pathstring.c_str();
181  else
182  localname++;
183  fd=open(localname,O_CREAT|O_TRUNC|O_WRONLY,0666);
184  if(fd==-1)
185  {
186  fprintf(stderr,"Error creating %s: %s\n",localname,strerror(errno));
187  ec=-1;
188  return 3;
189  }
190  if(write(fd,p,n)<0)
191  {
192  fprintf(stderr,"Error writing to %s: %s\n",localname,strerror(errno));
193  ec=-1;
194  close(fd);
195  return 3;
196  }
197  close(fd);
198  printf("%d bytes written to %s\n",n,localname);
199  frontierRSBlob_close(frsb,&ec);
200 
201 
202  frontier_closeChannel(channel);
203 
204  return (ec==FRONTIER_OK);
205 }
206 
208 {
209 }
210 
211 
212 
213 } // end of namespace definition
214 
215 //~ using spf::SherpackFetcher;
216 //~ DEFINE_FWK_MODULE(SherpackFetcher);
T getParameter(std::string const &) const
virtual std::string explainSelf() const
Definition: Exception.cc:146
void md5_File(std::string, char *)
std::string SherpackLocation
bool exists(std::string const &parameterName) const
checks if a parameter exists
#define NULL
Definition: scimark2.h:8
virtual std::string const lookupCalibConnect(std::string const &input) const =0
static ServiceToken createContaining(std::auto_ptr< T > iService)
create a service token that holds the service defined by iService
tuple path
else: Piece not in the list, fine.
std::string SherpackChecksum
void Untar(FILE *, const char *)
SherpackFetcher(edm::ParameterSet const &)
string connectstr
int FnFileGet(std::string)
tuple cout
Definition: gather_cfg.py:121
std::string SherpaProcess
int Unzip(std::string, std::string)