00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include <stdlib.h>
00064 #include "TAlien.h"
00065 #include "TUrl.h"
00066 #include "Riostream.h"
00067 #include "TString.h"
00068 #include "TObjString.h"
00069 #include "TObjArray.h"
00070 #include "TMap.h"
00071 #include "TSystem.h"
00072 #include "TAlienJDL.h"
00073 #include "TAlienResult.h"
00074 #include "TAlienJob.h"
00075 #include "TAlienCollection.h"
00076 #include "TAlienMasterJobStatus.h"
00077 #include "TAlienJobStatusList.h"
00078 #include "TAlienJobStatus.h"
00079 #include "gapiUI.h"
00080 #include "gapi_job_operations.h"
00081
00082 using namespace std;
00083
00084
00085
00086 ClassImp(TAlien)
00087
00088
00089 TAlien::TAlien(const char *gridurl, const char *uid, const char * passwd,
00090 const char *options)
00091 {
00092 gSystem->Unsetenv("GCLIENT_EXTRA_ARG");
00093
00094
00095 TUrl *gurl = new TUrl(gridurl);
00096
00097 fGridUrl = gridurl;
00098 fGrid = "alien";
00099 if (!strlen(gurl->GetHost())) {
00100 if (gSystem->Getenv("alien_API_HOST"))
00101 fHost = gSystem->Getenv("alien_API_HOST");
00102 else
00103 fHost = "";
00104 } else {
00105 fHost = gurl->GetHost();
00106 }
00107
00108 if (gurl->GetPort()<=0) {
00109 if (gSystem->Getenv("alien_API_PORT"))
00110 fPort = atoi(gSystem->Getenv("alien_API_PORT"));
00111 else
00112 fPort = 0;
00113 } else {
00114 fPort = gurl->GetPort();
00115 }
00116
00117 if (!strlen(uid)) {
00118 if (gSystem->Getenv("alien_API_USER")) {
00119 fUser = gSystem->Getenv("alien_API_USER");
00120 } else {
00121 if (gSystem->Getenv("LOGNAME")) {
00122
00123 fUser = gSystem->Getenv("LOGNAME");
00124 } else {
00125
00126 fUser = gSystem->Getenv("USER");
00127 }
00128 }
00129 } else {
00130 fUser = uid;
00131 }
00132
00133 fOptions = options;
00134
00135 if (gDebug > 1)
00136 Info("TAlien", "%s => %s port: %d user: %s",gridurl,fHost.Data(),fPort,fUser.Data());
00137
00138 Bool_t fstoken;
00139 fstoken = kFALSE;
00140
00141 if (options && (options[0] == 't')) {
00142 fGc = GapiUI::MakeGapiUI(kTRUE);
00143 fstoken = kTRUE;
00144 } else {
00145 fGc = GapiUI::MakeGapiUI(kFALSE);
00146 }
00147
00148 if (!fGc) {
00149 Error("TAlien", "could not connect to a alien service at:");
00150 Error("TAlien", "host: %s port: %d user: %s", fHost.Data(), fPort, fUser.Data());
00151 MakeZombie();
00152 } else {
00153 if (passwd) {
00154 if (!strlen(passwd)) {
00155 passwd = 0;
00156 }
00157 }
00158 if (fstoken) {
00159 if (!Command("motd")) {
00160 Error("TAlien", "we have no valid connection ... I try to connect ...");
00161 fGc->Connect(fHost, fPort, fUser, passwd);
00162 }
00163 } else {
00164 fGc->Connect(fHost, fPort, fUser, passwd);
00165 }
00166
00167 char cwd[1024];
00168 const char* cwdp=0;
00169 cwdp=Pwd();
00170 fHome=TString("/");
00171 if (cwdp) {
00172 sprintf(cwd,"%s",cwdp);
00173 if (Cd("")) {
00174 cwdp=Pwd();
00175 if (cwdp) {
00176 fHome = TString(cwdp);
00177 }
00178 }
00179 }
00180
00181 if (!fGc->Connected()) {
00182 Error("TAlien", "could not authenticate at:");
00183 Error("TAlien", "host: %s port: %d user: %s",fHost.Data(),fPort,fUser.Data());
00184 MakeZombie();
00185 } else {
00186 gGrid = this;
00187 Command("motd");
00188 Stdout();
00189 }
00190
00191 fGc->SetGapiUI(fGc);
00192 }
00193 delete gurl;
00194 }
00195
00196
00197 TAlien::~TAlien()
00198 {
00199
00200
00201 if (gDebug > 1)
00202 Info("~TAlien", "destructor called");
00203 }
00204
00205
00206 void TAlien::Shell()
00207 {
00208
00209
00210 fGc->Shell();
00211 }
00212
00213
00214 TString TAlien::Escape(const char *input)
00215 {
00216
00217
00218 if (!input)
00219 return TString();
00220
00221 TString output(input);
00222 output.ReplaceAll("\"", "\\\"");
00223
00224 return output;
00225 }
00226
00227
00228 TGridJob *TAlien::Submit(const char *jdl)
00229 {
00230
00231
00232 if (!jdl)
00233 return 0;
00234
00235 TString command("submit =< ");
00236
00237 command += jdl;
00238
00239 cout << command << endl;
00240
00241 TGridResult* result = Command(command,kFALSE,kOUTPUT);
00242 TAlienResult* alienResult = dynamic_cast<TAlienResult*>(result);
00243 TList* list = dynamic_cast<TList*>(alienResult);
00244 if (!list) {
00245 if (result)
00246 delete result;
00247 return 0;
00248 }
00249
00250 alienResult->DumpResult();
00251
00252 TString jobID = "0";
00253
00254 TIterator* iter = list->MakeIterator();
00255 TObject* object = 0;
00256 while ((object = iter->Next()) != 0) {
00257 TMap* map = dynamic_cast<TMap*>(object);
00258
00259 TObject* jobIDObject = map->GetValue("jobId");
00260 TObjString* jobIDStr = dynamic_cast<TObjString*>(jobIDObject);
00261 if (jobIDStr) {
00262 jobID = jobIDStr->GetString();
00263 }
00264 }
00265 delete iter;
00266 delete result;
00267
00268 if (jobID == "0") {
00269 Error("Submit", "error submitting job");
00270 return 0;
00271 }
00272
00273 Info("Submit", "your job was submitted with the ID = %s", jobID.Data());
00274
00275 return dynamic_cast<TGridJob*>(new TAlienJob(jobID));
00276 }
00277
00278
00279 TGridJDL *TAlien::GetJDLGenerator()
00280 {
00281
00282
00283 return new TAlienJDL();
00284 }
00285
00286
00287 TGridJobStatusList *TAlien::Ps(const char* , Bool_t verbose)
00288 {
00289
00290
00291 GAPI_JOBARRAY* gjobarray = gapi_queryjobs("-", gGrid->GetUser(), "-", "-", "-",
00292 "-", "-", "-", "-");
00293 if (!gjobarray)
00294 return 0;
00295
00296 if (gjobarray->size() == 0) {
00297 delete gjobarray;
00298 return 0;
00299 }
00300
00301 TGridJobStatusList* joblist = new TAlienJobStatusList();
00302
00303 for (UInt_t i = 0; i < gjobarray->size(); i++ ) {
00304 TMap* statusmap = new TMap();
00305 GAPI_JOB gjob = gjobarray->at(i);
00306 std::map<std::string, std::string>::const_iterator iter = gjob.gapi_jobmap.begin();
00307 for (; iter != gjob.gapi_jobmap.end(); ++iter) {
00308
00309 statusmap->Add(new TObjString(iter->first.c_str()), new TObjString(iter->second.c_str()));
00310 }
00311 TGridJobStatus *status = (TGridJobStatus*)new TAlienJobStatus(statusmap);
00312 joblist->Add((TGridJobStatus*)status);
00313 if (verbose) {
00314 }
00315 }
00316 delete gjobarray;
00317 return joblist;
00318 }
00319
00320
00321 Bool_t TAlien::KillById(TString jobid)
00322 {
00323
00324
00325 if (gapi_kill(atoi(jobid.Data())))
00326 return kFALSE;
00327 else
00328 return kTRUE;
00329 }
00330
00331
00332 Bool_t TAlien::ResubmitById(TString jobid)
00333 {
00334
00335
00336 if (gapi_resubmit(atoi(jobid.Data())))
00337 return kFALSE;
00338 else
00339 return kTRUE;
00340 }
00341
00342
00343 TGridResult *TAlien::Command(const char *command, bool interactive, UInt_t stream)
00344 {
00345
00346
00347 if (fGc) {
00348 if (fGc->Command(command)) {
00349
00350 TAlienResult* gresult = new TAlienResult();
00351
00352 for (Int_t column = 0 ; column < (fGc->GetStreamColumns(stream)); column++) {
00353 TMap *gmap = new TMap();
00354 gmap->SetOwner(kTRUE);
00355 for (Int_t row=0; row < fGc->GetStreamRows(stream,column); row++) {
00356 gmap->Add((TObject*)(new TObjString(fGc->GetStreamFieldKey(stream,column,row))),
00357 (TObject*)(new TObjString(fGc->GetStreamFieldValue(stream,column,row))));
00358 }
00359 gresult->Add(gmap);
00360 }
00361
00362 if (interactive) {
00363
00364 fGc->DebugDumpStreams();
00365 }
00366 return gresult;
00367 }
00368 }
00369 return 0;
00370 }
00371
00372
00373 TGridResult *TAlien::LocateSites()
00374 {
00375 return Command("locatesites");
00376 }
00377
00378
00379 void TAlien::Stdout()
00380 {
00381 if (fGc) {
00382 fGc->PrintCommandStdout();
00383 }
00384 }
00385
00386
00387 void TAlien::Stderr()
00388 {
00389 if (fGc) {
00390 fGc->PrintCommandStderr();
00391 }
00392 }
00393
00394
00395 TGridResult *TAlien::Query(const char *path, const char *pattern,
00396 const char *conditions, const char *options)
00397 {
00398 TString cmdline = TString("find -z ") + TString(options) + TString(" ") + TString(path) + TString(" ") + TString(pattern) + TString(" ") + TString(conditions);
00399 return Command(cmdline);
00400 }
00401
00402
00403 TGridResult *TAlien::OpenDataset(const char *lfn, const char *options)
00404 {
00405 TString cmdline = TString("getdataset") + TString(" ") + TString(options) + TString(" ") + TString(lfn);
00406 return Command(cmdline, kTRUE);
00407 }
00408
00409
00410 TMap *TAlien::GetColumn(UInt_t stream, UInt_t column)
00411 {
00412 TMap *gmap = new TMap();
00413 for (Int_t row = 0; row < fGc->GetStreamRows(stream,column); row++) {
00414 gmap->Add((TObject*)(new TObjString(fGc->GetStreamFieldKey(stream,column,row))),
00415 (TObject*)(new TObjString(fGc->GetStreamFieldValue(stream,column,row))));
00416 }
00417 return gmap;
00418 }
00419
00420
00421 const char *TAlien::GetStreamFieldValue(UInt_t stream, UInt_t column, UInt_t row)
00422 {
00423 return fGc->GetStreamFieldValue(stream,column,row);
00424 }
00425
00426
00427 const char *TAlien::GetStreamFieldKey(UInt_t stream, UInt_t column, UInt_t row)
00428 {
00429 return fGc->GetStreamFieldKey(stream,column,row);
00430 }
00431
00432
00433 UInt_t TAlien::GetNColumns(UInt_t stream)
00434 {
00435 return fGc->GetStreamColumns(stream);
00436 }
00437
00438
00439
00440
00441 TGridResult *TAlien::Ls(const char* ldn, Option_t* options, Bool_t verbose)
00442 {
00443 TString cmdline = TString("ls") + TString(" ") + TString(options) + TString(" ") + TString(ldn);
00444
00445 return Command(cmdline, verbose);
00446 }
00447
00448
00449 Bool_t TAlien::Cd(const char* ldn, Bool_t verbose)
00450 {
00451 TString cmdline = TString("cd") + TString(" ") + TString(ldn);
00452
00453 Command(cmdline, kFALSE);
00454
00455 if (verbose) {
00456 Stdout();
00457 Stderr();
00458 }
00459
00460 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
00461 if (result) {
00462 if (strlen(result) > 0) {
00463 if (atoi(result) == 1) {
00464 return kTRUE;
00465 }
00466 }
00467 }
00468
00469 Error("Cd","Cannot change to directory %s\n",ldn);
00470 if (!verbose) Stdout();
00471 return kFALSE;
00472 }
00473
00474
00475 const char* TAlien::Pwd(Bool_t verbose)
00476 {
00477 TString cmdline = TString("pwd");
00478
00479 TGridResult* result = Command(cmdline, kFALSE, kENVIR);
00480
00481 if (verbose) {
00482 Stdout();
00483 Stderr();
00484 }
00485
00486 if (result) {
00487 TMap* resultmap = ((TMap*)result->At(0));
00488 if (resultmap) {
00489 TObjString* pwd = (TObjString*)resultmap->GetValue("pwd");
00490 if (pwd) {
00491 fPwd = pwd->GetName();
00492 delete resultmap;
00493 return fPwd;
00494 } else {
00495 delete resultmap;
00496 return 0;
00497 }
00498 }
00499 }
00500
00501 Error("pwd","Cannot get current working directory\n");
00502 if (!verbose) Stdout();
00503 return 0;
00504 }
00505
00506
00507 Int_t TAlien::Mkdir(const char* ldn, Option_t* options, Bool_t verbose)
00508 {
00509 TString cmdline = TString("mkdir");
00510 if (strlen(options)) {
00511 cmdline += (TString(" ") + TString(options));
00512 }
00513
00514 cmdline += (TString(" -s ") + TString(ldn));
00515
00516 Command(cmdline, kFALSE);
00517
00518 if (verbose) {
00519 Stdout();
00520 Stderr();
00521 }
00522
00523 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
00524 if (result) {
00525 if (strlen(result) > 0) {
00526 if (atoi(result) > 0) {
00527 return atoi(result);
00528 }
00529 }
00530 }
00531
00532 Error("Mkdir","Cannot create directory %s\n",ldn);
00533 if (!verbose) Stdout();
00534 return 0;
00535 }
00536
00537
00538 Bool_t TAlien::Rmdir(const char* ldn, Option_t* options, Bool_t verbose)
00539 {
00540 TString cmdline = TString("rmdir");
00541 if (strlen(options)) {
00542 cmdline += (TString(" ") + TString(options));
00543 } else {
00544 cmdline += (TString(" -s ") + TString(ldn));
00545 }
00546
00547 Command(cmdline, kFALSE);
00548
00549 if (verbose) {
00550 Stdout();
00551 Stderr();
00552 }
00553
00554 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
00555 if (result) {
00556 if (strlen(result) > 0) {
00557 if (atoi(result) == 1) {
00558 return kTRUE;
00559 }
00560 }
00561 }
00562
00563 Error("Rmdir","Cannot remove directory %s\n",ldn);
00564 return kTRUE;
00565 }
00566
00567
00568 Bool_t TAlien::Register(const char* lfn, const char* turl, Long_t size, const char* se, const char* guid, Bool_t verbose)
00569 {
00570 TString cmdline = TString("register") + TString(" ") + TString(lfn) + TString(" ") + TString(turl);
00571 if (se) {
00572 cmdline += TString(" ");
00573 cmdline += size;
00574 cmdline += TString(" ");
00575 cmdline += TString(se);
00576 if (guid) {
00577 cmdline += TString(" ");
00578 cmdline += TString(guid);
00579 }
00580 }
00581
00582 Command(cmdline, kFALSE);
00583
00584 if (verbose) {
00585 Stdout();
00586 Stderr();
00587 }
00588
00589 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
00590 if (result) {
00591 if (strlen(result) > 0) {
00592 if (atoi(result) == 1) {
00593 return kTRUE;
00594 }
00595 }
00596 }
00597
00598 return kFALSE;
00599 }
00600
00601
00602 Bool_t TAlien::Rm(const char* lfn, Option_t* options, Bool_t verbose)
00603 {
00604 TString cmdline = TString("rm") + TString(" -s ") + TString(options) + TString(" ") + TString(lfn);
00605
00606 Command(cmdline, kFALSE);
00607
00608 if (verbose) {
00609 Stdout();
00610 Stderr();
00611 }
00612
00613 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
00614 if (result) {
00615 if (strlen(result) > 0) {
00616 if (atoi(result) == 1) {
00617 return kTRUE;
00618 }
00619 }
00620 }
00621
00622 Error("Rm","Cannot remove %s\n",lfn);
00623 return kFALSE;
00624 }
00625
00626
00627 TGridCollection *TAlien::OpenCollection(const char *collectionfile,
00628 UInt_t maxentries)
00629 {
00630
00631
00632 TString path(collectionfile);
00633 if (path.BeginsWith("alien://", TString::kIgnoreCase)) {
00634 TAlien* alien = dynamic_cast<TAlien*> (gGrid);
00635 if (!alien) {
00636 Error("OpenCollection",
00637 "Trying to read a collection, but gGrid not initialized with AliEn");
00638 return 0;
00639 }
00640 TString lfn = path(strlen("alien://"), path.Length());
00641 if (alien->Type(lfn) == kCollection) {
00642
00643 TGridResult* gridResult = alien->GetCollection(lfn);
00644 if (!gridResult) {
00645 Error("OpenCollection",
00646 "Could not retrieve collection %d from the catalog", collectionfile);
00647 return 0;
00648 }
00649
00650 return TAlienCollection::OpenAlienCollection(gridResult);
00651 }
00652 }
00653
00654 return TAlienCollection::Open(collectionfile, maxentries);
00655 }
00656
00657
00658 TGridCollection *TAlien::OpenCollectionQuery(TGridResult *queryresult,
00659 Bool_t nogrouping)
00660 {
00661
00662
00663 return (TGridCollection*)TAlienCollection::OpenQuery(queryresult, nogrouping);
00664 }
00665
00666
00667 TAlien::CatalogType TAlien::Type(const char* lfn, Option_t* option, Bool_t verbose)
00668 {
00669
00670
00671 TString cmdline = TString("type -z") + TString(" ") + TString(option) + TString(" ") + TString(lfn);
00672
00673 TGridResult* gridResult = Command(cmdline, kFALSE);
00674
00675 if (verbose) {
00676 Stdout();
00677 Stderr();
00678 }
00679
00680 if (!gridResult) {
00681 Error("Type", "Did not receive TGridResult from query %s", cmdline.Data());
00682 return kFailed;
00683 }
00684
00685 const char* typeStr = gridResult->GetKey(0, "type");
00686 if (!typeStr || strlen(typeStr) == 0) {
00687 Error("Type", "Could not get type of %s", lfn);
00688 delete gridResult;
00689 return kFailed;
00690 }
00691
00692 TAlien::CatalogType type = kFailed;
00693
00694 if (strcmp(typeStr, "file") == 0) {
00695 type = kFile;
00696 } else if (strcmp(typeStr, "directory") == 0) {
00697 type = kDirectory;
00698 } else if (strcmp(typeStr, "collection") == 0) {
00699 type = kCollection;
00700 } else
00701 Error("Type", "Unknown type %s", typeStr);
00702
00703 delete gridResult;
00704
00705 return type;
00706 }
00707
00708
00709 TGridResult* TAlien::GetCollection(const char* lfn, Option_t* option, Bool_t verbose)
00710 {
00711
00712
00713 TString cmdline = TString("listFilesFromCollection -z -v") + TString(" ") + TString(option) + TString(" ") + TString(lfn);
00714
00715 TGridResult* gridResult = Command(cmdline, kFALSE);
00716
00717 if (verbose) {
00718 Stdout();
00719 Stderr();
00720 }
00721
00722 return gridResult;
00723 }
00724
00725
00726 TGridResult* TAlien::ListPackages(const char* alienpackagedir)
00727 {
00728
00729
00730 if (!alienpackagedir) {
00731 alienpackagedir = "/alice/packages";
00732 }
00733
00734 TGridResult* gr = (TGridResult*) new TAlienResult();
00735
00736 TGridResult* result = Ls(alienpackagedir);
00737 if (result) {
00738 Int_t i=0;
00739 while ( result->GetFileName(i)) {
00740 TString pname=result->GetFileName(i);
00741 TGridResult* version = Ls(Form("%s/%s",alienpackagedir,pname.Data()));
00742 if (version) {
00743 Int_t j=0;
00744 while (version->GetFileName(j)) {
00745 TString pversion=version->GetFileName(j);
00746 if ((pversion.Contains("post_"))) {
00747 } else {
00748 TGridResult* platform = Ls(Form("%s/%s/%s",alienpackagedir,pname.Data(),pversion.Data()));
00749 if (platform) {
00750 Int_t k=0;
00751 TString allplatform="";
00752 while (platform->GetFileName(k)) {
00753 TString pplatform=platform->GetFileName(k);
00754 allplatform+=pplatform;
00755 allplatform+=" ";
00756 TMap* grmap = new TMap();
00757 grmap->Add((TObject*) new TObjString("name"),
00758 (TObject*) new TObjString(pplatform.Data()));
00759 grmap->Add((TObject*) new TObjString("path"),
00760 new TObjString( Form ( "%s/%s/%s/%s" , alienpackagedir,pname.Data(),pversion.Data(),pplatform.Data())));
00761 gr->Add(grmap);
00762 k++;
00763 }
00764 Info("ListPackages","Package: %-16s Version: %-20s Platform: [ %s ]",pname.Data(),pversion.Data(),allplatform.Data());
00765 delete platform;
00766 }
00767 }
00768 j++;
00769 }
00770 delete version;
00771 }
00772 i++;
00773 }
00774 delete result;
00775 }
00776 return gr;
00777 }