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