source: devel/5.X/pbs.i @ 335

Last change on this file since 335 was 335, checked in by bas, 9 years ago

finally version of pbs_python to 5.X, still no working rm interface

File size: 12.6 KB
RevLine 
[324]1// PBS python interface
2// Author: Bas van der Vlies <bas.vandervlies@surfsara.nl>
3// Date  : 04 Sep 2001
4// Vers. : 1.0
5// Desc. : This is a simple python wrapper for PBS.
6//
7// SVN info
8//  $Id: pbs.i 522 2010-03-26 08:45:32Z bas $
9//
10%module pbs
11%include "typemaps.i"
12
13// %include pointer.i
14// %include PBS.i
15
16// header declarations
17//
18%{
19
20#include "pbs_ifl.h"
21#include "pbs_error.h"
22#include "rm.h"
23#include "log.h"
24
[335]25#define SARA_DEBUG 0
[324]26
27%}
28
29
30#if defined(SWIGPYTHON)
31
32// *****************************************************************
33// Some IN typemaps from Python ---> C
34//
35/*
36 * Convert Python batch status list to a valid C-linked list
37*/
[326]38%typemap(in) struct batch_status *stat {
[324]39  PyObject              *py_obj;
40  struct batch_status   *ptr, *prev;
41  char                  s[255];
42  int                   i=0, size=0;
43
[335]44  if (SARA_DEBUG) printf("Convert python -> c (struct batch_status *):\n");
[324]45
46  size = Get_List_Size($input);
47  if (SARA_DEBUG) printf("\tSize of batch_status list: %d\n", size);
48 
49  if ( size == -1 ) {
50    PyErr_SetString(PyExc_TypeError, "not a list");
51    return NULL;
52  }
53  // printf("Size = %d\n", size);
54
55  if (SARA_DEBUG) printf("\t<Contents>\n");
56
57  $1 = prev = NULL;
58  for ( i=0; i < size; i++ ) {
59    py_obj = PyList_GetItem($input, i);
60    if (SWIG_ConvertPtr(py_obj, (void **) &ptr, SWIGTYPE_p_batch_status, 1)) {
61
62       sprintf(s,"list item %d has wrong type", i);
63       PyErr_SetString(PyExc_TypeError, s);
64       return NULL;
65
66       // This will skipp the wrong entry
67       // continue;
68    }
69
70    /*
71     * Make first entry head of C linked list
72    */
73    if ( i == 0) {
74      $1 = ptr;
75      ptr->next = prev;
76    }
77    else {
78      prev->next = ptr;
79      ptr->next = NULL;
80    }
81
82    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
83    prev = ptr;
84
85  } // end for
86
87  if (SARA_DEBUG) printf("\t</Contents>\n");
88} // end struct batch_status *IN typemap
89
90
91/*
92 * Convert Python attrl list to a valid C-linked list
93*/
94%typemap(in) struct attrl *attrib {
95  PyObject      *py_obj;
96  struct attrl  *ptr, *prev;
97  char          s[255];
98  int           i=0, size=0;
99
100  // printf("Python --> C\n");
101
[335]102  if (SARA_DEBUG) printf("Convert python -> c (struct attrl *):\n");
[324]103
104  size = Get_List_Size($input);
105  if (SARA_DEBUG) printf("\tSize of attrl List: %d\n", size);
106 
107  if ( size == -1 ) {
108      PyErr_SetString(PyExc_TypeError, "not a list");
109      return NULL;
110  }
111
112  if (SARA_DEBUG) printf("\t<Contents>\n");
113
114  $1 = prev = NULL;
115  for ( i=0; i < size; i++ ) {
116    py_obj = PyList_GetItem($input, i);
117    if (SWIG_ConvertPtr(py_obj, (void **) &ptr, SWIGTYPE_p_attrl, 1)) {
118
119      sprintf(s,"list item %d has wrong type", i);
120      PyErr_SetString(PyExc_TypeError, s);
121      return NULL;
122
[335]123      // This will skip the wrong entry
[324]124      // continue;
125    }
126
127    /*
128     * Make first entry head of C linked list
129    */
130    if ( i == 0) {
131      $1 = ptr;
132      ptr->next = prev;
133    }
134    else {
135      prev->next = ptr;
136      ptr->next = NULL;
137    }
138    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
139   
140    prev = ptr;
141
142  } // end for
[335]143
[324]144  if (SARA_DEBUG) printf("\t</Contents>\n");
145} // end struct attrl *IN typemap
146
147/*
148 * Convert Python attropl list to a valid C-linked list
149*/
[326]150%typemap(in) struct attropl *attrib {
[324]151  PyObject          *py_obj;
152  struct attropl    *ptr, *prev;
153  char              s[255];
154  int               i=0, size=0;
155
156  // printf("Python --> C\n");
157
[335]158  if (SARA_DEBUG) printf("Convert python -> c (struct attropl *):\n");
[324]159
160  size = Get_List_Size($input);
161
162  if (SARA_DEBUG) printf("\tSize attropl List: %d\n", size);
163
164  if ( size == -1 ) {
165    PyErr_SetString(PyExc_TypeError, "not a list");
166    return NULL;
167  }
168  //printf("Size = %d\n", size);
169
170  if (SARA_DEBUG) printf("\t<Contents>\n");
171
172  $1 = prev = NULL;
173  for ( i=0; i < size; i++ ) {
174    py_obj = PyList_GetItem($input, i);
175    if (SWIG_ConvertPtr(py_obj, (void **) &ptr, SWIGTYPE_p_attropl, 1)) {
176
177       sprintf(s,"list item %d has wrong type", i);
178       PyErr_SetString(PyExc_TypeError, s);
179       return NULL;
180
181       // This will skipp the wrong entry
182       // continue;
183    }
184
185    /*
186     * Make first entry head of C linked list
187    */
188    if ( i == 0) {
189      $1 = ptr;
190      ptr->next = prev;
191    }
192    else {
193      prev->next = ptr;
194      ptr->next = NULL;
195    }
196    prev = ptr;
197
198  } // end for
199  if (SARA_DEBUG) printf("\t</Contents>\n");
200} // end struct attrl *IN typemap
201
202%typemap(in) char **CHARCAST {
203  int       i=0, size=0;
204  PyObject  *py_obj;
205
206  if (SARA_DEBUG) printf("Convert python -> c (char **):\n");
207
208  size = Get_List_Size($input);
209
210  if (SARA_DEBUG) printf("\tSize of List: %d\n", size);
211
212  if ( size == -1 ) {
213    PyErr_SetString(PyExc_TypeError, "not a list");
214    return NULL;
215  }
216  // printf("Size = %d\n", size);
217
218  if (SARA_DEBUG) printf("\t<Contents>\n");
219
220  $1 = (char **) malloc( (size+1) * sizeof(char *));
221  for (i=0; i < size; i++) {
222    py_obj = PyList_GetItem($input, i);
223    if (PyString_Check(py_obj)) {
224      $1[i] = PyString_AsString(py_obj);
225      if (SARA_DEBUG) printf("%s", $1[i]);
226    }
227    else {
228      PyErr_SetString(PyExc_TypeError, "not a list of strings");
229      free($1);
230      return NULL;
231    }
232  } // end for
233  $1[i] = 0;
234  if (SARA_DEBUG) printf("\t</Contents>\n");
235} // end typemap char **IN
236
237// *****************************************************************
238// Some OUT typemaps from C ---> Python
239//
240%typemap(out) struct batch_status * {
241
242  PyObject              *obj_batch;
[335]243  struct batch_status   *ptr, *org;
[324]244  int                   i=0, len=0;
245
[335]246  if (SARA_DEBUG) printf("Convert c (struct batch_status *) -> python:\n");
[324]247 
248  // Deterime length of list
249  //
[335]250  org = $1;
251  ptr = org;
[324]252  while (ptr != NULL) {
253    len++;
254    ptr = ptr->next;
255  }
256  $result = PyList_New(len);
257
258  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
259
260  // Make a list of batch_status pointers
261  //
262  if (SARA_DEBUG) printf("\t<Contents>\n");
263  ptr = $1;
264  for (i=0; i < len; i++) {
265    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_batch_status,0);
266    PyList_SetItem($result, i, obj_batch);
267    if (SARA_DEBUG)  {
268        printf("\t\t- %s\n", ptr->name);
269    }
270    ptr = ptr->next;
271  }
272
273  if (SARA_DEBUG) printf("\t</Contents>\n");
274} // end typemap struct batch_status *
275
276%typemap(out) struct attrl * {
277  PyObject      *obj_batch;
278  struct attrl  *ptr;
279  int           i=0, len=0;
280
[335]281  if (SARA_DEBUG) printf("Convert c (struct attrl *) -> python:\n");
[324]282
283  ptr = $1;
284  while (ptr != NULL) {
285    len++;
286    ptr = ptr->next;
287  }
288  $result = PyList_New(len);
289
290  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
291
292  if (SARA_DEBUG) printf("\t<Contents>\n");
293  ptr = $1;
294  for (i=0; i < len; i++) {
295    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_attrl,1);
296    PyList_SetItem($result, i, obj_batch);
297    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
298    ptr = ptr->next;
299  }
300  if (SARA_DEBUG) printf("\t</Contents>\n");
301} // end typemap struct attrl *
302
303%typemap(out) struct attropl * {
304  PyObject          *obj_batch;
305  struct attropl    *ptr;
306  int               i=0, len=0;
307
[335]308  if (SARA_DEBUG) printf("Convert c (struct attropl *) -> python:\n");
[324]309 
310  ptr = $1;
311  while (ptr != NULL) {
312    len++;
313    ptr = ptr->next;
314  }
315  $result = PyList_New(len);
316  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
317
318  if (SARA_DEBUG) printf("\t<Contents>\n");
319  ptr = $1;
320  for (i=0; i < len; i++) {
321    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_attropl,1);
322    PyList_SetItem($result, i, obj_batch);
323    ptr = ptr->next;
324  }
325
326  if (SARA_DEBUG) printf("\t</Contents>\n");
327} // end typemap struct attropl *
328
329// Convert C (char **) to Python List
330//
331%typemap(out) char ** {
332   int len=0, i;
333
334   if (SARA_DEBUG)
[335]335     printf("Convert char ** -> python list\n");
[324]336
337
338   if ($1 == NULL)
339     $result = PyList_New(0);
340   else {
341     while ($1[len])
342       len++;
343   }
344
345   if (SARA_DEBUG)
346     printf("\tSize of List: %d\n", len);
347
348   $result = PyList_New(len);
349   if (SARA_DEBUG)
350     printf("\t<Contents>\n");
351   for (i=0; i < len; i++ ) {
352      PyList_SetItem($result, i , PyString_FromString($1[i]));
353      if (SARA_DEBUG)
354        printf("\t\t- %s\n", $1[i]);
355   }
356
357   if (SARA_DEBUG)
358     printf("\t</Contents>\n");
359} // end typemap char **pbs_selectjob
360
361// *****************************************************************
362// Some freearg typemaps
363//
364%typemap(freearg) char ** {
365  free( (char *) $1);
366}
367
368#endif
369
370// *****************************************************************
371// Some Functions used by all C-structs typemaps
372//
373
374%{
375int Get_List_Size(PyObject *src)
376{
377  if (PyList_Check(src))
378    return(PyList_Size(src));
379
380  /* check if valid NULL pointer */
381  if ( PyString_Check(src) ) {
382    if ( ! strcmp(PyString_AsString(src), "NULL") )
383      return(0);
384  }
385  return(-1);
386
387} // end Get_List_Size()
388
389%}
390
391
392// *****************************************************************
393// These C-functions are the constructurs for the different C-structs
394//
395/*
396 * Make some default constructors for the various structs
397*/
398%inline %{
399
400// The default constructor for struct attrl
401//
402struct attrl *new_attrl(int number)
403{
404  struct attrl *ptr;
405  struct attrl *prev, *current;
406  int i;
407
408  /*
409    allocate memory as a one block is handy for Python scripts
410    and fill in the next fields so it also works for the C-library
411  */
[327]412
413  if (SARA_DEBUG)
414  {
415     printf("new_attrl \n");
416  }
[324]417  ptr = (struct attrl *) malloc(number * sizeof(struct attrl));
418
419  prev = NULL;
420  current = ptr + (number - 1);
421  for (i=0; i < number; i++)
422  {
[326]423    // printf("constructor called\n");
[324]424    current->name     = (char *) malloc(MAXNAMLEN * sizeof(char));
425    current->resource = (char *) malloc(MAXNAMLEN * sizeof(char));
426    current->value    = (char *) malloc(MAXNAMLEN * sizeof(char));
427
428    bzero( (void*) current->name, sizeof(current->name));
429    bzero( (void*) current->resource, sizeof(current->resource));
430    bzero( (void*) current->value, sizeof(current->value));
431
432    current->next     = prev;
433    prev = current;
434    current--;
435  }
436  return (struct attrl *)ptr;
437
438} // end new_attrl()
439
440
441
442// The default constructor for struct attropl
443//
444struct attropl *new_attropl(int number)
445{
446  struct attropl *ptr;
447  struct attropl *prev, *current;
448  int i;
449
450  /*
451    allocate memory as a one block is handy for Python scripts
452    and fill in the next fields so it also works for the C-library
453  */
454  ptr = (struct attropl *) malloc(number * sizeof(struct attropl));
455
456  prev = NULL;
457  current = ptr + (number - 1);
458  for (i=0; i < number; i++)
459  {
[326]460    // printf("constructor called\n");
[324]461    current->name     = (char *) malloc(MAXNAMLEN * sizeof(char));
462    current->resource = (char *) malloc(MAXNAMLEN * sizeof(char));
463    current->value    = (char *) malloc(MAXNAMLEN * sizeof(char));
464
465    bzero( (void*) current->name, sizeof(current->name));
466    bzero( (void*) current->resource, sizeof(current->resource));
467    bzero( (void*) current->value, sizeof(current->value));
468    // current->op = 0;
469
470    current->next     = prev;
471    prev = current;
472    current--;
473  }
474  return (struct attropl *)ptr;
475
476} // end new_attropl()
477
478/* Not used only returned */
479struct batch_status *new_batch_status()
480{
481   struct batch_status *ptr;
482
483   ptr = (struct batch_status *) malloc(sizeof(struct batch_status));
484   return (struct batch_status *)ptr;
485} // end new struct batch_status
486
487int get_error()
488{
489   if (SARA_DEBUG)
490   {
[331]491      printf("Bas pbs_errno = %d, pbs_strerror %s\n", pbs_errno, pbs_strerror(pbs_errno));
[324]492   }
493   return (pbs_errno);
494}
495
496
497%}  // end %inline functions
498
499%include <carrays.i>
500%array_class(struct attrl, attrlArray);
501
502
503// *****************************************************************
504// Here the Python shadow class is generated. We have added some
505// stuff here to extend the python classes.
506//
507%nodefault;
[335]508
509
[324]510%include "pbs_ifl.h"
511%include "rm.h"
512%include "log.h"
[331]513%include "pbs_error.h"
[324]514
515/* not used
516%extend batch_status {
517  ~batch_status() {
518        if (SARA_DEBUG)
519      printf("Bas free batch_status\n");
520        free(self);
521  }
522}
523*/
524
525%extend attrl {
526  char *__str__() {
527    static char temp[4 * MAXNAMLEN] ;
528    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
529      self->name, self->resource, self->value);
530   
531    return &temp[0];
532  }
533
534/*
535 * For swig 1.3.29-2.1 we must create a destructor, but
536 * do not free anything pbs_statfree must do it
537*/
538  ~attrl() {
539    if (SARA_DEBUG)
540       printf("Bas free attrl\n");
541
[335]542    // free(self);
[324]543  }
544}
545
546%extend attropl {
547  char *__str__() {
548    static char temp[4 * MAXNAMLEN] ;
549    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
550      self->name, self->resource, self->value);
551   
552    return &temp[0];
553  }
554
555/*
556 * For swig 1.3.29-2.1 we must create a destructor, but
557 * do not free anything pbs_statfree must do it
558*/
559  ~attropl() {
560    if (SARA_DEBUG)
561       printf("Bas free attropl\n");
562
[335]563    // free(self);
[324]564  }
565}
566
567
568%shadow "resmom.py"
569%shadow "version.py"
570%shadow "errors.py"
Note: See TracBrowser for help on using the repository browser.