source: devel/C/pbs.i

Last change on this file was 320, checked in by bas, 10 years ago

added some free calss to free memorry

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  free(ptr);
275
276  if (SARA_DEBUG) printf("\t</Contents>\n");
277} // end typemap struct batch_status *
278
279%typemap(out) struct attrl * {
280  PyObject      *obj_batch;
281  struct attrl  *ptr;
282  int           i=0, len=0;
283
284  if (SARA_DEBUG) printf("Converteren c (struct attrl *) -> python:\n");
285
286  ptr = $1;
287  while (ptr != NULL) {
288    len++;
289    ptr = ptr->next;
290  }
291  $result = PyList_New(len);
292
293  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
294
295  if (SARA_DEBUG) printf("\t<Contents>\n");
296  ptr = $1;
297  for (i=0; i < len; i++) {
298    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_attrl,1);
299    PyList_SetItem($result, i, obj_batch);
300    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
301    ptr = ptr->next;
302  }
303
304  free(ptr);
305  if (SARA_DEBUG) printf("\t</Contents>\n");
306} // end typemap struct attrl *
307
308%typemap(out) struct attropl * {
309  PyObject          *obj_batch;
310  struct attropl    *ptr;
311  int               i=0, len=0;
312
313  if (SARA_DEBUG) printf("Converteren c (struct attropl *) -> python:\n");
314 
315  ptr = $1;
316  while (ptr != NULL) {
317    len++;
318    ptr = ptr->next;
319  }
320  $result = PyList_New(len);
321  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
322
323  if (SARA_DEBUG) printf("\t<Contents>\n");
324  ptr = $1;
325  for (i=0; i < len; i++) {
326    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_attropl,1);
327    PyList_SetItem($result, i, obj_batch);
328    ptr = ptr->next;
329  }
330
331  free(ptr);
332  if (SARA_DEBUG) printf("\t</Contents>\n");
333} // end typemap struct attropl *
334
335// Convert C (char **) to Python List
336//
337%typemap(out) char ** {
338   int len=0, i;
339
340   if (SARA_DEBUG)
341     printf("Converteren char ** -> python list\n");
342
343
344   if ($1 == NULL)
345     $result = PyList_New(0);
346   else {
347     while ($1[len])
348       len++;
349   }
350
351   if (SARA_DEBUG)
352     printf("\tSize of List: %d\n", len);
353
354   $result = PyList_New(len);
355   if (SARA_DEBUG)
356     printf("\t<Contents>\n");
357   for (i=0; i < len; i++ ) {
358      PyList_SetItem($result, i , PyString_FromString($1[i]));
359      if (SARA_DEBUG)
360        printf("\t\t- %s\n", $1[i]);
361   }
362   if (SARA_DEBUG)
363     printf("\t</Contents>\n");
364} // end typemap char **pbs_selectjob
365
366// *****************************************************************
367// Some freearg typemaps
368//
369%typemap(freearg) char ** {
370  free( (char *) $1);
371}
372
373#endif
374
375// *****************************************************************
376// Some Functions used by all C-structs typemaps
377//
378
379%{
380int Get_List_Size(PyObject *src)
381{
382  if (PyList_Check(src))
383    return(PyList_Size(src));
384
385  /* check if valid NULL pointer */
386  if ( PyString_Check(src) ) {
387    if ( ! strcmp(PyString_AsString(src), "NULL") )
388      return(0);
389  }
390  return(-1);
391
392} // end Get_List_Size()
393
394%}
395
396
397// *****************************************************************
398// These C-functions are the constructurs for the different C-structs
399//
400/*
401 * Make some default constructors for the various structs
402*/
403%inline %{
404
405// The default constructor for struct attrl
406//
407struct attrl *new_attrl(int number)
408{
409  struct attrl *ptr;
410  struct attrl *prev, *current;
411  int i;
412
413  /*
414    allocate memory as a one block is handy for Python scripts
415    and fill in the next fields so it also works for the C-library
416  */
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  {
423    // printf("constructor called\n");
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  {
460    // printf("constructor called\n");
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   char *errmsg;
490
491   errmsg = pbse_to_txt(pbs_errno);
492   if (SARA_DEBUG)
493   {
494      printf("Bas = %d\n", pbs_errno);
495      printf("Bas = %d, text = %s\n", pbs_errno, errmsg);
496   }
497   return (pbs_errno);
498}
499
500
501%}  // end %inline functions
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;
508// %include "pbs_python.h"
509%include "pbs_ifl.h"
510%include "rm.h"
511%include "log.h"
512
513
514// Iets van uhhh..... obsolete
515// %ignore pbs_errno;
516//%include <pbs_error.h>
517
518
519/*
520%feature("shadow") attrl::__str__ {
521  def __str__(self): print self.name + self.value;
522}
523%extend attrl {
524  void __str__();
525}
526*/
527
528/* not used
529%extend batch_status {
530  ~batch_status() {
531        if (SARA_DEBUG)
532      printf("Bas free batch_status\n");
533        free(self);
534  }
535}
536*/
537
538%extend attrl {
539  char *__str__() {
540    static char temp[4 * MAXNAMLEN] ;
541    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
542      self->name, self->resource, self->value);
543   
544    return &temp[0];
545  }
546
547/*
548 * For swig 1.3.29-2.1 we must create a destructor, but
549 * do not free anything pbs_statfree must do it
550*/
551  ~attrl() {
552    if (SARA_DEBUG)
553       printf("Bas free attrl\n");
554
555    /*
556        free(self);
557    */
558  }
559}
560
561%extend attropl {
562  char *__str__() {
563    static char temp[4 * MAXNAMLEN] ;
564    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
565      self->name, self->resource, self->value);
566   
567    return &temp[0];
568  }
569
570/*
571 * For swig 1.3.29-2.1 we must create a destructor, but
572 * do not free anything pbs_statfree must do it
573*/
574  ~attropl() {
575    if (SARA_DEBUG)
576       printf("Bas free attropl\n");
577
578    /*
579        free(self);
580    */
581  }
582}
583
584
585%shadow "resmom.py"
586%shadow "version.py"
587%shadow "errors.py"
Note: See TracBrowser for help on using the repository browser.