Salome HOME
BUG 0020024: a --gdb-session option to runSalome ...
[modules/kernel.git] / src / Batch / Batch_JobInfo_PBS.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21  * JobInfo_PBS.cxx : 
22  *
23  * Auteur : Ivan DUTKA-MALEN - EDF R&D
24  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
25  * Date   : Fri Nov 21 09:42:06 2003
26  * Projet : Salome 2
27  *
28  */
29
30 #include <cstdio>
31 #include <sstream>
32 //#include "MEDMEM_STRING.hxx"
33 #include "Batch_Parametre.hxx"
34 #include "Batch_Environnement.hxx"
35 #include "Batch_RunTimeException.hxx"
36 #include "Batch_JobInfo_PBS.hxx"
37 using namespace std;
38
39 namespace Batch {
40
41   // Constructeurs
42   JobInfo_PBS::JobInfo_PBS(struct batch_status * list, bool tobedeleted) : JobInfo()
43   {
44     // On ne considere que le premier element de la liste
45     // Si tout est OK, la liste ne devrait contenir qu'un element
46     // Sinon on leve une exception.
47     struct batch_status * p_job = list;
48     int i;
49     for(i=0; p_job; p_job = p_job->next) i++;
50     if (i == 0) throw RunTimeException("Liste vide (le job est absent de la file)");
51     if (i > 1) {
52       //MEDMEM::STRING sst;
53       ostringstream sst;
54       sst << "JobInfo_PBS::JobInfo_PBS(struct batch_status * list, bool tobedeleted) : la liste contient "
55           << i << " elements" << " (1 seul requis)" << endl;
56       throw RunTimeException(sst.str());
57     }
58     p_job = list;
59
60     // On remplit les membres _param et _env
61
62     if (p_job->name && strlen(p_job->name)) _param[ID]   = p_job->name;
63     if (p_job->text && strlen(p_job->text)) _param[TEXT] = p_job->text;
64
65     for(struct attrl * p_attr = p_job->attribs; p_attr; p_attr = p_attr->next) {
66
67       string name, res, value;
68       if (p_attr->name && strlen(p_attr->name)) name = p_attr->name;
69       if (p_attr->resource && strlen(p_attr->resource)) res = p_attr->resource;
70       if (p_attr->value && strlen(p_attr->value)) value = p_attr->value;
71
72       if (name == ATTR_N) {
73         _param[NAME] = value;
74       
75       } else if (name == ATTR_owner) {
76         _param[USER] = value;
77
78       } else if (name == ATTR_state) {
79         _param[STATE] = value;
80
81       } else if (name == ATTR_queue) {
82         _param[QUEUE] = value;
83
84       } else if (name == ATTR_A) {
85         _param[ACCOUNT] = value;
86
87       } else if (name == ATTR_M) {
88         _param[MAIL] = value;
89
90       } else if (name == ATTR_c) {
91         if (!strcmp(value.c_str(), CHECKPOINT_UNSPECIFIED)) _param[CHECKPOINT] = 1L;
92         else _param[CHECKPOINT] = 0L;
93
94       } else if (name == ATTR_h) {
95         if (!strcmp(value.c_str(), NO_HOLD)) _param[HOLD] = 0L;
96         else _param[HOLD] = 1L;
97
98       } else if (name == ATTR_ctime) {
99         _param[CREATIONTIME] = atol(value.c_str());
100
101       } else if (name == ATTR_etime) {
102         _param[ELIGIBLETIME] = atol(value.c_str());
103
104       } else if (name == ATTR_mtime) {
105         _param[MODIFICATIONTIME] = atol(value.c_str());
106
107       } else if (name == ATTR_qtime) {
108         _param[QUEUEDTIME] = atol(value.c_str());
109
110       } else if (name == ATTR_exechost) {
111         _param[EXECUTIONHOST] = value;
112
113       } else if (name == ATTR_session) {
114         _param[PID] = atol(value.c_str());
115
116       } else if (name == ATTR_euser) {
117         _param[EUSER] = value;
118
119       } else if (name == ATTR_egroup) {
120         _param[EGROUP] = value;
121
122       } else if (name == ATTR_l) {
123         if (res == "cput") {
124           _param[MAXCPUTIME] = HMStoLong(value);
125
126         } else if (res == "walltime") {
127           _param[MAXWALLTIME] = HMStoLong(value);
128
129         }
130
131       } else if (name == ATTR_used) {
132         if (res == "cput") {
133           _param[USEDCPUTIME] = HMStoLong(value);
134
135         } else if (res == "walltime") {
136           _param[USEDWALLTIME] = HMStoLong(value);
137
138         }
139
140       } else if (name == ATTR_v) {
141         int deb = 0;
142         int pos = 0;
143         bool ok = true;
144
145         while (ok) {
146           pos = value.find(",", deb);
147           string sub = value.substr(deb, pos-deb);
148           deb = pos + 1;
149           if (pos < 0) ok = false;
150
151           int eq = sub.find("=");
152           _env[sub.substr(0, eq)] = sub.substr(eq+1);
153         }
154
155       }
156     }
157
158
159     if (tobedeleted) pbs_statfree(list);
160   }
161
162   // Destructeur
163   JobInfo_PBS::~JobInfo_PBS()
164   {
165     // Nothing to do
166   }
167   
168   // Convertit une date HH:MM:SS en secondes
169   long JobInfo_PBS::HMStoLong(const string & s)
170   {
171     long hour, min, sec;
172
173     sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
174     return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
175   }
176
177   // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
178   string JobInfo_PBS::__str__() const
179   {
180     //MEDMEM::STRING sst; 
181     ostringstream sst;
182     sst << "<JobInfo_PBS (" << this << ") :" << endl;
183     sst << " ID = " <<_param[ID] << endl;
184
185     sst << "  + Parametre :" << endl;
186     Parametre::const_iterator itp;
187     for(itp=_param.begin(); itp!=_param.end(); itp++) {
188       if ( (*itp).first != ID ) {
189         sst << "    * " << (*itp).first << " = " << (*itp).second << endl;
190       }
191     }
192     return sst.str();
193   }
194
195
196 }