From 8719b84a95805d109e21c20f05a0164315e1b38a Mon Sep 17 00:00:00 2001
From: Vasilii Chernov <vchernov@inr.ru>
Date: Wed, 2 Mar 2016 10:26:13 +0100
Subject: Move scripts handing code from py.c to Python wrap

---
 pywrap/CMakeLists.txt    |   4 +-
 pywrap/pcilib.py         |  38 ++++++
 pywrap/pcipywrap.c       | 327 ++++++++++++++++++-----------------------------
 pywrap/pcipywrap.h       |  79 +-----------
 pywrap/pcipywrap.i       |   3 -
 pywrap/test_pcilib.py    | 265 ++++++++++++++++++++++++++++++++++++++
 pywrap/test_pcipywrap.py | 265 --------------------------------------
 7 files changed, 429 insertions(+), 552 deletions(-)
 create mode 100644 pywrap/pcilib.py
 create mode 100644 pywrap/test_pcilib.py
 delete mode 100644 pywrap/test_pcipywrap.py

(limited to 'pywrap')

diff --git a/pywrap/CMakeLists.txt b/pywrap/CMakeLists.txt
index 8f14e4f..4cc51da 100644
--- a/pywrap/CMakeLists.txt
+++ b/pywrap/CMakeLists.txt
@@ -19,7 +19,9 @@ swig_link_libraries(pcipywrap ${PYTHON_LIBRARIES} pcilib)
 
 install(TARGETS ${SWIG_MODULE_pcipywrap_REAL_NAME} DESTINATION ${PYTHON_INSTALL_DIR})
 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pcipywrap.py DESTINATION ${PYTHON_INSTALL_DIR})
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pcilib.py DESTINATION ${PYTHON_INSTALL_DIR})
 
 if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
-    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_pcipywrap.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
+   file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pcilib.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
+   file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_pcilib.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
 endif(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
diff --git a/pywrap/pcilib.py b/pywrap/pcilib.py
new file mode 100644
index 0000000..7696524
--- /dev/null
+++ b/pywrap/pcilib.py
@@ -0,0 +1,38 @@
+from pcipywrap import *
+import os
+import sys
+
+class Pcilib(Pcipywrap):
+   def __init__(s, *args):
+      Pcipywrap.__init__(s, *args)
+      
+      #load scripts
+      scripts_dir = os.environ.get('PCILIB_SCRIPTS_DIR')
+      if scripts_dir:
+         scripts_dir_abs = os.path.abspath(scripts_dir)
+         if not scripts_dir_abs in sys.path:
+            sys.path.append(scripts_dir_abs)
+         
+         s.__scipts = dict()
+         for script in os.listdir(scripts_dir_abs):
+            if script.endswith('.py'):
+               script_module = os.path.splitext(script)[0]
+               __import__(script_module)
+               s.__scipts[script_module] = sys.modules[script_module]
+               
+               
+   def get_scripts_list(s):
+      scripts = []
+      for script in s.__scipts:
+         curr_script = dict()
+         curr_script['name'] = script
+         if 'description' in dir(s.__scipts[script]):
+            curr_script['description'] = s.__scipts[script].description
+         scripts.append(curr_script)
+      return scripts
+      
+      
+   def run_script(s, name, input_value):
+      if not name in s.__scipts:
+         raise Exception('Script ' + name +' has not loaded')
+      return s.__scipts[name].run(s, input_value)
diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c
index 9113d40..0f6729e 100644
--- a/pywrap/pcipywrap.c
+++ b/pywrap/pcipywrap.c
@@ -1,9 +1,6 @@
 #include "pcipywrap.h"
 #include "locking.h"
 
-#include <dirent.h>
-#include <strings.h>
-
 char* full_log = NULL;
 
 /*!
@@ -104,6 +101,27 @@ void redirect_logs_to_exeption()
                       pcilib_get_logger_context());
 }
 
+/*!
+ * \brief Wrap for PyDict_SetItem, with decrease reference counting after set.
+ */
+void pcilib_pydict_set_item(PyObject* dict, PyObject* name, PyObject* value)
+{
+    PyDict_SetItem(dict,
+                   name,
+                   value);
+    Py_XDECREF(name);
+    Py_XDECREF(value);
+}
+
+/*!
+ * \brief Wrap for PyList_Append, with decrease reference counting after append.
+ */
+void pcilib_pylist_append(PyObject* list, PyObject* value)
+{
+    PyList_Append(list, value);
+    Py_XDECREF(value);
+}
+
 void add_pcilib_value_to_dict(pcilib_t* ctx, PyObject* dict, pcilib_value_t* val, const char *name)
 {
     PyObject *py_val = (PyObject*)pcilib_get_value_as_pyobject(ctx, val, NULL);
@@ -197,198 +215,126 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper
 
 PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_register_info_t listItem)
 {
-   PyObject* pylistItem = PyDict_New();
-
-   if(listItem.name)
-     pcilib_pydict_set_item(pylistItem,
-                            PyString_FromString("name"),
-                            PyString_FromString(listItem.name));
-
-   if(listItem.description)
-     pcilib_pydict_set_item(pylistItem,
-                            PyString_FromString("description"),
-                            PyString_FromString(listItem.description));
-
-   if(listItem.bank)
-     pcilib_pydict_set_item(pylistItem,
-                            PyString_FromString("bank"),
-                            PyString_FromString(listItem.bank));
-
-
-   //serialize modes
-   PyObject* modes = PyList_New(0);
-
-   if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R)
-     pcilib_pylist_append(modes, PyString_FromString("R"));
-   if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W)
-     pcilib_pylist_append(modes, PyString_FromString("W"));
-   if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW)
-     pcilib_pylist_append(modes, PyString_FromString("RW"));
-   if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C)
-     pcilib_pylist_append(modes, PyString_FromString("W1C"));
-   if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C)
-     pcilib_pylist_append(modes, PyString_FromString("RW1C"));
-   if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I)
-     pcilib_pylist_append(modes, PyString_FromString("W1I"));
-   if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I)
-     pcilib_pylist_append(modes, PyString_FromString("RW1I"));
-   if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT)
-     pcilib_pylist_append(modes, PyString_FromString("NO_CHK"));
-
-   pcilib_pydict_set_item(pylistItem,
-                        PyString_FromString("mode"),
-                        modes);
-
-   pcilib_value_t defval = {0};
-   pcilib_set_value_from_register_value(ctx, &defval, listItem.defvalue);
-   add_pcilib_value_to_dict(ctx, pylistItem, &defval, "defvalue");
-
-   if(listItem.range)
-   {
-     pcilib_value_t minval = {0};
-     pcilib_set_value_from_register_value(ctx, &minval, listItem.range->min);
-
-     pcilib_value_t maxval = {0};
-     pcilib_set_value_from_register_value(ctx, &maxval, listItem.range->max);
-
-     PyObject* range = PyDict_New();
-     add_pcilib_value_to_dict(ctx, range, &minval, "min");
-     add_pcilib_value_to_dict(ctx, range, &maxval, "max");
-     pcilib_pydict_set_item(pylistItem,
-                            PyString_FromString("range"),
-                            range);
-   }
+    PyObject* pylistItem = PyDict_New();
 
