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

Last change on this file since 329 was 327, checked in by bas, 8 years ago

some small improvements but no working rm interface

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