80 #include "gapi_job_operations.h"
90 TAlien::TAlien(
const char *gridurl,
const char *uid,
const char * passwd,
93 gSystem->Unsetenv(
"GCLIENT_EXTRA_ARG");
96 TUrl *gurl =
new TUrl(gridurl);
100 if (!strlen(gurl->GetHost())) {
101 if (gSystem->Getenv(
"alien_API_HOST"))
102 fHost = gSystem->Getenv(
"alien_API_HOST");
106 fHost = gurl->GetHost();
109 if (gurl->GetPort()<=0) {
110 if (gSystem->Getenv(
"alien_API_PORT"))
111 fPort = atoi(gSystem->Getenv(
"alien_API_PORT"));
115 fPort = gurl->GetPort();
119 if (gSystem->Getenv(
"alien_API_USER")) {
120 fUser = gSystem->Getenv(
"alien_API_USER");
122 if (gSystem->Getenv(
"LOGNAME")) {
124 fUser = gSystem->Getenv(
"LOGNAME");
127 fUser = gSystem->Getenv(
"USER");
137 Info(
"TAlien",
"%s => %s port: %d user: %s",gridurl,fHost.Data(),fPort,fUser.Data());
142 if (options && (options[0] ==
't')) {
143 fGc = GapiUI::MakeGapiUI(kTRUE);
146 fGc = GapiUI::MakeGapiUI(kFALSE);
150 Error(
"TAlien",
"could not connect to a alien service at:");
151 Error(
"TAlien",
"host: %s port: %d user: %s", fHost.Data(), fPort, fUser.Data());
160 if (!Command(
"motd")) {
161 Error(
"TAlien",
"we have no valid connection ... I try to connect ...");
162 fGc->Connect(fHost, fPort, fUser, passwd);
165 fGc->Connect(fHost, fPort, fUser, passwd);
173 sprintf(cwd,
"%s",cwdp);
177 fHome = TString(cwdp);
182 if (!fGc->Connected()) {
183 Error(
"TAlien",
"could not authenticate at:");
184 Error(
"TAlien",
"host: %s port: %d user: %s",fHost.Data(),fPort,fUser.Data());
203 Info(
"~TAlien",
"destructor called");
217 TString TAlien::Escape(
const char *input)
222 TString output(input);
223 output.ReplaceAll(
"\"",
"\\\"");
231 TGridJob *TAlien::Submit(
const char *jdl)
236 TString command(
"submit =< ");
240 cout << command << endl;
242 TGridResult* result = Command(command,kFALSE,kOUTPUT);
243 TAlienResult* alienResult =
dynamic_cast<TAlienResult*
>(result);
244 TList* list =
dynamic_cast<TList*
>(alienResult);
251 alienResult->DumpResult();
255 TIterator* iter = list->MakeIterator();
257 while ((
object = iter->Next()) != 0) {
258 TMap* map =
dynamic_cast<TMap*
>(object);
260 TObject* jobIDObject = map->GetValue(
"jobId");
261 TObjString* jobIDStr =
dynamic_cast<TObjString*
>(jobIDObject);
263 jobID = jobIDStr->GetString();
270 Error(
"Submit",
"error submitting job");
274 Info(
"Submit",
"your job was submitted with the ID = %s", jobID.Data());
276 return dynamic_cast<TGridJob*
>(
new TAlienJob(jobID));
282 TGridJDL *TAlien::GetJDLGenerator()
284 return new TAlienJDL();
290 TGridJobStatusList *TAlien::Ps(
const char* , Bool_t verbose)
292 GAPI_JOBARRAY* gjobarray = gapi_queryjobs(
"-", gGrid->GetUser(),
"-",
"-",
"-",
297 if (gjobarray->size() == 0) {
302 TGridJobStatusList* joblist =
new TAlienJobStatusList();
304 for (UInt_t i = 0; i < gjobarray->size(); i++ ) {
305 TMap* statusmap =
new TMap();
306 GAPI_JOB gjob = gjobarray->at(i);
307 std::map<std::string, std::string>::const_iterator iter = gjob.gapi_jobmap.begin();
308 for (; iter != gjob.gapi_jobmap.end(); ++iter) {
310 statusmap->Add(
new TObjString(iter->first.c_str()),
new TObjString(iter->second.c_str()));
312 TGridJobStatus *status = (TGridJobStatus*)
new TAlienJobStatus(statusmap);
313 joblist->Add((TGridJobStatus*)status);
324 Bool_t TAlien::KillById(TString jobid)
326 if (gapi_kill(atoi(jobid.Data())))
335 Bool_t TAlien::ResubmitById(TString jobid)
337 if (gapi_resubmit(atoi(jobid.Data())))
346 TGridResult *TAlien::Command(
const char *command,
bool interactive, UInt_t stream)
349 if (fGc->Command(command)) {
351 TAlienResult* gresult =
new TAlienResult();
353 for (Int_t column = 0 ; column < (fGc->GetStreamColumns(stream)); column++) {
354 TMap *gmap =
new TMap();
355 gmap->SetOwner(kTRUE);
356 for (Int_t row=0; row < fGc->GetStreamRows(stream,column); row++) {
357 gmap->Add((TObject*)(
new TObjString(fGc->GetStreamFieldKey(stream,column,row))),
358 (TObject*)(
new TObjString(fGc->GetStreamFieldValue(stream,column,row))));
365 fGc->DebugDumpStreams();
375 TGridResult *TAlien::LocateSites()
377 return Command(
"locatesites");
382 void TAlien::Stdout()
385 fGc->PrintCommandStdout();
391 void TAlien::Stderr()
394 fGc->PrintCommandStderr();
400 TGridResult *TAlien::Query(
const char *path,
const char *pattern,
401 const char *conditions,
const char *options)
403 TString cmdline = TString(
"find -z ") + TString(options) + TString(
" ") + TString(path) + TString(
" ") + TString(pattern) + TString(
" ") + TString(conditions);
404 return Command(cmdline);
409 TGridResult *TAlien::OpenDataset(
const char *lfn,
const char *options)
411 TString cmdline = TString(
"getdataset") + TString(
" ") + TString(options) + TString(
" ") + TString(lfn);
412 return Command(cmdline, kTRUE);
417 TMap *TAlien::GetColumn(UInt_t stream, UInt_t column)
419 TMap *gmap =
new TMap();
420 for (Int_t row = 0; row < fGc->GetStreamRows(stream,column); row++) {
421 gmap->Add((TObject*)(
new TObjString(fGc->GetStreamFieldKey(stream,column,row))),
422 (TObject*)(
new TObjString(fGc->GetStreamFieldValue(stream,column,row))));
429 const char *TAlien::GetStreamFieldValue(UInt_t stream, UInt_t column, UInt_t row)
431 return fGc->GetStreamFieldValue(stream,column,row);
436 const char *TAlien::GetStreamFieldKey(UInt_t stream, UInt_t column, UInt_t row)
438 return fGc->GetStreamFieldKey(stream,column,row);
443 UInt_t TAlien::GetNColumns(UInt_t stream)
445 return fGc->GetStreamColumns(stream);
452 TGridResult *TAlien::Ls(
const char* ldn, Option_t* options, Bool_t verbose)
454 TString cmdline = TString(
"ls") + TString(
" ") + TString(options) + TString(
" ") + TString(ldn);
456 return Command(cmdline, verbose);
461 Bool_t TAlien::Cd(
const char* ldn, Bool_t verbose)
463 TString cmdline = TString(
"cd") + TString(
" ") + TString(ldn);
465 Command(cmdline, kFALSE);
472 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
474 if (strlen(result) > 0) {
475 if (atoi(result) == 1) {
481 Error(
"Cd",
"Cannot change to directory %s\n",ldn);
482 if (!verbose) Stdout();
488 const char* TAlien::Pwd(Bool_t verbose)
490 TString cmdline = TString(
"pwd");
492 TGridResult* result = Command(cmdline, kFALSE, kENVIR);
500 TMap* resultmap = ((TMap*)result->At(0));
502 TObjString* pwd = (TObjString*)resultmap->GetValue(
"pwd");
504 fPwd = pwd->GetName();
514 Error(
"pwd",
"Cannot get current working directory\n");
515 if (!verbose) Stdout();
521 Int_t TAlien::Mkdir(
const char* ldn, Option_t* options, Bool_t verbose)
523 TString cmdline = TString(
"mkdir");
524 if (strlen(options)) {
525 cmdline += (TString(
" ") + TString(options));
528 cmdline += (TString(
" -s ") + TString(ldn));
530 Command(cmdline, kFALSE);
537 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
539 if (strlen(result) > 0) {
540 if (atoi(result) > 0) {
546 Error(
"Mkdir",
"Cannot create directory %s\n",ldn);
547 if (!verbose) Stdout();
553 Bool_t TAlien::Rmdir(
const char* ldn, Option_t* options, Bool_t verbose)
555 TString cmdline = TString(
"rmdir");
556 if (strlen(options)) {
557 cmdline += (TString(
" ") + TString(options));
559 cmdline += (TString(
" -s ") + TString(ldn));
562 Command(cmdline, kFALSE);
569 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
571 if (strlen(result) > 0) {
572 if (atoi(result) == 1) {
578 Error(
"Rmdir",
"Cannot remove directory %s\n",ldn);
584 Bool_t TAlien::Register(
const char* lfn,
const char* turl, Long_t size,
const char* se,
const char* guid, Bool_t verbose)
586 TString cmdline = TString(
"register") + TString(
" ") + TString(lfn) + TString(
" ") + TString(turl);
588 cmdline += TString(
" ");
590 cmdline += TString(
" ");
591 cmdline += TString(se);
593 cmdline += TString(
" ");
594 cmdline += TString(guid);
598 Command(cmdline, kFALSE);
605 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
607 if (strlen(result) > 0) {
608 if (atoi(result) == 1) {
619 Bool_t TAlien::Rm(
const char* lfn, Option_t* options, Bool_t verbose)
621 TString cmdline = TString(
"rm") + TString(
" -s ") + TString(options) + TString(
" ") + TString(lfn);
623 Command(cmdline, kFALSE);
630 const char* result = (GetStreamFieldValue(kOUTPUT,0,0));
632 if (strlen(result) > 0) {
633 if (atoi(result) == 1) {
639 Error(
"Rm",
"Cannot remove %s\n",lfn);
646 TGridCollection *TAlien::OpenCollection(
const char *collectionfile,
649 TString path(collectionfile);
650 if (path.BeginsWith(
"alien://", TString::kIgnoreCase)) {
651 TAlien* alien =
dynamic_cast<TAlien*
> (gGrid);
653 Error(
"OpenCollection",
654 "Trying to read a collection, but gGrid not initialized with AliEn");
657 TString lfn = path(strlen(
"alien://"), path.Length());
658 if (alien->Type(lfn) == kCollection) {
660 TGridResult* gridResult = alien->GetCollection(lfn);
662 Error(
"OpenCollection",
663 "Could not retrieve collection %s from the catalog", collectionfile);
667 return TAlienCollection::OpenAlienCollection(gridResult);
671 return TAlienCollection::Open(collectionfile, maxentries);
677 TGridCollection *TAlien::OpenCollectionQuery(TGridResult *queryresult,
680 return (TGridCollection*)TAlienCollection::OpenQuery(queryresult, nogrouping);
686 TAlien::CatalogType TAlien::Type(
const char* lfn, Option_t* option, Bool_t verbose)
688 TString cmdline = TString(
"type -z") + TString(
" ") + TString(option) + TString(
" ") + TString(lfn);
690 TGridResult* gridResult = Command(cmdline, kFALSE);
698 Error(
"Type",
"Did not receive TGridResult from query %s", cmdline.Data());
702 const char* typeStr = gridResult->GetKey(0,
"type");
703 if (!typeStr || !typeStr[0]) {
704 Error(
"Type",
"Could not get type of %s", lfn);
709 TAlien::CatalogType type = kFailed;
711 if (strcmp(typeStr,
"file") == 0) {
713 }
else if (strcmp(typeStr,
"directory") == 0) {
715 }
else if (strcmp(typeStr,
"collection") == 0) {
718 Error(
"Type",
"Unknown type %s", typeStr);
728 TGridResult* TAlien::GetCollection(
const char* lfn, Option_t* option, Bool_t verbose)
730 TString cmdline = TString(
"listFilesFromCollection -z -v") + TString(
" ") + TString(option) + TString(
" ") + TString(lfn);
732 TGridResult* gridResult = Command(cmdline, kFALSE);
745 TGridResult* TAlien::ListPackages(
const char* alienpackagedir)
747 if (!alienpackagedir) {
748 alienpackagedir =
"/alice/packages";
751 TGridResult* gr = (TGridResult*)
new TAlienResult();
753 TGridResult* result = Ls(alienpackagedir);
756 while ( result->GetFileName(i)) {
757 TString pname=result->GetFileName(i);
758 TGridResult* version = Ls(Form(
"%s/%s",alienpackagedir,pname.Data()));
761 while (version->GetFileName(j)) {
762 TString pversion=version->GetFileName(j);
763 if ((pversion.Contains(
"post_"))) {
765 TGridResult* platform = Ls(Form(
"%s/%s/%s",alienpackagedir,pname.Data(),pversion.Data()));
768 TString allplatform=
"";
769 while (platform->GetFileName(k)) {
770 TString pplatform=platform->GetFileName(k);
771 allplatform+=pplatform;
773 TMap* grmap =
new TMap();
774 grmap->Add((TObject*)
new TObjString(
"name"),
775 (TObject*)
new TObjString(pplatform.Data()));
776 grmap->Add((TObject*)
new TObjString(
"path"),
777 new TObjString( Form (
"%s/%s/%s/%s" , alienpackagedir,pname.Data(),pversion.Data(),pplatform.Data())));
781 Info(
"ListPackages",
"Package: %-16s Version: %-20s Platform: [ %s ]",pname.Data(),pversion.Data(),allplatform.Data());