-   if(listItem.values)
-   {
+    if(listItem.name)
+        pcilib_pydict_set_item(pylistItem,
+                               PyString_FromString("name"),
+                               PyString_FromString(listItem.name));
 
-     PyObject* values = PyList_New(0);
+    if(listItem.description)
+        pcilib_pydict_set_item(pylistItem,
+                               PyString_FromString("description"),
+                               PyString_FromString(listItem.description));
 
-     for (int j = 0; listItem.values[j].name; j++)
-     {
-         PyObject* valuesItem = PyDict_New();
+    if(listItem.bank)
+        pcilib_pydict_set_item(pylistItem,
+                               PyString_FromString("bank"),
+                               PyString_FromString(listItem.bank));
 
-         pcilib_value_t val = {0};
-         pcilib_set_value_from_register_value(ctx, &val, listItem.values[j].value);
 
-         pcilib_value_t min = {0};
-         pcilib_set_value_from_register_value(ctx, &min, listItem.values[j].min);
+    //serialize modes
+    PyObject* modes = PyList_New(0);
 
-         pcilib_value_t max = {0};
-         pcilib_set_value_from_register_value(ctx, &max, listItem.values[j].max);
+    if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R)
+        pcilib_pylist_append(modes, PyString_FromString("R"));
+    if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W)
+        pcilib_pylist_append(modes, PyString_FromString("W"));
+    if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW)
+        pcilib_pylist_append(modes, PyString_FromString("RW"));
+    if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C)
+        pcilib_pylist_append(modes, PyString_FromString("W1C"));
+    if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C)
+        pcilib_pylist_append(modes, PyString_FromString("RW1C"));
+    if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I)
+        pcilib_pylist_append(modes, PyString_FromString("W1I"));
+    if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I)
+        pcilib_pylist_append(modes, PyString_FromString("RW1I"));
+    if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT)
+        pcilib_pylist_append(modes, PyString_FromString("NO_CHK"));
 
-         add_pcilib_value_to_dict(ctx, valuesItem, &val, "value");
-         add_pcilib_value_to_dict(ctx, valuesItem, &min, "min");
-         add_pcilib_value_to_dict(ctx, valuesItem, &max, "max");
+    pcilib_pydict_set_item(pylistItem,
+                           PyString_FromString("mode"),
+                           modes);
 
-         if(listItem.values[j].name)
-             pcilib_pydict_set_item(valuesItem,
-                                    PyString_FromString("name"),
-                                    PyString_FromString(listItem.values[j].name));
-         if(listItem.values[j].description)
-         {
-             pcilib_pydict_set_item(valuesItem,
-                                    PyString_FromString("description"),
-                                    PyString_FromString(listItem.values[j].description));
+    pcilib_value_t defval = {0};
+    pcilib_set_value_from_register_value(ctx, &defval, listItem.defvalue);
+    add_pcilib_value_to_dict(ctx, pylistItem, &defval, "defvalue");
 
-         }
-         pcilib_pylist_append(values, valuesItem);
-     }
+    if(listItem.range)
+    {
+        pcilib_value_t minval = {0};
+        pcilib_set_value_from_register_value(ctx, &minval, listItem.range->min);
 
-     pcilib_pydict_set_item(pylistItem,
-                            PyString_FromString("values"),
-                            values);
-   }
+        pcilib_value_t maxval = {0};
+        pcilib_set_value_from_register_value(ctx, &maxval, listItem.range->max);
 
