ROOT  6.06/09
Reference Guide
pq2main.cxx
Go to the documentation of this file.
1 // @(#)root/proof:$Id$
2 // Author: G. Ganis, Mar 2010
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 // ************************************************************************* //
13 // * * //
14 // * p q 2 m a i n * //
15 // * * //
16 // * This file implements the steering main for PD2 * //
17 // * The tests can be run as a standalone program or with the interpreter. * //
18 // * * //
19 // ************************************************************************* //
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "TMacro.h"
25 #include "TString.h"
26 #include "TSystem.h"
27 #include "TUUID.h"
28 
29 #include "pq2actions.h"
30 #include "pq2ping.h"
31 
32 // Local prototype and global variables
33 void showFile(const char *fn, int show, int keep);
34 static Int_t gkeep = 0;
35 
36 // Global variables used by other PQ2 components
41 
42 //_____________________________batch only_____________________
43 int main(int argc, char **argv)
44 {
45 
46  // Request for help?
47  if (argc > 1 && !strcmp(argv[1],"-h")) {
48  printf(" \n");
49  printf(" PQ2 functionality\n");
50  printf(" \n");
51  printf(" Usage:\n");
52  printf(" \n");
53  printf(" $ ./pq2 [-h] [-v] [-k] <action> [-d datasetname|datasetfile] [-s server] -u serviceurl\n");
54  printf(" \n");
55  printf(" Arguments:\n");
56  printf(" -h prints this menu\n");
57  printf(" -v verbose mode\n");
58  printf(" -k keep temporary files\n");
59  printf(" <action> ls, ls-files, ls-files-server, info-server, put, rm, verify\n");
60  printf(" datasetname Name of the dataset; the wild card '*' is accepted: in \n");
61  printf(" such a case the full path - as shown by pq2-ls - must \n");
62  printf(" be given in quotes, e.g. \"/default/ganis/h1-set5*\"\n");
63  printf(" (applies to: ls-files, ls-files-server, rm, verify\n");
64  printf(" datasetfile Path to the file with the list of files in the dataset or \n");
65  printf(" directory with the files containing the file lists of the \n");
66  printf(" datasets to be registered; in the first case wildcards '*' \n");
67  printf(" can be specified in the file name, i.e. \"<dir>/fil*\" is ok \n");
68  printf(" but \"<dir>/*/file\" is not. In all cases the name of the \n");
69  printf(" dataset is the name of the file finally used\n");
70  printf(" (applies to: put)\n");
71  printf(" server Name of the server for which the information is wanted; can be in \n");
72  printf(" URL form \n");
73  printf(" (applies to: ls-files-server, info-server)\n");
74  printf(" serviceurl entry point of the service to be used to get the information (PROOF master\n");
75  printf(" or data server) in the form '[user@]host.domain[:port]'\n");
76  printf(" \n");
77  gSystem->Exit(0);
78  }
79 
80  // Parse options
81  const char *action = 0;
82  const char *url = 0;
83  const char *dataset = 0;
84  const char *servers = 0;
85  const char *options = 0;
86  const char *ignsrvs = 0;
87  const char *excsrvs = 0;
88  const char *metrics = 0;
89  const char *fout = 0;
90  const char *plot = 0;
91  const char *infile = 0;
92  const char *outfile = 0;
93  const char *redir = 0;
94  Int_t i = 1;
95  while (i < argc) {
96  if (!strcmp(argv[i],"-h")) {
97  // Ignore if not first argument
98  i++;
99  } else if (!strcmp(argv[i],"-d")) {
100  if (i+1 == argc || argv[i+1][0] == '-') {
101  Printf(" -d should be followed by a string: ignoring");
102  i++;
103  } else {
104  dataset = argv[i+1];
105  i += 2;
106  }
107  } else if (!strcmp(argv[i],"-o")) {
108  if (i+1 == argc || argv[i+1][0] == '-') {
109  Printf(" -o should be followed by a string: ignoring");
110  i++;
111  } else {
112  options = argv[i+1];
113  i += 2;
114  }
115  } else if (!strcmp(argv[i],"-e") ||!strcmp(argv[i],"--exclude") ) {
116  if (i+1 == argc || argv[i+1][0] == '-') {
117  Printf(" -e or --exclude should be followed by a string: ignoring");
118  i++;
119  } else {
120  excsrvs = argv[i+1];
121  i += 2;
122  }
123  } else if (!strcmp(argv[i],"-i") ||!strcmp(argv[i],"--ignore") ) {
124  if (i+1 == argc || argv[i+1][0] == '-') {
125  Printf(" -i or --ignore should be followed by a string: ignoring");
126  i++;
127  } else {
128  ignsrvs = argv[i+1];
129  i += 2;
130  }
131  } else if (!strcmp(argv[i],"-s") || !strcmp(argv[i],"--servers")) {
132  if (i+1 == argc || argv[i+1][0] == '-') {
133  Printf(" -s or --servers should be followed by a string: ignoring");
134  i++;
135  } else {
136  servers = argv[i+1];
137  i += 2;
138  }
139  } else if (!strcmp(argv[i],"-m")) {
140  if (i+1 == argc || argv[i+1][0] == '-') {
141  Printf(" -m should be followed by a string: ignoring");
142  i++;
143  } else {
144  metrics = argv[i+1];
145  i += 2;
146  }
147  } else if (!strcmp(argv[i],"-f")) {
148  if (i+1 == argc || argv[i+1][0] == '-') {
149  Printf(" -f should be followed by a string: ignoring");
150  i++;
151  } else {
152  fout = argv[i+1];
153  i += 2;
154  }
155  } else if (!strcmp(argv[i],"-r")) {
156  if (i+1 == argc || argv[i+1][0] == '-') {
157  Printf(" -r should be followed by a string: ignoring");
158  i++;
159  } else {
160  redir = argv[i+1];
161  i += 2;
162  }
163  } else if (!strcmp(argv[i],"-u")) {
164  if (i+1 == argc || argv[i+1][0] == '-') {
165  Printf(" -u should be followed by a string: ignoring");
166  i++;
167  } else {
168  url = argv[i+1];
169  i += 2;
170  }
171  } else if (!strcmp(argv[i],"--plot")) {
172  if (i+1 == argc || argv[i+1][0] == '-') {
173  plot = "plot.png";
174  i++;
175  } else {
176  plot = argv[i+1];
177  i += 2;
178  }
179  } else if (!strcmp(argv[i],"--infile")) {
180  if (i+1 == argc || argv[i+1][0] == '-') {
181  Printf(" --infile should be followed by a string: ignoring");
182  i++;
183  } else {
184  infile = argv[i+1];
185  i += 2;
186  }
187  } else if (!strcmp(argv[i],"--outfile")) {
188  if (i+1 == argc || argv[i+1][0] == '-') {
189  Printf(" --outfile should be followed by a string: ignoring");
190  i++;
191  } else {
192  outfile = argv[i+1];
193  i += 2;
194  }
195  } else if (!strncmp(argv[i],"-v",2)) {
196  gverbose++;
197  if (!strncmp(argv[i],"-vv", 3)) gverbose++;
198  if (!strncmp(argv[i],"-vvv", 4)) gverbose++;
199  i++;
200  } else if (!strncmp(argv[i],"-k",2)) {
201  gkeep++;
202  i++;
203  } else {
204  action = argv[i];
205  i++;
206  }
207  }
208  if (!action) {
209  Printf("Specifying an action is mandatory - exit");
210  gSystem->Exit(1);
211  }
212  if (gverbose > 0) Printf("action: %s (url: %s)", action, url);
213 
214  // Find out the action index
215  const int nact = 9;
216  const char *actions[nact] = { "ls", "ls-files", "ls-files-server",
217  "info-server", "put", "rm", "verify",
218  "ana-dist", "cache" };
219  const char *tags[nact] = { "ls", "lsfiles", "filessrv",
220  "infosrv", "put", "rm", "vfy", "anadist", "cache" };
221  const char *tag = 0;
222  Int_t iact = -1;
223  for (i = 0; i < nact; i++) {
224  if (action && !strcmp(action, actions[i])) {
225  iact = i;
226  tag = tags[i];
227  }
228  }
229  if (iact == -1) {
230  Printf("Unknown action: %d (%s)", iact, action ? action : "");
231  gSystem->Exit(1);
232  }
233 
234  // Unique temporary dir
235  if (gverbose > 0) Printf("Tmp dir: %s", gSystem->TempDirectory());
236  TString tdir(gSystem->TempDirectory());
238  if (ug) {
239  if (!(tdir.EndsWith(ug->fUser))) {
240  if (!(tdir.EndsWith("/"))) tdir += "/";
241  tdir += ug->fUser;
242  }
243  SafeDelete(ug);
244  }
245  if (gSystem->AccessPathName(tdir) && gSystem->mkdir(tdir, kTRUE) != 0) {
246  Printf("Could create temp directory at: %s", tdir.Data());
247  gSystem->Exit(1);
248  }
249  flog.Form("%s/pq2%s.log", tdir.Data(), tag);
250  ferr.Form("%s/pq2%s.err", tdir.Data(), tag);
251  fres.Form("%s/pq2%s.res", tdir.Data(), tag);
252  if (!gSystem->AccessPathName(ferr)) gSystem->Unlink(ferr);
253  if (!gSystem->AccessPathName(flog)) gSystem->Unlink(flog);
254  if (!gSystem->AccessPathName(fres)) gSystem->Unlink(ferr);
255 
256  // Check URL
257  bool def_proof= 0;
258  if (!url) {
259  // List of actions to be done via server
260  TString dsmgracts = getenv("PQ2DSSRVACTS") ? getenv("PQ2DSSRVACTS")
261  : "ls:lsfiles:filessrv:infosrv:anadist:cache:" ;
262  // Determine the server to be used
263  TString atag(TString::Format("%s:", tag));
264  if (dsmgracts.Contains(atag) && getenv("PQ2DSSRVURL")) {
265  url = getenv("PQ2DSSRVURL");
266  } else if (getenv("PROOFURL") || getenv("PQ2PROOFURL")) {
267  url = getenv("PQ2PROOFURL") ? getenv("PQ2PROOFURL") : getenv("PROOFURL");
268  def_proof = 1;
269  } else {
270  Printf("Specifying a service URL is mandatory - exit");
271  gSystem->Exit(1);
272  }
273  }
274  if (gverbose > 0) Printf("Checking URL: %s", url ? url : "--undef--");
275  Int_t urlrc = checkUrl(url, flog.Data(), def_proof);
276  if (urlrc < 0) {
277  Printf("Specified URL does not identifies a running service: %s", url);
278  gSystem->Exit(1);
279  }
280 
281  Int_t rc = 0;
282  try {
283  if (iact == 0) {
284  // ls
285  do_ls(dataset, options);
286 
287  } else if (iact == 1) {
288  // ls-files
289  do_ls_files_server(dataset, 0);
290 
291  } else if (iact == 2) {
292  // ls-files-server
293  do_ls_files_server(dataset, servers);
294 
295  } else if (iact == 3) {
296  // info-server
297  do_info_server(servers);
298 
299  } else if (iact == 4) {
300  // put
301  do_put(dataset, options);
302 
303  } else if (iact == 5) {
304  // rm
305  do_rm(dataset);
306 
307  } else if (iact == 6) {
308  // verify
309  rc = do_verify(dataset, options, redir);
310 
311  } else if (iact == 7) {
312  // ana-dist
313  do_anadist(dataset, servers, ignsrvs, excsrvs, metrics, fout, plot, outfile, infile);
314 
315  } else if (iact == 8) {
316  // cache
317  bool clear = (options && !strcmp(options, "clear")) ? 1 : 0;
318  do_cache(clear, dataset);
319 
320  } else {
321  // Unknown
322  Printf("Unknown action code: %d - Protocol error?", iact);
323  }
324  }
325  catch (std::exception& exp) {
326  Printf("Standard exception caught: '%s' - We exit whatever it is ...", exp.what());
327  gSystem->Exit(rc);
328  }
329  catch (const char *str) {
330  Printf("Exception thrown: %s", str);
331  }
332  // handle every exception
333  catch (...) {
334  Printf("Handle uncaugth exception, terminating");
335  }
336 
337  if (!gSystem->AccessPathName(ferr)) {
338  showFile(ferr, 1, gkeep);
339  } else {
340  if (!gSystem->AccessPathName(flog)) showFile(flog, gverbose, gkeep);
341  if (!gSystem->AccessPathName(fres)) showFile(fres, 1, gkeep);
342  }
343  if (gkeep > 0) {
344  Printf("Temporary files kept: ");
345  if (!gSystem->AccessPathName(ferr)) Printf(" -> %s", ferr.Data());
346  if (!gSystem->AccessPathName(flog)) Printf(" -> %s", flog.Data());
347  if (!gSystem->AccessPathName(fres)) Printf(" -> %s", fres.Data());
348  }
349 
350  gSystem->Exit(rc);
351 }
352 
353 ////////////////////////////////////////////////////////////////////////////////
354 /// Display the content of file 'fn'
355 
356 void showFile(const char *fn, int show, int keep)
357 {
358  if (fn && strlen(fn)) {
359  FileStat_t st;
360  if (gSystem->GetPathInfo(fn, st) != 0 || !R_ISREG(st.fMode)) {
361  Printf("File '%s' cannot be stated or is not regular: ignoring", fn);
362  return;
363  }
364  if (show > 0) { TMacro m(fn); m.Print(); }
365  if (keep == 0) gSystem->Unlink(fn);
366  }
367 }
static Int_t gkeep
Definition: pq2main.cxx:34
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
void do_info_server(const char *server)
Execute 'info-server'.
Definition: pq2actions.cxx:204
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition: TSystem.cxx:1363
Int_t gverbose
Definition: pq2main.cxx:40
Class supporting a collection of lines with C++ code.
Definition: TMacro.h:33
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1447
Basic string class.
Definition: TString.h:137
virtual void Print(Option_t *option="") const
Print contents of this macro.
Definition: TMacro.cxx:321
int Int_t
Definition: RtypesCore.h:41
void do_anadist(const char *ds, const char *newsrvs=0, const char *ignsrvs=0, const char *excsrvs=0, const char *metrics="F", const char *fout=0, const char *plot=0, const char *outfile=0, const char *infile=0)
Execute 'analyze-distribution' for the dataset(s) described by 'ds'.
Definition: pq2actions.cxx:530
Int_t checkUrl(const char *url, const char *flog, bool def_proof=0)
Check if something is running at gUrl Return 0 if OK and data server 1 if OK and PROOF server -1 if n...
Definition: pq2ping.cxx:62
Bool_t R_ISREG(Int_t mode)
Definition: TSystem.h:129
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition: TSystem.cxx:900
Int_t fMode
Definition: TSystem.h:138
const char * Data() const
Definition: TString.h:349
#define SafeDelete(p)
Definition: RConfig.h:436
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1346
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2334
void do_ls(const char *ds, const char *opt="")
Execute 'ls'.
Definition: pq2actions.cxx:71
virtual UserGroup_t * GetUserInfo(Int_t uid)
Returns all user info in the UserGroup_t structure.
Definition: TSystem.cxx:1563
TString ferr
Definition: pq2main.cxx:38
TString fUser
Definition: TSystem.h:152
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
void do_cache(bool clear=1, const char *ds=0)
Execute 'cache'.
Definition: pq2actions.cxx:60
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2321
TMarker * m
Definition: textangle.C:8
void do_put(const char *ds, const char *opt)
Execute 'put'.
Definition: pq2actions.cxx:278
#define Printf
Definition: TGeoToOCC.h:18
void do_rm(const char *ds)
Execute 'rm'.
Definition: pq2actions.cxx:391
int main(int argc, char **argv)
Definition: pq2main.cxx:43
TString flog
Definition: pq2main.cxx:37
TString fres
Definition: pq2main.cxx:39
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void showFile(const char *fn, int show, int keep)
Display the content of file 'fn'.
Definition: pq2main.cxx:356
virtual Int_t GetUid(const char *user=0)
Returns the user's id. If user = 0, returns current user's id.
Definition: TSystem.cxx:1524
virtual void Exit(int code, Bool_t mode=kTRUE)
Exit the application.
Definition: TSystem.cxx:720
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
void do_ls_files_server(const char *ds, const char *server)
Execute 'ls-files'.
Definition: pq2actions.cxx:82
double exp(double)
const Bool_t kTRUE
Definition: Rtypes.h:91
int do_verify(const char *ds, const char *opt=0, const char *redir=0)
Execute 'verify'.
Definition: pq2actions.cxx:459