00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023
00024 #include "TMacro.h"
00025 #include "TString.h"
00026 #include "TSystem.h"
00027 #include "TUUID.h"
00028
00029 #include "pq2actions.h"
00030 #include "pq2ping.h"
00031
00032
00033 void showFile(const char *fn, int show, int keep);
00034 static Int_t gkeep = 0;
00035
00036
00037 TString flog;
00038 TString ferr;
00039 TString fres;
00040 Int_t gverbose = 0;
00041
00042
00043 int main(int argc,const char *argv[])
00044 {
00045
00046
00047 if (argc > 1 && !strcmp(argv[1],"-h")) {
00048 printf(" \n");
00049 printf(" PQ2 functionality\n");
00050 printf(" \n");
00051 printf(" Usage:\n");
00052 printf(" \n");
00053 printf(" $ ./pq2 [-h] [-v] [-k] <action> [-d datasetname|datasetfile] [-s server] -u serviceurl\n");
00054 printf(" \n");
00055 printf(" Arguments:\n");
00056 printf(" -h prints this menu\n");
00057 printf(" -v verbose mode\n");
00058 printf(" -k keep temporary files\n");
00059 printf(" <action> ls, ls-files, ls-files-server, info-server, put, rm, verify\n");
00060 printf(" datasetname Name of the dataset; the wild card '*' is accepted: in \n");
00061 printf(" such a case the full path - as shown by pq2-ls - must \n");
00062 printf(" be given in quotes, e.g. \"/default/ganis/h1-set5*\"\n");
00063 printf(" (applies to: ls-files, ls-files-server, rm, verify\n");
00064 printf(" datasetfile Path to the file with the list of files in the dataset or \n");
00065 printf(" directory with the files containing the file lists of the \n");
00066 printf(" datasets to be registered; in the first case wildcards '*' \n");
00067 printf(" can be specified in the file name, i.e. \"<dir>/fil*\" is ok \n");
00068 printf(" but \"<dir>/*/file\" is not. In all cases the name of the \n");
00069 printf(" dataset is the name of the file finally used\n");
00070 printf(" (applies to: put)\n");
00071 printf(" server Name of the server for which the information is wanted; can be in \n");
00072 printf(" URL form \n");
00073 printf(" (applies to: ls-files-server, info-server)\n");
00074 printf(" serviceurl entry point of the service to be used to get the information (PROOF master\n");
00075 printf(" or data server) in the form '[user@]host.domain[:port]'\n");
00076 printf(" \n");
00077 gSystem->Exit(0);
00078 }
00079
00080
00081 const char *action = 0;
00082 const char *url = 0;
00083 const char *dataset = 0;
00084 const char *servers = 0;
00085 const char *options = 0;
00086 const char *ignsrvs = 0;
00087 const char *excsrvs = 0;
00088 const char *metrics = 0;
00089 const char *fout = 0;
00090 const char *plot = 0;
00091 const char *infile = 0;
00092 const char *outfile = 0;
00093 const char *redir = 0;
00094 Int_t i = 1;
00095 while (i < argc) {
00096 if (!strcmp(argv[i],"-h")) {
00097
00098 i++;
00099 } else if (!strcmp(argv[i],"-d")) {
00100 if (i+1 == argc || argv[i+1][0] == '-') {
00101 Printf(" -d should be followed by a string: ignoring");
00102 i++;
00103 } else {
00104 dataset = argv[i+1];
00105 i += 2;
00106 }
00107 } else if (!strcmp(argv[i],"-o")) {
00108 if (i+1 == argc || argv[i+1][0] == '-') {
00109 Printf(" -o should be followed by a string: ignoring");
00110 i++;
00111 } else {
00112 options = argv[i+1];
00113 i += 2;
00114 }
00115 } else if (!strcmp(argv[i],"-e") ||!strcmp(argv[i],"--exclude") ) {
00116 if (i+1 == argc || argv[i+1][0] == '-') {
00117 Printf(" -e or --exclude should be followed by a string: ignoring");
00118 i++;
00119 } else {
00120 excsrvs = argv[i+1];
00121 i += 2;
00122 }
00123 } else if (!strcmp(argv[i],"-i") ||!strcmp(argv[i],"--ignore") ) {
00124 if (i+1 == argc || argv[i+1][0] == '-') {
00125 Printf(" -i or --ignore should be followed by a string: ignoring");
00126 i++;
00127 } else {
00128 ignsrvs = argv[i+1];
00129 i += 2;
00130 }
00131 } else if (!strcmp(argv[i],"-s") || !strcmp(argv[i],"--servers")) {
00132 if (i+1 == argc || argv[i+1][0] == '-') {
00133 Printf(" -s or --servers should be followed by a string: ignoring");
00134 i++;
00135 } else {
00136 servers = argv[i+1];
00137 i += 2;
00138 }
00139 } else if (!strcmp(argv[i],"-m")) {
00140 if (i+1 == argc || argv[i+1][0] == '-') {
00141 Printf(" -m should be followed by a string: ignoring");
00142 i++;
00143 } else {
00144 metrics = argv[i+1];
00145 i += 2;
00146 }
00147 } else if (!strcmp(argv[i],"-f")) {
00148 if (i+1 == argc || argv[i+1][0] == '-') {
00149 Printf(" -f should be followed by a string: ignoring");
00150 i++;
00151 } else {
00152 fout = argv[i+1];
00153 i += 2;
00154 }
00155 } else if (!strcmp(argv[i],"-r")) {
00156 if (i+1 == argc || argv[i+1][0] == '-') {
00157 Printf(" -r should be followed by a string: ignoring");
00158 i++;
00159 } else {
00160 redir = argv[i+1];
00161 i += 2;
00162 }
00163 } else if (!strcmp(argv[i],"-u")) {
00164 if (i+1 == argc || argv[i+1][0] == '-') {
00165 Printf(" -u should be followed by a string: ignoring");
00166 i++;
00167 } else {
00168 url = argv[i+1];
00169 i += 2;
00170 }
00171 } else if (!strcmp(argv[i],"--plot")) {
00172 if (i+1 == argc || argv[i+1][0] == '-') {
00173 plot = "plot.png";
00174 i++;
00175 } else {
00176 plot = argv[i+1];
00177 i += 2;
00178 }
00179 } else if (!strcmp(argv[i],"--infile")) {
00180 if (i+1 == argc || argv[i+1][0] == '-') {
00181 Printf(" --infile should be followed by a string: ignoring");
00182 i++;
00183 } else {
00184 infile = argv[i+1];
00185 i += 2;
00186 }
00187 } else if (!strcmp(argv[i],"--outfile")) {
00188 if (i+1 == argc || argv[i+1][0] == '-') {
00189 Printf(" --outfile should be followed by a string: ignoring");
00190 i++;
00191 } else {
00192 outfile = argv[i+1];
00193 i += 2;
00194 }
00195 } else if (!strncmp(argv[i],"-v",2)) {
00196 gverbose++;
00197 if (!strncmp(argv[i],"-vv", 3)) gverbose++;
00198 if (!strncmp(argv[i],"-vvv", 4)) gverbose++;
00199 i++;
00200 } else if (!strncmp(argv[i],"-k",2)) {
00201 gkeep++;
00202 i++;
00203 } else {
00204 action = argv[i];
00205 i++;
00206 }
00207 }
00208 if (!action) {
00209 Printf("Specifying an action is mandatory - exit");
00210 gSystem->Exit(1);
00211 }
00212 if (gverbose > 0) Printf("action: %s (url: %s)", action, url);
00213
00214
00215 const int nact = 9;
00216 const char *actions[nact] = { "ls", "ls-files", "ls-files-server",
00217 "info-server", "put", "rm", "verify",
00218 "ana-dist", "cache" };
00219 const char *tags[nact] = { "ls", "lsfiles", "filessrv",
00220 "infosrv", "put", "rm", "vfy", "anadist", "cache" };
00221 const char *tag = 0;
00222 Int_t iact = -1;
00223 for (i = 0; i < nact; i++) {
00224 if (action && !strcmp(action, actions[i])) {
00225 iact = i;
00226 tag = tags[i];
00227 }
00228 }
00229 if (iact == -1) {
00230 Printf("Unknown action: %d (%s)", iact, action ? action : "");
00231 gSystem->Exit(1);
00232 }
00233
00234
00235 if (gverbose > 0) Printf("Tmp dir: %s", gSystem->TempDirectory());
00236 TString tdir(gSystem->TempDirectory());
00237 UserGroup_t *ug = gSystem->GetUserInfo(gSystem->GetUid());
00238 if (ug) {
00239 if (!(tdir.EndsWith(ug->fUser))) {
00240 if (!(tdir.EndsWith("/"))) tdir += "/";
00241 tdir += ug->fUser;
00242 }
00243 SafeDelete(ug);
00244 }
00245 if (gSystem->AccessPathName(tdir) && gSystem->mkdir(tdir, kTRUE) != 0) {
00246 Printf("Could create temp directory at: %s", tdir.Data());
00247 gSystem->Exit(1);
00248 }
00249 flog.Form("%s/pq2%s.log", tdir.Data(), tag);
00250 ferr.Form("%s/pq2%s.err", tdir.Data(), tag);
00251 fres.Form("%s/pq2%s.res", tdir.Data(), tag);
00252 if (!gSystem->AccessPathName(ferr)) gSystem->Unlink(ferr);
00253 if (!gSystem->AccessPathName(flog)) gSystem->Unlink(flog);
00254 if (!gSystem->AccessPathName(fres)) gSystem->Unlink(ferr);
00255
00256
00257 bool def_proof= 0;
00258 if (!url) {
00259
00260 TString dsmgracts = getenv("PQ2DSSRVACTS") ? getenv("PQ2DSSRVACTS")
00261 : "ls:lsfiles:filessrv:infosrv:anadist:cache:" ;
00262
00263 TString atag(TString::Format("%s:", tag));
00264 if (dsmgracts.Contains(atag) && getenv("PQ2DSSRVURL")) {
00265 url = getenv("PQ2DSSRVURL");
00266 } else if (getenv("PROOFURL") || getenv("PQ2PROOFURL")) {
00267 url = getenv("PQ2PROOFURL") ? getenv("PQ2PROOFURL") : getenv("PROOFURL");
00268 def_proof = 1;
00269 } else {
00270 Printf("Specifying a service URL is mandatory - exit");
00271 gSystem->Exit(1);
00272 }
00273 }
00274 if (gverbose > 0) Printf("Checking URL: %s", url ? url : "--undef--");
00275 Int_t urlrc = checkUrl(url, flog.Data(), def_proof);
00276 if (urlrc < 0) {
00277 Printf("Specified URL does not identifies a running service: %s", url);
00278 gSystem->Exit(1);
00279 }
00280
00281 Int_t rc = 0;
00282 try {
00283 if (iact == 0) {
00284
00285 do_ls(dataset, options);
00286
00287 } else if (iact == 1) {
00288
00289 do_ls_files_server(dataset, 0);
00290
00291 } else if (iact == 2) {
00292
00293 do_ls_files_server(dataset, servers);
00294
00295 } else if (iact == 3) {
00296
00297 do_info_server(servers);
00298
00299 } else if (iact == 4) {
00300
00301 do_put(dataset, options);
00302
00303 } else if (iact == 5) {
00304
00305 do_rm(dataset);
00306
00307 } else if (iact == 6) {
00308
00309 rc = do_verify(dataset, options, redir);
00310
00311 } else if (iact == 7) {
00312
00313 do_anadist(dataset, servers, ignsrvs, excsrvs, metrics, fout, plot, outfile, infile);
00314
00315 } else if (iact == 8) {
00316
00317 bool clear = (options && !strcmp(options, "clear")) ? 1 : 0;
00318 do_cache(clear, dataset);
00319
00320 } else {
00321
00322 Printf("Unknown action code: %d - Protocol error?", iact);
00323 }
00324 }
00325 catch (std::exception& exc) {
00326 Printf("Standard exception caught: we abort whatever it is ...");
00327 throw;
00328 }
00329 catch (const char *str) {
00330 Printf("Exception thrown: %s", str);
00331 }
00332
00333 catch (...) {
00334 Printf("Handle uncaugth exception, terminating");
00335 }
00336
00337 if (!gSystem->AccessPathName(ferr)) {
00338 showFile(ferr, 1, gkeep);
00339 } else {
00340 if (!gSystem->AccessPathName(flog)) showFile(flog, gverbose, gkeep);
00341 if (!gSystem->AccessPathName(fres)) showFile(fres, 1, gkeep);
00342 }
00343 if (gkeep > 0) {
00344 Printf("Temporary files kept: ");
00345 if (!gSystem->AccessPathName(ferr)) Printf(" -> %s", ferr.Data());
00346 if (!gSystem->AccessPathName(flog)) Printf(" -> %s", flog.Data());
00347 if (!gSystem->AccessPathName(fres)) Printf(" -> %s", fres.Data());
00348 }
00349
00350 gSystem->Exit(rc);
00351 }
00352
00353
00354 void showFile(const char *fn, int show, int keep)
00355 {
00356
00357
00358 if (fn && strlen(fn)) {
00359 FileStat_t st;
00360 if (gSystem->GetPathInfo(fn, st) != 0 || !R_ISREG(st.fMode)) {
00361 Printf("File '%s' cannot be stated or is not regular: ignoring", fn);
00362 return;
00363 }
00364 if (show > 0) { TMacro m(fn); m.Print(); }
00365 if (keep == 0) gSystem->Unlink(fn);
00366 }
00367 }