-   return pylistItem;
-}
+        PyObject* range = PyDict_New();
+        add_pcilib_value_to_dict(ctx, range, &minval, "min");
+        add_pcilib_value_to_dict(ctx, range, &maxval, "max");
+        pcilib_pydict_set_item(pylistItem,
+                               PyString_FromString("range"),
+                               range);
+    }
+
+    if(listItem.values)
+    {
+
+        PyObject* values = PyList_New(0);
+
+        for (int j = 0; listItem.values[j].name; j++)
+        {
+            PyObject* valuesItem = PyDict_New();
+
+            pcilib_value_t val = {0};
+            pcilib_set_value_from_register_value(ctx, &val, listItem.values[j].value);
+
+            pcilib_value_t min = {0};
+            pcilib_set_value_from_register_value(ctx, &min, listItem.values[j].min);
+
+            pcilib_value_t max = {0};
+            pcilib_set_value_from_register_value(ctx, &max, listItem.values[j].max);
+
+            add_pcilib_value_to_dict(ctx, valuesItem, &val, "value");
+            add_pcilib_value_to_dict(ctx, valuesItem, &min, "min");
+            add_pcilib_value_to_dict(ctx, valuesItem, &max, "max");
+
+            if(listItem.values[j].name)
+                pcilib_pydict_set_item(valuesItem,
+                                       PyString_FromString("name"),
+                                       PyString_FromString(listItem.values[j].name));
+            if(listItem.values[j].description)
+            {
+                pcilib_pydict_set_item(valuesItem,
+                                       PyString_FromString("description"),
+                                       PyString_FromString(listItem.values[j].description));
+
+            }
+            pcilib_pylist_append(values, valuesItem);
+        }
+
+        pcilib_pydict_set_item(pylistItem,
+                               PyString_FromString("values"),
+                               values);
+    }
+
+    return pylistItem;
 
+}
 
 Pcipywrap *new_Pcipywrap(const char* fpga_device, const char* model)
 {
     //opening device
     pcilib_t* ctx = pcilib_open(fpga_device, model);
-    if(!ctx) {
+    if(!ctx)
+    {
         set_python_exception("Failed pcilib_open(%s, %s)", fpga_device, model);
         return NULL;
     }
-    
     Pcipywrap *self;
     self = (Pcipywrap *) malloc(sizeof(Pcipywrap));
-    if(!self) {
-        pcilib_close(ctx);
-        return (Pcipywrap *)PyExc_MemoryError;
-    }  
     self->shared = 0;
     self->ctx = ctx;
-    self->py = NULL;
-    self->names = NULL;
-    self->names_size = 0;
-
-    
-    //processing pcilib scrips
-    const char *scripts_dir = getenv("PCILIB_SCRIPTS_DIR");
-    if(scripts_dir) {
-      int err = 0;
-
-      self->py = pcilib_init_py_ctx(ctx->py, &err);
-      if(err) {
-         delete_Pcipywrap(self);
-         set_python_exception("Failed pcilib_py_s_clone (%i)", err);
-         return NULL;
-      }
-      
-      //add scripts directory to Python path
-      err = pcilib_py_ctx_add_script_dir(self->py, scripts_dir);
-      if(err) {
-         delete_Pcipywrap(self);
-         set_python_exception("Failed pcilib_py_add_dir (%i)", err);
-         return NULL;
-      }
-      
-      //load scripts in PCILIB_SCRIPTS_DIR
-      self->names = malloc(++(self->names_size) * sizeof(char*));
-      self->names[self->names_size - 1] = NULL;
-      
-      DIR *dir;
-      struct dirent *script_path;
-      dir = opendir(scripts_dir);
-      if (dir) {
-         while ((script_path = readdir(dir)) != NULL) {
-
-            char *py = strrchr(script_path->d_name, '.');
-            if ((!py)||(strcasecmp(py, ".py"))) {
-               continue;
-            }
-
-            char *name = malloc(strlen(script_path->d_name));
-            if(!name) {
-               delete_Pcipywrap(self);
-               return (Pcipywrap *)PyExc_MemoryError;
-            }
-            
-            strcpy(name, script_path->d_name);
-
-            err = pcilib_py_ctx_load_script(self->py, name);
-
-            if(err) {
-               delete_Pcipywrap(self);
-               set_python_exception("pcilib_py_ctx_load_script (%i)", err);
-               return NULL;
-            }
-            
-            self->names = realloc(self->names, ++(self->names_size));
-            if(!self->names) {
-               delete_Pcipywrap(self);
-               return (Pcipywrap *)PyExc_MemoryError;
-            }
-            self->names[self->names_size - 1] = NULL;
-            self->names[self->names_size - 2] = name;
-         }
-         closedir(dir);
-      }
-    }
-    
     return self;
 }
 
@@ -404,27 +350,14 @@ Pcipywrap *create_Pcipywrap(PyObject* ctx)
     self = (Pcipywrap *) malloc(sizeof(Pcipywrap));
     self->shared = 1;
     self->ctx = PyCObject_AsVoidPtr(ctx);
-    self->py = NULL;
-    self->names = NULL;
-    self->names_size = 0;
     return self;
 }
 
 void delete_Pcipywrap(Pcipywrap *self) {
-   if(!self->shared)
-     pcilib_close(self->ctx);
-     
-   pcilib_free_py_ctx(self->py);
-
-   if(self->names) {
-      for(int i = 0; self->names[i]; i++)
-         free(self->names[i]);
-      free(self->names);
-      self->names = NULL;
-      self->names_size = 0;
-   }
-    
-   free(self);
+    if(!self->shared)
+        pcilib_close(self->ctx);
+
+    free(self);
 }
 
 PyObject* Pcipywrap_read_register(Pcipywrap *self, const char *regname, const char *bank)
