00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <errno.h>
00021 #ifdef WIN32
00022 #include <io.h>
00023 #endif
00024
00025 #include "TProofMgrLite.h"
00026
00027 #include "Riostream.h"
00028 #include "TEnv.h"
00029 #include "TError.h"
00030 #include "TObjString.h"
00031 #include "TProofLite.h"
00032 #include "TProofLog.h"
00033 #include "TROOT.h"
00034 #include "TRegexp.h"
00035 #include "TSortedList.h"
00036
00037 ClassImp(TProofMgrLite)
00038
00039
00040 TProofMgrLite::TProofMgrLite(const char *url, Int_t dbg, const char *alias)
00041 : TProofMgr(url, dbg, alias)
00042 {
00043
00044
00045
00046 fServType = kProofLite;
00047 }
00048
00049
00050 TProof *TProofMgrLite::CreateSession(const char *cfg,
00051 const char *, Int_t loglevel)
00052 {
00053
00054
00055 Int_t nwrk = TProofLite::GetNumberOfWorkers(fUrl.GetOptions());
00056 if (nwrk == 0) return (TProof *)0;
00057
00058
00059 if (gProof && gProof->IsLite()) {
00060 if (gProof->IsValid()) {
00061 if (nwrk > 0 && gProof->GetParallel() != nwrk) {
00062 delete gProof;
00063 gProof = 0;
00064 } else {
00065
00066 return gProof;
00067 }
00068 } else {
00069
00070 delete gProof;
00071 gProof = 0;
00072 }
00073 }
00074
00075
00076 TString u = (strlen(fUrl.GetOptions()) > 0) ? Form("lite/?%s", fUrl.GetOptions())
00077 : "lite";
00078 TProof *p = new TProofLite(u, cfg, 0, loglevel, 0, this);
00079
00080 if (p && p->IsValid()) {
00081
00082
00083 Int_t ns = 1;
00084 if (fSessions) {
00085
00086 if (fSessions->Last())
00087 ns = ((TProofDesc *)(fSessions->Last()))->GetLocalId() + 1;
00088 } else {
00089
00090 fSessions = new TList;
00091 }
00092
00093
00094 Int_t st = (p->IsIdle()) ? TProofDesc::kIdle : TProofDesc::kRunning ;
00095 TProofDesc *d =
00096 new TProofDesc(p->GetName(), p->GetTitle(), p->GetUrl(),
00097 ns, p->GetSessionID(), st, p);
00098 fSessions->Add(d);
00099
00100 } else {
00101
00102 Error("CreateSession", "creating PROOF session");
00103 SafeDelete(p);
00104 }
00105
00106
00107 return p;
00108 }
00109
00110
00111 TProofLog *TProofMgrLite::GetSessionLogs(Int_t isess,
00112 const char *stag, const char *pattern)
00113 {
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 TProofLog *pl = 0;
00132
00133
00134 isess = (isess < 0) ? -isess : isess;
00135
00136
00137 bool retrieve = 1;
00138 TString tag(stag);
00139 if (tag == "NR") {
00140 retrieve = 0;
00141 tag = "";
00142 }
00143
00144
00145 TString sandbox(gSystem->WorkingDirectory());
00146 sandbox.ReplaceAll(gSystem->HomeDirectory(),"");
00147 sandbox.ReplaceAll("/","-");
00148 sandbox.Replace(0,1,"/",1);
00149 if (strlen(gEnv->GetValue("ProofLite.Sandbox", "")) > 0) {
00150 sandbox.Insert(0, gEnv->GetValue("ProofLite.Sandbox", ""));
00151 } else if (strlen(gEnv->GetValue("Proof.Sandbox", "")) > 0) {
00152 sandbox.Insert(0, gEnv->GetValue("Proof.Sandbox", ""));
00153 } else {
00154 TString sb;
00155 sb.Form("~/%s", kPROOF_WorkDir);
00156 sandbox.Insert(0, sb.Data());
00157 }
00158 gSystem->ExpandPathName(sandbox);
00159
00160 TString sessiondir;
00161 if (tag.Length() > 0) {
00162 sessiondir.Form("%s/session-%s", sandbox.Data(), tag.Data());
00163 if (gSystem->AccessPathName(sessiondir, kReadPermission)) {
00164 Error("GetSessionLogs", "information for session '%s' not available", tag.Data());
00165 return (TProofLog *)0;
00166 }
00167 } else {
00168
00169 TSortedList *olddirs = new TSortedList(kFALSE);
00170 void *dirp = gSystem->OpenDirectory(sandbox);
00171 if (dirp) {
00172 const char *e = 0;
00173 while ((e = gSystem->GetDirEntry(dirp))) {
00174 if (!strncmp(e, "session-", 8)) {
00175 TString d(e);
00176 Int_t i = d.Last('-');
00177 if (i != kNPOS) d.Remove(i);
00178 i = d.Last('-');
00179 if (i != kNPOS) d.Remove(0,i+1);
00180 TString path = Form("%s/%s", sandbox.Data(), e);
00181 olddirs->Add(new TNamed(d, path));
00182 }
00183 }
00184 gSystem->FreeDirectory(dirp);
00185 }
00186
00187
00188 if (isess > olddirs->GetSize() - 1) {
00189 Warning("GetSessionLogs",
00190 "session index out of range (%d): take oldest available session", isess);
00191 isess = olddirs->GetSize() - 1;
00192 }
00193
00194
00195 Int_t isx = isess;
00196 TNamed *n = (TNamed *) olddirs->First();
00197 while (isx-- > 0) {
00198 olddirs->Remove(n);
00199 delete n;
00200 n = (TNamed *) olddirs->First();
00201 }
00202 if (!n) {
00203 Error("GetSessionLogs", "cannot locate session dir for index '%d' under '%s':"
00204 " cannot continue!", isess, sandbox.Data());
00205 return (TProofLog *)0;
00206 }
00207 sessiondir = n->GetTitle();
00208 tag = gSystem->BaseName(sessiondir);
00209 tag.ReplaceAll("session-", "");
00210
00211
00212 olddirs->SetOwner();
00213 delete olddirs;
00214 }
00215 Info("GetSessionLogs", "analysing session dir %s", sessiondir.Data());
00216
00217
00218 pl = new TProofLog(tag, "", this);
00219
00220 void *dirp = gSystem->OpenDirectory(sessiondir);
00221 if (dirp) {
00222 TSortedList *logs = new TSortedList;
00223 const char *e = 0;
00224 while ((e = gSystem->GetDirEntry(dirp))) {
00225 TString fn(e);
00226 if (fn.EndsWith(".log") && fn.CountChar('-') > 0) {
00227 TString ord, url;
00228 if (fn.BeginsWith("session-")) {
00229 ord = "-1";
00230 } else if (fn.BeginsWith("worker-")) {
00231 ord = fn;
00232 ord.ReplaceAll("worker-", "");
00233 Int_t id = ord.First('-');
00234 if (id != kNPOS) {
00235 ord.Remove(id);
00236 } else if (ord.Contains(".valgrind")) {
00237
00238 ord.ReplaceAll(".valgrind.log","-valgrind");
00239 } else {
00240
00241 ord = "";
00242 }
00243 if (!ord.IsNull()) ord.ReplaceAll("0.", "");
00244 }
00245 if (!ord.IsNull()) {
00246 url = Form("%s/%s", sessiondir.Data(), e);
00247
00248 logs->Add(new TNamed(ord, url));
00249
00250 if (gDebug > 1)
00251 Info("GetSessionLogs", "ord: %s, url: %s", ord.Data(), url.Data());
00252 }
00253 }
00254 }
00255 gSystem->FreeDirectory(dirp);
00256
00257 TIter nxl(logs);
00258 TNamed *n = 0;
00259 while ((n = (TNamed *) nxl())) {
00260 TString ord = Form("0.%s", n->GetName());
00261 if (ord == "0.-1") ord = "0";
00262
00263 pl->Add(ord, n->GetTitle());
00264 }
00265
00266
00267 logs->SetOwner();
00268 delete logs;
00269 }
00270
00271
00272 if (pl && retrieve) {
00273 if (pattern && strlen(pattern) > 0)
00274 pl->Retrieve("*", TProofLog::kGrep, 0, pattern);
00275 else
00276 pl->Retrieve();
00277 }
00278
00279
00280 return pl;
00281 }
00282
00283
00284 TObjString *TProofMgrLite::ReadBuffer(const char *fin, Long64_t ofs, Int_t len)
00285 {
00286
00287
00288
00289 if (!fin || strlen(fin) <= 0) {
00290 Error("ReadBuffer", "undefined path!");
00291 return (TObjString *)0;
00292 }
00293
00294
00295 TString fn = TUrl(fin).GetFile();
00296 Int_t fd = open(fn.Data(), O_RDONLY);
00297 if (fd < 0) {
00298 Error("ReadBuffer", "problems opening file %s", fn.Data());
00299 return (TObjString *)0;
00300 }
00301
00302
00303 off_t start = 0, end = lseek(fd, (off_t) 0, SEEK_END);
00304
00305
00306 if (ofs > 0 && ofs < end) {
00307 start = lseek(fd, (off_t) ofs, SEEK_SET);
00308 } else {
00309 start = lseek(fd, (off_t) 0, SEEK_SET);
00310 }
00311 if (len > (end - start + 1) || len <= 0)
00312 len = end - start + 1;
00313
00314 TString outbuf;
00315 const Int_t kMAXBUF = 32768;
00316 char buf[kMAXBUF];
00317 Int_t left = len;
00318 Int_t wanted = (left > kMAXBUF - 1) ? kMAXBUF - 1 : left;
00319 do {
00320 while ((len = read(fd, buf, wanted)) < 0 && TSystem::GetErrno() == EINTR)
00321 TSystem::ResetErrno();
00322
00323 if (len < 0) {
00324 Error("ReadBuffer", "error reading file %s", fn.Data());
00325 close(fd);
00326 return (TObjString *)0;
00327 } else if (len > 0) {
00328 if (len == wanted)
00329 buf[len-1] = '\n';
00330 buf[len] = '\0';
00331 outbuf += buf;
00332 }
00333
00334
00335 left -= len;
00336 wanted = (left > kMAXBUF - 1) ? kMAXBUF - 1 : left;
00337
00338 } while (len > 0 && left > 0);
00339
00340
00341 return new TObjString(outbuf.Data());
00342 }
00343
00344
00345 TObjString *TProofMgrLite::ReadBuffer(const char *fin, const char *pattern)
00346 {
00347
00348
00349
00350
00351 if (!pattern || strlen(pattern) <= 0)
00352 return (TObjString *)0;
00353
00354 if (!fin || strlen(fin) <= 0) {
00355 Error("ReadBuffer", "undefined path!");
00356 return (TObjString *)0;
00357 }
00358 TString fn = TUrl(fin).GetFile();
00359
00360 TString pat(pattern);
00361
00362 Bool_t excl = kFALSE;
00363 if (pat.Contains("-v ")) {
00364 pat.ReplaceAll("-v ", "");
00365 excl = kTRUE;
00366 }
00367 pat = pat.Strip(TString::kLeading, ' ');
00368 pat = pat.Strip(TString::kTrailing, ' ');
00369 pat = pat.Strip(TString::kLeading, '\"');
00370 pat = pat.Strip(TString::kTrailing, '\"');
00371
00372
00373 TRegexp re(pat);
00374
00375
00376 ifstream in;
00377 in.open(fn.Data());
00378
00379 TString outbuf;
00380
00381
00382 TString line;
00383 while(in.good()) {
00384
00385
00386 line.ReadLine(in);
00387
00388
00389 if ((excl && line.Index(re) != kNPOS) ||
00390 (!excl && line.Index(re) == kNPOS)) continue;
00391
00392
00393 if (!line.EndsWith("\n")) line.Append('\n');
00394
00395
00396 outbuf += line;
00397 }
00398 in.close();
00399
00400
00401 return new TObjString(outbuf.Data());
00402 }