Logo ROOT   6.14/05
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 void PrintDeprecation(bool withctx = true)
43 {
44  if (withctx) printf(" \n");
45  printf(" NB: The PQ2 tools are deprecated and not maintained any longer and will be removed in ROOT v6.16/00\n");
46  printf(" Please contact the ROOT team in the unlikely event this change is disruptive for your workflow.\n");
47  if (withctx) printf(" \n");
48 }
49 
50 
51 //_____________________________batch only_____________________
52 int main(int argc, char **argv)
53 {
54 
55  // Request for help?
56  if (argc > 1 && !strcmp(argv[1],"-h")) {
57  printf(" \n");
58  printf(" PQ2 functionality\n");
59  printf(" \n");
60  printf(" Usage:\n");
61  printf(" \n");
62  printf(" $ ./pq2 [-h] [-v] [-k] <action> [-d datasetname|datasetfile] [-s server] -u serviceurl\n");
63  printf(" \n");
64  printf(" Arguments:\n");
65  printf(" -h prints this menu\n");
66  printf(" -v verbose mode\n");
67  printf(" -k keep temporary files\n");
68  printf(" <action> ls, ls-files, ls-files-server, info-server, put, rm, verify\n");
69  printf(" datasetname Name of the dataset; the wild card '*' is accepted: in \n");
70  printf(" such a case the full path - as shown by pq2-ls - must \n");
71  printf(" be given in quotes, e.g. \"/default/ganis/h1-set5*\"\n");
72  printf(" (applies to: ls-files, ls-files-server, rm, verify\n");
73  printf(" datasetfile Path to the file with the list of files in the dataset or \n");
74  printf(" directory with the files containing the file lists of the \n");
75  printf(" datasets to be registered; in the first case wildcards '*' \n");
76  printf(" can be specified in the file name, i.e. \"<dir>/fil*\" is ok \n");
77  printf(" but \"<dir>/*/file\" is not. In all cases the name of the \n");
78  printf(" dataset is the name of the file finally used\n");
79  printf(" (applies to: put)\n");
80  printf(" server Name of the server for which the information is wanted; can be in \n");
81  printf(" URL form \n");
82  printf(" (applies to: ls-files-server, info-server)\n");
83  printf(" serviceurl entry point of the service to be used to get the information (PROOF master\n");
84  printf(" or data server) in the form '[user@]host.domain[:port]'\n");
85  printf(" \n");
86  PrintDeprecation(false);
87  printf(" \n");
88  gSystem->Exit(0);
89  }
90 
92 
93  // Parse options
94  const char *action = 0;
95  const char *url = 0;
96  const char *dataset = 0;
97  const char *servers = 0;
98  const char *options = 0;
99  const char *ignsrvs = 0;
100  const char *excsrvs = 0;
101  const char *metrics = 0;
102  const char *fout = 0;
103  const char *plot = 0;
104  const char *infile = 0;
105  const char *outfile = 0;
106  const char *redir = 0;
107  Int_t i = 1;
108  while (i < argc) {
109  if (!strcmp(argv[i],"-h")) {
110  // Ignore if not first argument
111  i++;
112  } else if (!strcmp(argv[i],"-d")) {
113  if (i+1 == argc || argv[i+1][0] == '-') {
114  Printf(" -d should be followed by a string: ignoring");
115  i++;
116  } else {
117  dataset = argv[i+1];
118  i += 2;
119  }
120  } else if (!strcmp(argv[i],"-o")) {
121  if (i+1 == argc || argv[i+1][0] == '-') {
122  Printf(" -o should be followed by a string: ignoring");
123  i++;
124  } else {
125  options = argv[i+1];
126  i += 2;
127  }
128  } else if (!strcmp(argv[i],"-e") ||!strcmp(argv[i],"--exclude") ) {
129  if (i+1 == argc || argv[i+1][0] == '-') {
130  Printf(" -e or --exclude should be followed by a string: ignoring");
131  i++;
132  } else {
133  excsrvs = argv[i+1];
134  i += 2;
135  }
136  } else if (!strcmp(argv[i],"-i") ||!strcmp(argv[i],"--ignore") ) {
137  if (i+1 == argc || argv[i+1][0] == '-') {
138  Printf(" -i or --ignore should be followed by a string: ignoring");
139  i++;
140  } else {
141  ignsrvs = argv[i+1];
142  i += 2;
143  }
144  } else if (!strcmp(argv[i],"-s") || !strcmp(argv[i],"--servers")) {
145  if (i+1 == argc || argv[i+1][0] == '-') {
146  Printf(" -s or --servers should be followed by a string: ignoring");
147  i++;
148  } else {
149  servers = argv[i+1];
150  i += 2;
151  }
152  } else if (!strcmp(argv[i],"-m")) {
153  if (i+1 == argc || argv[i+1][0] == '-') {
154  Printf(" -m should be followed by a string: ignoring");
155  i++;
156  } else {
157  metrics = argv[i+1];
158  i += 2;
159  }
160  } else if (!strcmp(argv[i],"-f")) {
161  if (i+1 == argc || argv[i+1][0] == '-') {
162  Printf(" -f should be followed by a string: ignoring");
163  i++;
164  } else {
165  fout = argv[i+1];
166  i += 2;
167  }
168  } else if (!strcmp(argv[i],"-r")) {
169  if (i+1 == argc || argv[i+1][0] == '-') {
170  Printf(" -r should be followed by a string: ignoring");
171  i++;
172  } else {
173  redir = argv[i+1];
174  i += 2;
175  }
176  } else if (!strcmp(argv[i],"-u")) {
177  if (i+1 == argc || argv[i+1][0] == '-') {
178  Printf(" -u should be followed by a string: ignoring");
179  i++;
180  } else {
181  url = argv[i+1];
182  i += 2;
183  }
184  } else if (!strcmp(argv[i],"--plot")) {
185  if (i+1 == argc || argv[i+1][0] == '-') {
186  plot = "plot.png";
187  i++;
188  } else {
189  plot = argv[i+1];
190  i += 2;
191  }
192  } else if (!strcmp(argv[i],"--infile")) {
193  if (i+1 == argc || argv[i+1][0] == '-') {
194  Printf(" --infile should be followed by a string: ignoring");
195  i++;
196  } else {
197  infile = argv[i+1];
198  i += 2;
199  }
200  } else if (!strcmp(argv[i],"--outfile")) {
201  if (i+1 == argc || argv[i+1][0] == '-') {
202  Printf(" --outfile should be followed by a string: ignoring");
203  i++;
204  } else {
205  outfile = argv[i+1];
206  i += 2;
207  }
208  } else if (!strncmp(argv[i],"-v",2)) {
209  gverbose++;
210  if (!strncmp(argv[i],"-vv", 3)) gverbose++;
211  if (!strncmp(argv[i],"-vvv", 4)) gverbose++;
212  i++;
213  } else if (!strncmp(argv[i],"-k",2)) {
214  gkeep++;
215  i++;
216  } else {
217  action = argv[i];
218  i++;
219  }
220  }
221  if (!action) {
222  Printf("Specifying an action is mandatory - exit");
223  gSystem->Exit(1);
224  }
225  if (gverbose > 0) Printf("action: %s (url: %s)", action, url);
226 
227  // Find out the action index
228  const int nact = 9;
229  const char *actions[nact] = { "ls", "ls-files", "ls-files-server",
230  "info-server", "put", "rm", "verify",
231  "ana-dist", "cache" };
232  const char *tags[nact] = { "ls", "lsfiles", "filessrv",
233  "infosrv", "put", "rm", "vfy", "anadist", "cache" };
234  const char *tag = 0;
235  Int_t iact = -1;
236  for (i = 0; i < nact; i++) {
237  if (action && !strcmp(action, actions[i])) {
238  iact = i;
239  tag = tags[i];
240  }
241  }
242  if (iact == -1) {
243  Printf("Unknown action: %d (%s)", iact, action ? action : "");
244  gSystem->Exit(1);
245  }
246 
247  // Unique temporary dir
248  if (gverbose > 0) Printf("Tmp dir: %s", gSystem->TempDirectory());
249  TString tdir(gSystem->TempDirectory());
251  if (ug) {
252  if (!(tdir.EndsWith(ug->fUser))) {
253  if (!(tdir.EndsWith("/"))) tdir += "/";
254  tdir += ug->fUser;
255  }
256  SafeDelete(ug);
257  }
258  if (gSystem->AccessPathName(tdir) && gSystem->mkdir(tdir, kTRUE) != 0) {
259  Printf("Could create temp directory at: %s", tdir.Data());
260  gSystem->Exit(1);
261  }
262  flog.Form("%s/pq2%s.log", tdir.Data(), tag);
263  ferr.Form("%s/pq2%s.err", tdir.Data(), tag);
264  fres.Form("%s/pq2%s.res", tdir.Data(), tag);
265  if (!gSystem->AccessPathName(ferr)) gSystem->Unlink(ferr);
266  if (!gSystem->AccessPathName(flog)) gSystem->Unlink(flog);
267  if (!gSystem->AccessPathName(fres)) gSystem->Unlink(fres);
268 
269  // Check URL
270  bool def_proof= 0;
271  if (!url) {
272  // List of actions to be done via server
273  TString dsmgracts = getenv("PQ2DSSRVACTS") ? getenv("PQ2DSSRVACTS")
274  : "ls:lsfiles:filessrv:infosrv:anadist:cache:" ;
275  // Determine the server to be used
276  TString atag(TString::Format("%s:", tag));
277  if (dsmgracts.Contains(atag) && getenv("PQ2DSSRVURL")) {
278  url = getenv("PQ2DSSRVURL");
279  } else if (getenv("PROOFURL") || getenv("PQ2PROOFURL")) {
280  url = getenv("PQ2PROOFURL") ? getenv("PQ2PROOFURL") : getenv("PROOFURL");
281  def_proof = 1;
282  } else {
283  Printf("Specifying a service URL is mandatory - exit");
284  gSystem->Exit(1);
285  }
286  }
287  if (gverbose > 0) Printf("Checking URL: %s", url ? url : "--undef--");
288  Int_t urlrc = checkUrl(url, flog.Data(), def_proof);
289  if (urlrc < 0) {
290  Printf("Specified URL does not identifies a running service: %s", url);
291  gSystem->Exit(1);
292  }
293 
294  Int_t rc = 0;
295  try {
296  if (iact == 0) {
297  // ls
298  do_ls(dataset, options);
299 
300  } else if (iact == 1) {
301  // ls-files
302  do_ls_files_server(dataset, 0);
303 
304  } else if (iact == 2) {
305  // ls-files-server
306  do_ls_files_server(dataset, servers);
307 
308  } else if (iact == 3) {
309  // info-server
310  do_info_server(servers);
311 
312  } else if (iact == 4) {
313  // put
314  do_put(dataset, options);
315 
316  } else if (iact == 5) {
317  // rm
318  do_rm(dataset);
319 
320  } else if (iact == 6) {
321  // verify
322  rc = do_verify(dataset, options, redir);
323 
324  } else if (iact == 7) {
325  // ana-dist
326  do_anadist(dataset, servers, ignsrvs, excsrvs, metrics, fout, plot, outfile, infile);
327 
328  } else if (iact == 8) {
329  // cache
330  bool clear = (options && !strcmp(options, "clear")) ? 1 : 0;
331  do_cache(clear, dataset);
332 
333  } else {
334  // Unknown
335  Printf("Unknown action code: %d - Protocol error?", iact);
336  }
337  }
338  catch (std::exception& exp) {
339  Printf("Standard exception caught: '%s' - We exit whatever it is ...", exp.what());
340  gSystem->Exit(rc);
341  }
342  catch (const char *str) {
343  Printf("Exception thrown: %s", str);
344  }
345  // handle every exception
346  catch (...) {
347  Printf("Handle uncaugth exception, terminating");
348  }
349 
350  if (!gSystem->AccessPathName(ferr)) {
351  showFile(ferr, 1, gkeep);
352  } else {
353  if (!gSystem->AccessPathName(flog)) showFile(flog, gverbose, gkeep);
354  if (!gSystem->AccessPathName(fres)) showFile(fres, 1, gkeep);
355  }
356  if (gkeep > 0) {
357  Printf("Temporary files kept: ");
358  if (!gSystem->AccessPathName(ferr)) Printf(" -> %s", ferr.Data());
359  if (!gSystem->AccessPathName(flog)) Printf(" -> %s", flog.Data());
360  if (!gSystem->AccessPathName(fres)) Printf(" -> %s", fres.Data());
361  }
362 
364 
365  gSystem->Exit(rc);
366 }
367 
368 ////////////////////////////////////////////////////////////////////////////////
369 /// Display the content of file 'fn'
370 
371 void showFile(const char *fn, int show, int keep)
372 {
373  if (fn && strlen(fn)) {
374  FileStat_t st;
375  if (gSystem->GetPathInfo(fn, st) != 0 || !R_ISREG(st.fMode)) {
376  Printf("File '%s' cannot be stated or is not regular: ignoring", fn);
377  return;
378  }
379  if (show > 0) { TMacro m(fn); m.Print(); }
380  if (keep == 0) gSystem->Unlink(fn);
381  }
382 }
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:1276
void do_anadist(const char *ds, const char *servers, const char *ignsrvs, const char *excsrvs, const char *metrics, const char *fnout, const char *plot, const char *outfile, const char *infile)
Execute &#39;analyze-distribution&#39; for the dataset(s) described by &#39;ds&#39;.
Definition: pq2actions.cxx:527
auto * m
Definition: textangle.C:8
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:1374
Int_t gverbose
Definition: pq2main.cxx:40
Class supporting a collection of lines with C++ code.
Definition: TMacro.h:31
Basic string class.
Definition: TString.h:131
void do_rm(const char *dsname)
Execute &#39;rm&#39;.
Definition: pq2actions.cxx:388
int Int_t
Definition: RtypesCore.h:41
Bool_t R_ISREG(Int_t mode)
Definition: TSystem.h:119
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition: TSystem.cxx:904
Int_t fMode
Definition: TSystem.h:128
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1357
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:2286
virtual void Print(Option_t *option="") const
Print contents of this macro.
Definition: TMacro.cxx:323
virtual UserGroup_t * GetUserInfo(Int_t uid)
Returns all user info in the UserGroup_t structure.
Definition: TSystem.cxx:1574
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1458
TString ferr
Definition: pq2main.cxx:38
TString fUser
Definition: TSystem.h:142
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2264
void do_info_server(const char *server)
Execute &#39;info-server&#39;.
Definition: pq2actions.cxx:204
void do_cache(bool clear, const char *ds)
Execute &#39;cache&#39;.
Definition: pq2actions.cxx:60
#define Printf
Definition: TGeoToOCC.h:18
int main(int argc, char **argv)
Definition: pq2main.cxx:52
#define SafeDelete(p)
Definition: RConfig.h:529
int do_verify(const char *dsname, const char *opt, const char *redir)
Execute &#39;verify&#39;.
Definition: pq2actions.cxx:456
TString flog
Definition: pq2main.cxx:37
TString fres
Definition: pq2main.cxx:39
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
Int_t checkUrl(const char *url, const char *flog, bool def_proof)
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
void PrintDeprecation(bool withctx=true)
Definition: pq2main.cxx:42
void showFile(const char *fn, int show, int keep)
Display the content of file &#39;fn&#39;.
Definition: pq2main.cxx:371
virtual Int_t GetUid(const char *user=0)
Returns the user&#39;s id. If user = 0, returns current user&#39;s id.
Definition: TSystem.cxx:1535
void do_put(const char *files, const char *opt)
Execute &#39;put&#39;.
Definition: pq2actions.cxx:278
virtual void Exit(int code, Bool_t mode=kTRUE)
Exit the application.
Definition: TSystem.cxx:724
void do_ls(const char *ds, const char *opt)
Execute &#39;ls&#39;.
Definition: pq2actions.cxx:71
double exp(double)
const Bool_t kTRUE
Definition: RtypesCore.h:87
void do_ls_files_server(const char *ds, const char *server)
Execute &#39;ls-files&#39;.
Definition: pq2actions.cxx:82
const char * Data() const
Definition: TString.h:364