@@ -531,7 +464,7 @@ PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank)
       set_python_exception("pcilib_get_register_list return NULL");
       return NULL;
    }
-      
+
    PyObject* pyList = PyList_New(0);
    for(int i = 0; list[i].name; i++)
    {
@@ -676,20 +609,4 @@ PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id)
     return PyInt_FromLong((long)1);
 }
 
-PyObject* Pcipywrap_get_scripts_list(Pcipywrap *self)
-{
-   return pcilib_py_ctx_get_scripts_info(self->py);
-}
 
-PyObject* Pcipywrap_run_script(Pcipywrap *self, const char* script_name, PyObject* value)
-{
-   int err = 0;
-   PyObject* value_out = pcilib_py_ctx_eval_func(self->py, script_name, "run", value, &err);
-   
-   if(err) {
-    set_python_exception("Failed pcilib_py_ctx_eval_func (%i)", err);
-     return NULL;
-   }
-   
-   return value_out;
-}
diff --git a/pywrap/pcipywrap.h b/pywrap/pcipywrap.h
index fa7e4f4..0258f04 100644
--- a/pywrap/pcipywrap.h
+++ b/pywrap/pcipywrap.h
@@ -5,23 +5,11 @@
 #include "error.h"
 #include <Python.h>
 
-#include "config.h"
-#include "py.h"
-
-#include "pci.h"
-#include "pcilib.h"
-
-
 typedef struct {
-    char** names;
-    int names_size;
-   
     void* ctx;
-    struct pcilib_py_s *py;
     int shared;
 } Pcipywrap;
 
-
 /*!
  * \brief Redirect pcilib standart log stream to exeption text.
  * Logger will accumulate errors untill get message, starts with "#E".
@@ -73,55 +61,18 @@ PyObject* Pcipywrap_get_property(Pcipywrap *self, const char *prop);
  * \return 1, serialized to PyObject or NULL with exeption text, if failed.
  */
 PyObject* Pcipywrap_set_property(Pcipywrap *self, PyObject* val, const char *prop);
-
-
-/*!
- * \brief Wrap for pcilib_get_register_list function.
- * \param bank [in]	bank - if set, only register within the specified bank will be returned
- * \return list of registers, serialized to Python object
- */
 PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank);
-
-/*!
- * \brief Returns the information about the specified register. Wrap for pcilib_get_register_info
- * \param[in] reg the name of the register
- * \param[in] bank indicates the bank where to look for register, autodetected if NULL is passed
- * \return information about the specified register, serialized to Python object
- */
 PyObject* Pcipywrap_get_register_info(Pcipywrap *self, const char* reg,const char *bank);
-
-/*!
- * \brief Returns the list of properties available under the specified path. Wrap for pcilib_get_property_list
- * \param[in] branch path or NULL to return the top-level properties
- * \return the list of the properties, serialized to Python object
- */
 PyObject* Pcipywrap_get_property_list(Pcipywrap *self, const char* branch);
 
-/*!
- * \brief Reads data from DMA until timeout is hit, a full DMA packet is read, or the specified number of bytes are read.
- * Wrap for pcilib_read_dma.
- * \param dma ID of DMA engine
- * \param size specifies how many bytes should be read
- * \return DMA data, serialierd to Python bytearray
- * \warning This function has not been tested.
- * \todo Test this fucntion
- */
 PyObject* Pcipywrap_read_dma(Pcipywrap *self, unsigned char dma, size_t size);
 
-/*!
- * \brief Wrap for pcilib_lock_global
- * \return 1, serialized to PyObject or NULL with exeption text, if failed.
- */
 PyObject* Pcipywrap_lock_global(Pcipywrap *self);
-
-/*!
- * \brief Wrap for pcilib_unlock_global
- */
 void Pcipywrap_unlock_global(Pcipywrap *self);
 
 /*!
  * \brief Wrap for pcilib_lock
- * \param[in] lock_id lock identificator
+ * \param lock_id lock identificator
  * \warning This function should be called only under Python standart threading lock.
  * Otherwise it will stuck with more than 1 threads. See /xml/test/test_prop_mt.py
  * for example.
@@ -129,35 +80,7 @@ void Pcipywrap_unlock_global(Pcipywrap *self);
  */
 PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id);
 
-/*!
- * \brief This function will try to take a lock for the mutex pointed by
- * lockfunction to acquire a lock, but that returns immediatly if the lock can't be
- * acquired on first try. Wrap for pcilib_try_lock.
- * \param[in] lock_id lock id
- * \return 1, serialized to PyObject or NULL with exeption text, if failed.
- */
 PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id);
-
-/*!
- * \brief This function unlocks the lock with specified id. Wrap for pcilib_unlock.
- * \param[in] lock_id lock id
- * \return 1, serialized to PyObject or NULL with exeption text, if failed.
- */
 PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id);
 
-/*!
- * \brief Returns list with information about aviable scripts
- * \return list with information about scripts, aviable in model
- */
-PyObject* Pcipywrap_get_scripts_list(Pcipywrap *self);
-
-/*!
- * \brief Runs script with specified name
- * \param script_name script name (with extension); name could be found by
- * Pcipywrap_get_scripts_list fucntion
- * \param[in] value input value
- * \return value returned by script
- */
-PyObject* Pcipywrap_run_script(Pcipywrap *self, const char* script_name, PyObject* value);
-
 #endif /* PCIPYWRAP_H */
