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

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

added 5.X setup, must be made better, want to generate a of stuff from the debian installation package, see #47

File size: 13.0 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
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 *attrib {
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  printf("basje \n");
420  ptr = (struct attrl *) malloc(number * sizeof(struct attrl));
421
422  prev = NULL;
423  current = ptr + (number - 1);
424  for (i=0; i < number; i++)
425  {
426    printf("constructor called\n");
427    current->name     = (char *) malloc(MAXNAMLEN * sizeof(char));
428    current->resource = (char *) malloc(MAXNAMLEN * sizeof(char));
429    current->value    = (char *) malloc(MAXNAMLEN * sizeof(char));
430
431    bzero( (void*) current->name, sizeof(current->name));
432    bzero( (void*) current->resource, sizeof(current->resource));
433    bzero( (void*) current->value, sizeof(current->value));
434
435    current->next     = prev;
436    prev = current;
437    current--;
438  }
439  return (struct attrl *)ptr;
440
441} // end new_attrl()
442
443
444
445// The default constructor for struct attropl
446//
447struct attropl *new_attropl(int number)
448{
449  struct attropl *ptr;
450  struct attropl *prev, *current;
451  int i;
452
453  /*
454    allocate memory as a one block is handy for Python scripts
455    and fill in the next fields so it also works for the C-library
456  */
457  ptr = (struct attropl *) malloc(number * sizeof(struct attropl));
458
459  prev = NULL;
460  current = ptr + (number - 1);
461  for (i=0; i < number; i++)
462  {
463    printf("constructor called\n");
464    current->name     = (char *) malloc(MAXNAMLEN * sizeof(char));
465    current->resource = (char *) malloc(MAXNAMLEN * sizeof(char));
466    current->value    = (char *) malloc(MAXNAMLEN * sizeof(char));
467
468    bzero( (void*) current->name, sizeof(current->name));
469    bzero( (void*) current->resource, sizeof(current->resource));
470    bzero( (void*) current->value, sizeof(current->value));
471    // current->op = 0;
472
473    current->next     = prev;
474    prev = current;
475    current--;
476  }
477  return (struct attropl *)ptr;
478
479} // end new_attropl()
480
481/* Not used only returned */
482struct batch_status *new_batch_status()
483{
484   struct batch_status *ptr;
485
486   ptr = (struct batch_status *) malloc(sizeof(struct batch_status));
487   return (struct batch_status *)ptr;
488} // end new struct batch_status
489
490int get_error()
491{
492   char *errmsg;
493
494   errmsg = pbse_to_txt(pbs_errno);
495   if (SARA_DEBUG)
496   {
497      printf("Bas = %d\n", pbs_errno);
498      printf("Bas = %d, text = %s\n", pbs_errno, errmsg);
499   }
500   return (pbs_errno);
501}
502
503
504%}  // end %inline functions
505
506%include <carrays.i>
507%array_class(struct attrl, attrlArray);
508
509
510// *****************************************************************
511// Here the Python shadow class is generated. We have added some
512// stuff here to extend the python classes.
513//
514%nodefault;
515// %include "pbs_python.h"
516%include "pbs_ifl.h"
517%include "rm.h"
518%include "log.h"
519
520
521// Iets van uhhh..... obsolete
522// %ignore pbs_errno;
523//%include <pbs_error.h>
524
525
526/*
527%feature("shadow") attrl::__str__ {
528  def __str__(self): print self.name + self.value;
529}
530%extend attrl {
531  void __str__();
532}
533*/
534
535/* not used
536%extend batch_status {
537  ~batch_status() {
538        if (SARA_DEBUG)
539      printf("Bas free batch_status\n");
540        free(self);
541  }
542}
543*/
544
545%extend attrl {
546  char *__str__() {
547    static char temp[4 * MAXNAMLEN] ;
548    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
549      self->name, self->resource, self->value);
550   
551    return &temp[0];
552  }
553
554/*
555 * For swig 1.3.29-2.1 we must create a destructor, but
556 * do not free anything pbs_statfree must do it
557*/
558  ~attrl() {
559    if (SARA_DEBUG)
560       printf("Bas free attrl\n");
561
562    /*
563        free(self);
564    */
565  }
566}
567
568%extend attropl {
569  char *__str__() {
570    static char temp[4 * MAXNAMLEN] ;
571    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
572      self->name, self->resource, self->value);
573   
574    return &temp[0];
575  }
576
577/*
578 * For swig 1.3.29-2.1 we must create a destructor, but
579 * do not free anything pbs_statfree must do it
580*/
581  ~attropl() {
582    if (SARA_DEBUG)
583       printf("Bas free attropl\n");
584
585    /*
586        free(self);
587    */
588  }
589}
590
591
592%shadow "resmom.py"
593%shadow "version.py"
594%shadow "errors.py"
Note: See TracBrowser for help on using the repository browser.