source: devel/C/pbs.i @ 319

Last change on this file since 319 was 309, checked in by bas, 10 years ago

Checkin the pbs swig development files

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