diff --git a/pywrap/pcipywrap.i b/pywrap/pcipywrap.i
index 697820d..7749a67 100644
--- a/pywrap/pcipywrap.i
+++ b/pywrap/pcipywrap.i
@@ -29,8 +29,5 @@ typedef struct {
 		PyObject* lock(const char *lock_id);
 		PyObject* try_lock(const char *lock_id);
 		PyObject* unlock(const char *lock_id);
-      
-      PyObject* get_scripts_list();
-      PyObject* run_script(const char* script_name, PyObject* value);
 	}
 } Pcipywrap;
diff --git a/pywrap/test_pcilib.py b/pywrap/test_pcilib.py
new file mode 100644
index 0000000..69540ec
--- /dev/null
+++ b/pywrap/test_pcilib.py
@@ -0,0 +1,265 @@
+import threading
+import pcilib
+import random
+import os
+import json
+import requests
+import time
+from optparse import OptionParser, OptionGroup
+
+class test_pcilib():
+   def __init__(self, 
+                device, 
+                model, 
+                num_threads = 150,
+                write_percentage = 0.1, 
+                register = 'reg1',
+                prop = '/test/prop1',
+                branch = '/test',
+                server_host = 'http://localhost', 
+                server_port = 12412,
+                server_message_delay = 0):
+	  #initialize enviroment variables
+      if not 'APP_PATH' in os.environ:
+         APP_PATH = ''
+         file_dir = os.path.dirname(os.path.abspath(__file__))
+         APP_PATH = str(os.path.abspath(file_dir + '/../..'))
+         os.environ["APP_PATH"] = APP_PATH
+       
+      if not 'PCILIB_MODEL_DIR' in os.environ:   
+         os.environ['PCILIB_MODEL_DIR'] = os.environ["APP_PATH"] + "/xml"
+      if not 'LD_LIBRARY_PATH' in os.environ: 
+         os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib"
+   
+      random.seed()
+      #create pcilib_instance
+      self.device = device
+      self.model = model
+      self.pcilib = pcilib.Pcilib(device, model)
+      self.num_threads = num_threads
+      self.write_percentage = write_percentage
+      self.register = register
+      self.prop = prop
+      self.branch = branch
+      self.server_message_delay = server_message_delay
+      self.server_port = server_port
+      self.server_host = server_host
+
+   def testLocking(self, message, lock_id = None):
+      url = str(self.server_host + ':' + str(self.server_port))
+      headers = {'content-type': 'application/json'}
+      payload =[{'command': 'lock', 'lock_id': lock_id},
+                {'command': 'try_lock', 'lock_id': lock_id},
+                {'command': 'unlock', 'lock_id': lock_id},
+                {'command': 'lock_global'},
+                {'command': 'unlock_global'},
+                {'command': 'help'}]  
+      r = requests.get(url, data=json.dumps(payload[message]), headers=headers)
+      print json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))
+    
+   def testThreadSafeReadWrite(self):
+      def threadFunc():
+         if random.randint(0, 100) >= (self.write_percentage * 100):
+            ret = self.pcilib.get_property(self.prop)
+            print self.register, ':', ret
+            del ret
+         else:
+            val = random.randint(0, 65536)
+            print 'set value:', val
+            self.pcilib.set_property(val, self.prop)
+      try:
+         while(1):
+            thread_list = [threading.Thread(target=threadFunc) for i in range(0, self.num_threads)]
+            for i in range(0, self.num_threads):
+               thread_list[i].start()
+            for i in range(0, self.num_threads):
+               thread_list[i].join()
+            print 'cycle done'
+      except KeyboardInterrupt:
+         print 'testing done'
+         pass
+   
+   def testMemoryLeak(self):
+      try:
+         while(1):
+            val = random.randint(0, 8096)
+            self.pcilib = pcilib.Pcilib(self.device, self.model)
+            print self.pcilib.get_property_list(self.branch)
+            print self.pcilib.get_register_info(self.register)
+            print self.pcilib.get_registers_list();
+            print self.pcilib.write_register(val, self.register)
+            print self.pcilib.read_register(self.register)
+            print self.pcilib.set_property(val, self.prop)
+            print self.pcilib.get_property(self.prop)
+      except KeyboardInterrupt:
+         print 'testing done'
+         pass
+
+   def testServer(self):
+      url = str(self.server_host + ':' + str(self.server_port))
+      headers = {'content-type': 'application/json'}
+      payload =[{'com': 'open', 'data2' : '12341'},
+      #{'command': 'open', 'device' : '/dev/fpga0', 'model': 'test_pywrap'},
+      {'command': 'help'},
+      {'command': 'get_registers_list'},
+      {'command': 'get_register_info', 'reg': self.register},
+      {'command': 'get_property_list', 'branch': self.branch},
+      {'command': 'read_register', 'reg': self.register},
+      {'command': 'write_register', 'reg': self.register},
+      {'command': 'get_property', 'prop': self.prop},
+      {'command': 'set_property', 'prop': self.prop}]
+      
+      def sendRandomMessage():
+         message_number = random.randint(1, len(payload) - 1)
+         print 'message number: ', message_number
+         payload[message_number]['value'] =  random.randint(0, 8096)
+         r = requests.get(url, data=json.dumps(payload[message_number]), headers=headers)
+         print json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': '))
+      
+      try:    
+         r = requests.get(url, data=json.dumps(payload[1]), headers=headers)
+         print json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))
+   
+         while(1):
+            time.sleep(self.server_message_delay)
+            thread_list = [threading.Thread(target=sendRandomMessage) for i in range(0, self.num_threads)]
+            for i in range(0, self.num_threads):
+               thread_list[i].start()
+            for i in range(0, self.num_threads):
+               thread_list[i].join()
+            print 'cycle done'
+            
+      except KeyboardInterrupt:
+         print 'testing done'
+         pass
+
+if __name__ == '__main__':
+   #parce command line options
+   parser = OptionParser()
+   parser.add_option("-d", "--device",  action="store",
+                     type="string", dest="device", default=str('/dev/fpga0'),
+                     help="FPGA device (/dev/fpga0)")                     
+   parser.add_option("-m", "--model",  action="store",
+                     type="string", dest="model", default=None,
+                     help="Memory model (autodetected)")
+   parser.add_option("-t", "--threads",  action="store",
+                     type="int", dest="threads", default=150,
+                     help="Threads number (150)")
+                     
+   server_group = OptionGroup(parser, "Server Options",
+                              "Options for testing server.")
+   server_group.add_option("-p", "--port",  action="store",
+                           type="int", dest="port", default=9000,
+                           help="Set testing server port (9000)")
+   server_group.add_option("--host",  action="store",
+                           type="string", dest="host", default='http://localhost',
+                           help="Set testing server host (http://localhost)")
+   server_group.add_option("--delay",  action="store",
+                           type="float", dest="delay", default=0.0,
+                           help="Set delay in seconds between sending messages to setver (0.0)")
+   parser.add_option_group(server_group)
+   
+   rw_group = OptionGroup(parser, "Registers/properties Options",
+                         "Group stores testing register and property options")
+   rw_group.add_option("--write_percentage",  action="store",
+                       type="float", dest="write_percentage", default=0.5,
+                       help="Set percentage (0.0 - 1.0) of write commands in multithread (0.5)"
+                       "read/write test")
+   rw_group.add_option("-r", "--register", action="store",
+                       type="string", dest="register", default='reg1',
+                       help="Set register name (reg1)")
+   rw_group.add_option("--property", action="store",
+                       type="string", dest="prop", default='/test/prop1',
+                       help="Set property name (/test/prop1)")
+   rw_group.add_option("-b", "--branch", action="store",
+                       type="string", dest="branch", default='/test',
+                       help="Set property branch (/test)")
+                       
+   parser.add_option_group(rw_group)
+   
+   test_group = OptionGroup(parser, "Test commands group",
+                            "This group conatains aviable commands for testing. "
+                            "If user add more than one command, they will process"
+                            "sequientally. To stop test, press Ctrl-C."
+                            )
+   test_group.add_option("--test_mt_rw",  action="store_true",
+                       dest="test_mt_rw", default=False,
+                       help="Multithread read/write test. This test will execute "
+                       "get_property/set_property commands with random values in multi-thread mode")
+   test_group.add_option("--test_memory_leak",  action="store_true",
+                       dest="test_memory_leak", default=False,
+                       help="Python wrap memory leak test. This test will execute all"
+                       "Python wrap memory leak test. This test will execute all"
+                       )
+   test_group.add_option("--test_server",  action="store_true",
+                       dest="test_server", default=False,
+                       help="Python server test. This test will send "
+                       "random commands to server in multi-thread mode"
+                       )
+   parser.add_option_group(test_group)
+   
+   lock_group = OptionGroup(parser, "Locking test group")
+   
+   lock_group.add_option("--lock",  action="store",
+                       dest="lock_id", default=None, type="string",
+                       help="Lock id."
+                       )
+   lock_group.add_option("--try_lock",  action="store",
+                       dest="try_lock_id", default=None, type="string",
+                       help="Try to lock id."
+                       )
+   lock_group.add_option("--unlock",  action="store",
+                       dest="unlock_id", default=None, type="string",
+                       help="Unlock lock with id."
+                       )
+   lock_group.add_option("--lock_global",  action="store_true",
+                       dest="lock_global", default=False,
+                       help="Global pcilib lock."
+                       )
+   lock_group.add_option("--unlock_global",  action="store_true",
+                       dest="unlock_global", default=False,
+                       help="Global pcilib unlock."
+                       )
+                       
+   lock_group.add_option("--get_server_message",  action="store_true",
+                       dest="get_server_message", default=False,
+                       help="Global pcilib unlock."
+                       )
+                       
+
+   parser.add_option_group(lock_group)
+   
+   opts = parser.parse_args()[0]
+
+   #create pcilib test instance
+   lib = test_pcilib(opts.device,
+                        opts.model,
+                        num_threads = opts.threads,
+                        write_percentage = opts.write_percentage,
+                        register = opts.register,
+                        prop = opts.prop,
+                        branch = opts.branch,
+                        server_host = opts.host,
+                        server_port = opts.port,
+                        server_message_delay = opts.delay)
+   
+   #make some tests                
+   if opts.test_mt_rw:
+      lib.testThreadSafeReadWrite()
+   if opts.test_memory_leak:
+      lib.testMemoryLeak()
+   if opts.test_server:
+      lib.testServer()
+   if opts.lock_id:
+      lib.testLocking(0, opts.lock_id)  
+   if opts.try_lock_id:
+      lib.testLocking(1, opts.try_lock_id)  
+   if opts.unlock_id:
+      lib.testLocking(2, opts.unlock_id)  
+   if opts.lock_global:
+      lib.testLocking(3)  
+   if opts.unlock_global:
+      lib.testLocking(4)  
+   if(opts.get_server_message):
+      lib.testLocking(5)
+
diff --git a/pywrap/test_pcipywrap.py b/pywrap/test_pcipywrap.py
deleted file mode 100644
index 809a81a..0000000
--- a/pywrap/test_pcipywrap.py
+++ /dev/null
@@ -1,265 +0,0 @@
-import threading
-import pcipywrap
-import random
-import os
-import json
-import requests
-import time
-from optparse import OptionParser, OptionGroup
-
-class test_pcipywrap():
-   def __init__(self, 
-                device, 
-                model, 
-                num_threads = 150,
-                write_percentage = 0.1, 
-                register = 'reg1',
-                prop = '/test/prop1',
-                branch = '/test',
-                server_host = 'http://localhost', 
-                server_port = 12412,
-                server_message_delay = 0):
-	  #initialize enviroment variables
-      if not 'APP_PATH' in os.environ:
-         APP_PATH = ''
-         file_dir = os.path.dirname(os.path.abspath(__file__))
-         APP_PATH = str(os.path.abspath(file_dir + '/../..'))
-         os.environ["APP_PATH"] = APP_PATH
-       
-      if not 'PCILIB_MODEL_DIR' in os.environ:   
-         os.environ['PCILIB_MODEL_DIR'] = os.environ["APP_PATH"] + "/xml"
-      if not 'LD_LIBRARY_PATH' in os.environ: 
-         os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib"
-   
-      random.seed()
-      #create pcilib_instance
-      self.device = device
-      self.model = model
-      self.pcilib = pcipywrap.Pcipywrap(device, model)
-      self.num_threads = num_threads
-      self.write_percentage = write_percentage
-      self.register = register
-      self.prop = prop
-      self.branch = branch
-      self.server_message_delay = server_message_delay
-      self.server_port = server_port
-      self.server_host = server_host
-
-   def testLocking(self, message, lock_id = None):
-      url = str(self.server_host + ':' + str(self.server_port))
-      headers = {'content-type': 'application/json'}
-      payload =[{'command': 'lock', 'lock_id': lock_id},
-                {'command': 'try_lock', 'lock_id': lock_id},
-                {'command': 'unlock', 'lock_id': lock_id},
-                {'command': 'lock_global'},
-                {'command': 'unlock_global'},
-                {'command': 'help'}]  
-      r = requests.get(url, data=json.dumps(payload[message]), headers=headers)
-      print json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))
-    
-   def testThreadSafeReadWrite(self):
-      def threadFunc():
-         if random.randint(0, 100) >= (self.write_percentage * 100):
-            ret = self.pcilib.get_property(self.prop)
-            print self.register, ':', ret
-            del ret
-         else:
-            val = random.randint(0, 65536)
-            print 'set value:', val
-            self.pcilib.set_property(val, self.prop)
-      try:
-         while(1):
-            thread_list = [threading.Thread(target=threadFunc) for i in range(0, self.num_threads)]
-            for i in range(0, self.num_threads):
-               thread_list[i].start()
-            for i in range(0, self.num_threads):
-               thread_list[i].join()
-            print 'cycle done'
-      except KeyboardInterrupt:
-         print 'testing done'
-         pass
-   
-   def testMemoryLeak(self):
-      try:
-         while(1):
-            val = random.randint(0, 8096)
-            self.pcilib = pcipywrap.Pcipywrap(self.device, self.model)
-            print self.pcilib.get_property_list(self.branch)
-            print self.pcilib.get_register_info(self.register)
-            print self.pcilib.get_registers_list();
-            print self.pcilib.write_register(val, self.register)
-            print self.pcilib.read_register(self.register)
-            print self.pcilib.set_property(val, self.prop)
-            print self.pcilib.get_property(self.prop)
-      except KeyboardInterrupt:
-         print 'testing done'
-         pass
-
-   def testServer(self):
-      url = str(self.server_host + ':' + str(self.server_port))
-      headers = {'content-type': 'application/json'}
-      payload =[{'com': 'open', 'data2' : '12341'},
-      #{'command': 'open', 'device' : '/dev/fpga0', 'model': 'test_pywrap'},
-      {'command': 'help'},
-      {'command': 'get_registers_list'},
-      {'command': 'get_register_info', 'reg': self.register},
-      {'command': 'get_property_list', 'branch': self.branch},
-      {'command': 'read_register', 'reg': self.register},
-      {'command': 'write_register', 'reg': self.register},
-      {'command': 'get_property', 'prop': self.prop},
-      {'command': 'set_property', 'prop': self.prop}]
-      
-      def sendRandomMessage():
-         message_number = random.randint(1, len(payload) - 1)
-         print 'message number: ', message_number
-         payload[message_number]['value'] =  random.randint(0, 8096)
-         r = requests.get(url, data=json.dumps(payload[message_number]), headers=headers)
-         print json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': '))
-      
-      try:    
-         r = requests.get(url, data=json.dumps(payload[1]), headers=headers)
-         print json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))
-   
-         while(1):
-            time.sleep(self.server_message_delay)
-            thread_list = [threading.Thread(target=sendRandomMessage) for i in range(0, self.num_threads)]
-            for i in range(0, self.num_threads):
-               thread_list[i].start()
-            for i in range(0, self.num_threads):
-               thread_list[i].join()
-            print 'cycle done'
-            
-      except KeyboardInterrupt:
-         print 'testing done'
-         pass
-
-if __name__ == '__main__':
-   #parce command line options
-   parser = OptionParser()
-   parser.add_option("-d", "--device",  action="store",
-                     type="string", dest="device", default=str('/dev/fpga0'),
-                     help="FPGA device (/dev/fpga0)")                     
-   parser.add_option("-m", "--model",  action="store",
-                     type="string", dest="model", default=None,
-                     help="Memory model (autodetected)")
-   parser.add_option("-t", "--threads",  action="store",
-                     type="int", dest="threads", default=150,
-                     help="Threads number (150)")
-                     
-   server_group = OptionGroup(parser, "Server Options",
-                              "Options for testing server.")
-   server_group.add_option("-p", "--port",  action="store",
-                           type="int", dest="port", default=9000,
-                           help="Set testing server port (9000)")
-   server_group.add_option("--host",  action="store",
-                           type="string", dest="host", default='http://localhost',
-                           help="Set testing server host (http://localhost)")
-   server_group.add_option("--delay",  action="store",
-                           type="float", dest="delay", default=0.0,
-                           help="Set delay in seconds between sending messages to setver (0.0)")
-   parser.add_option_group(server_group)
-   
-   rw_group = OptionGroup(parser, "Registers/properties Options",
-                         "Group stores testing register and property options")
-   rw_group.add_option("--write_percentage",  action="store",
-                       type="float", dest="write_percentage", default=0.5,
-                       help="Set percentage (0.0 - 1.0) of write commands in multithread (0.5)"
-                       "read/write test")
-   rw_group.add_option("-r", "--register", action="store",
-                       type="string", dest="register", default='reg1',
-                       help="Set register name (reg1)")
-   rw_group.add_option("--property", action="store",
-                       type="string", dest="prop", default='/test/prop1',
-                       help="Set property name (/test/prop1)")
-   rw_group.add_option("-b", "--branch", action="store",
-                       type="string", dest="branch", default='/test',
-                       help="Set property branch (/test)")
-                       
-   parser.add_option_group(rw_group)
-   
-   test_group = OptionGroup(parser, "Test commands group",
-                            "This group conatains aviable commands for testing. "
-                            "If user add more than one command, they will process"
-                            "sequientally. To stop test, press Ctrl-C."
-                            )
-   test_group.add_option("--test_mt_rw",  action="store_true",
-                       dest="test_mt_rw", default=False,
-                       help="Multithread read/write test. This test will execute "
-                       "get_property/set_property commands with random values in multi-thread mode")
-   test_group.add_option("--test_memory_leak",  action="store_true",
-                       dest="test_memory_leak", default=False,
-                       help="Python wrap memory leak test. This test will execute all"
-                       "Python wrap memory leak test. This test will execute all"
-                       )
-   test_group.add_option("--test_server",  action="store_true",
-                       dest="test_server", default=False,
-                       help="Python server test. This test will send "
-                       "random commands to server in multi-thread mode"
-                       )
-   parser.add_option_group(test_group)
-   
-   lock_group = OptionGroup(parser, "Locking test group")
-   
-   lock_group.add_option("--lock",  action="store",
-                       dest="lock_id", default=None, type="string",
-                       help="Lock id."
-                       )
-   lock_group.add_option("--try_lock",  action="store",
-                       dest="try_lock_id", default=None, type="string",
-                       help="Try to lock id."
-                       )
-   lock_group.add_option("--unlock",  action="store",
-                       dest="unlock_id", default=None, type="string",
-                       help="Unlock lock with id."
-                       )
-   lock_group.add_option("--lock_global",  action="store_true",
-                       dest="lock_global", default=False,
-                       help="Global pcilib lock."
-                       )
-   lock_group.add_option("--unlock_global",  action="store_true",
-                       dest="unlock_global", default=False,
-                       help="Global pcilib unlock."
-                       )
-                       
-   lock_group.add_option("--get_server_message",  action="store_true",
-                       dest="get_server_message", default=False,
-                       help="Global pcilib unlock."
-                       )
-                       
-
-   parser.add_option_group(lock_group)
-   
-   opts = parser.parse_args()[0]
-
-   #create pcilib test instance
-   lib = test_pcipywrap(opts.device,
-                        opts.model,
-                        num_threads = opts.threads,
-                        write_percentage = opts.write_percentage,
-                        register = opts.register,
-                        prop = opts.prop,
-                        branch = opts.branch,
-                        server_host = opts.host,
-                        server_port = opts.port,
-                        server_message_delay = opts.delay)
-   
-   #make some tests                
-   if opts.test_mt_rw:
-      lib.testThreadSafeReadWrite()
-   if opts.test_memory_leak:
-      lib.testMemoryLeak()
-   if opts.test_server:
-      lib.testServer()
-   if opts.lock_id:
-      lib.testLocking(0, opts.lock_id)  
-   if opts.try_lock_id:
-      lib.testLocking(1, opts.try_lock_id)  
-   if opts.unlock_id:
-      lib.testLocking(2, opts.unlock_id)  
-   if opts.lock_global:
-      lib.testLocking(3)  
-   if opts.unlock_global:
-      lib.testLocking(4)  
-   if(opts.get_server_message):
-      lib.testLocking(5)
-
-- 
cgit v1.2.3