source: devel/C++/pbs.i

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

removed statement basje from development version, see #46

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