Initial commit

This commit is contained in:
Valery Febvre 2004-04-02 00:54:07 +00:00
parent d09a0de3b0
commit b6c73fd19c
10 changed files with 680 additions and 0 deletions

1
python/.cvsignore Normal file
View File

@ -0,0 +1 @@
build

52
python/lassomod.c Normal file
View File

@ -0,0 +1,52 @@
/* $Id$
*
* PyLasso - Python bindings for Lasso library
*
* Copyright (C) 2004 Entr'ouvert
* http://lasso.labs.libre-entreprise.org
*
* Author: Valery Febvre <vfebvre@easter-eggs.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "lassomod.h"
#include "py_lasso.h"
static PyMethodDef lasso_methods[] = {
/* py_lasso.h */
{"init", init, METH_VARARGS},
{"shutdown", shutdown, METH_VARARGS},
{"check_version_exact", check_version_exact, METH_VARARGS},
{"check_version", check_version, METH_VARARGS},
{"check_version_ext", check_version_ext, METH_VARARGS},
{NULL, NULL} /* End of Methods Sentinel */
};
PyObject *lasso_error;
void initlassomod(void) {
PyObject *m, *d;
m = Py_InitModule("lassomod", lasso_methods);
d = PyModule_GetDict(m);
lasso_error = PyErr_NewException("lassomod.error", NULL, NULL);
PyDict_SetItemString(d, "lassomod error", lasso_error);
Py_INCREF(lasso_error);
PyModule_AddObject(m, "lassomod error", lasso_error);
}

13
python/lassomod.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef __PYLASSO_LASSOMOD_H__
#define __PYLASSO_LASSOMOD_H__
#include <lasso/lasso.h>
#include "utils.h"
#include "wrap_objs.h"
#define HASH_TABLE_SIZE 10
extern PyObject *lasso_error;
#endif /* __PYLASSO_LASSOMOD_H__ */

59
python/py_lasso.c Normal file
View File

@ -0,0 +1,59 @@
/* $Id$
*
* PyLasso -- Python bindings for Lasso library
*
* Copyright (C) 2004 Entr'ouvert
* http://lasso.labs.libre-entreprise.org
*
* Author: Valery Febvre <vfebvre@easter-eggs.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "lassomod.h"
#include "py_lasso.h"
PyObject *init(PyObject *self, PyObject *args) {
return (wrap_int(lasso_init()));
}
PyObject *shutdown(PyObject *self, PyObject *args) {
return (wrap_int(lasso_shutdown()));
}
PyObject *check_version_exact(PyObject *self, PyObject *args) {
return (wrap_int(lasso_check_version_exact()));
}
PyObject *check_version(PyObject *self, PyObject *args) {
return (wrap_int(lasso_check_version()));
}
PyObject *check_version_ext(PyObject *self, PyObject *args) {
int major;
int minor;
int subminor;
lassoCheckVersionMode mode;
if (CheckArgs(args, "IIII:check_version_ext")) {
if(!PyArg_ParseTuple(args, (char *) "iiii:check_version_ext",
&major, &minor, &subminor, &mode))
return NULL;
}
else return NULL;
return (wrap_int(lasso_check_version_ext(major, minor, subminor, mode)));
}

5
python/py_lasso.h Normal file
View File

@ -0,0 +1,5 @@
PyObject *init(PyObject *self, PyObject *args);
PyObject *shutdown(PyObject *self, PyObject *args);
PyObject *check_version_exact(PyObject *self, PyObject *args);
PyObject *check_version(PyObject *self, PyObject *args);
PyObject *check_version_ext(PyObject *self, PyObject *args);

210
python/setup.py Executable file
View File

@ -0,0 +1,210 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# $Id$
#
# PyLasso - Python bindings for Lasso library
#
# Copyright (C) 2004 Entr'ouvert
# http://lasso.labs.libre-entreprise.org
#
# Author: Valéry Febvre <vfebvre@easter-eggs.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
__doc__ = """Python bindings for Lasso Library
PyLasso is a set of Python bindings for Lasso Library.
"""
classifiers = """\
Development Status :: 2 - Pre-Alpha
Intended Audience :: Developers
License :: OSI Approved :: GNU General Public License (GPL)
Operating System :: POSIX
Programming Language :: C
Programming Language :: Python
Topic :: Software Development :: Libraries :: Python Modules
"""
from distutils.core import setup, Extension
import sys, commands
# check python version
if not hasattr(sys, 'version_info') or sys.version_info < (2,2):
raise SystemExit, "PyLasso requires Python version 2.2 or above."
# sanity check for any arguments
if len(sys.argv) == 1:
msg = 'Choose an action :\n' \
' 1. Build\n' \
' 2. Install\n' \
' 3. Clean\n' \
' 4. Exit\n' \
'Your choice : '
reply = raw_input(msg)
choice = None
if reply:
choice = reply[0]
if choice == '1':
sys.argv.append('build')
elif choice == '2':
sys.argv.append('install')
elif choice == '3':
sys.argv.append('clean')
sys.argv.append('-a')
elif choice == '4':
sys.exit(0)
# the crypto engine name : openssl, gnutls or nss
xmlsec1_crypto = "openssl"
if 'build' in sys.argv:
msg = '\nChoose a crypto engine :\n' \
' 1. OpenSSL\n' \
' 2. GnuTLS\n' \
' 3. NSS\n' \
'Your choice : '
reply = raw_input(msg)
choice = None
if reply:
choice = reply[0]
if choice == '1':
xmlsec1_crypto = "openssl"
elif choice == '2':
xmlsec1_crypto = "gnutls"
elif choice == '3':
xmlsec1_crypto = "nss"
define_macros = []
include_dirs = []
library_dirs = []
libraries = []
def extract_cflags(cflags):
global define_macros, include_dirs
list = cflags.split(' ')
for flag in list:
if flag == '':
continue
flag = flag.replace("\\\"", "")
if flag[:2] == "-I":
if flag[2:] not in include_dirs:
include_dirs.append(flag[2:])
elif flag[:2] == "-D":
t = tuple(flag[2:].split('='))
if t not in define_macros:
define_macros.append(t)
else:
print "Warning : cflag %s skipped" % flag
def extract_libs(libs):
global library_dirs, libraries
list = libs.split(' ')
for flag in list:
if flag == '':
continue
if flag[:2] == "-l":
if flag[2:] not in libraries:
libraries.append(flag[2:])
elif flag[:2] == "-L":
if flag[2:] not in library_dirs:
library_dirs.append(flag[2:])
else:
print "Warning : linker flag %s skipped" % flag
# GObject
gobject_cflags = commands.getoutput('pkg-config gobject-2.0 --cflags')
if gobject_cflags[:2] not in ["-I", "-D"]:
print "Error : cannot get GObject pre-processor and compiler flags"
gobject_libs = commands.getoutput('pkg-config gobject-2.0 --libs')
if gobject_libs[:2] not in ["-l", "-L"]:
print "Error : cannot get GObject linker flags"
# LibXML2
libxml2_cflags = commands.getoutput('pkg-config libxml-2.0 --cflags')
if libxml2_cflags[:2] not in ["-I", "-D"]:
libxml2_cflags = commands.getoutput('xml2-config --cflags')
if libxml2_cflags[:2] not in ["-I", "-D"]:
print "Error : cannot get LibXML2 pre-processor and compiler flags"
libxml2_libs = commands.getoutput('pkg-config libxml-2.0 --libs')
if libxml2_libs[:2] not in ["-l", "-L"]:
libxml2_libs = commands.getoutput('xml2-config --libs')
if libxml2_libs[:2] not in ["-l", "-L"]:
print "Error : cannot get LibXML2 linker flags"
# XMLSec1
cmd = 'pkg-config xmlsec1-%s --cflags' % xmlsec1_crypto
xmlsec1_cflags = commands.getoutput(cmd)
if xmlsec1_cflags[:2] not in ["-I", "-D"]:
cmd = 'xmlsec1-config --cflags --crypto=%s' % xmlsec1_crypto
xmlsec1_cflags = commands.getoutput(cmd)
if xmlsec1_cflags[:2] not in ["-I", "-D"]:
print "Error : cannot get XMLSec1 pre-processor and compiler flags"
cmd = 'pkg-config xmlsec1-%s --libs' % xmlsec1_crypto
xmlsec1_libs = commands.getoutput(cmd)
if xmlsec1_libs[:2] not in ["-l", "-L"]:
cmd = 'xmlsec1-config --libs --crypto=%s' % xmlsec1_crypto
xmlsec1_libs = commands.getoutput(cmd)
if xmlsec1_libs[:2] not in ["-l", "-L"]:
print "Error : cannot get XMLSec1 linker flags"
print gobject_cflags
print gobject_libs
print libxml2_cflags
print libxml2_libs
print xmlsec1_cflags
print xmlsec1_libs
extract_cflags(gobject_cflags)
extract_libs(gobject_libs)
extract_cflags(libxml2_cflags)
extract_libs(libxml2_libs)
extract_cflags(xmlsec1_cflags)
extract_libs(xmlsec1_libs)
# FIXME : cflags & libs for lasso
include_dirs.append('..')
library_dirs.append('../lasso/.libs')
libraries.append('lasso')
em = Extension("lassomod",
sources = ["py_lasso.c",
"lassomod.c",
"utils.c", "wrap_objs.c"],
define_macros = define_macros,
include_dirs = include_dirs,
library_dirs = library_dirs,
libraries = libraries
)
doclines = __doc__.split("\n")
setup(name = "pylasso",
version = "0.0.1",
description = doclines[0],
long_description = "\n" . join(doclines[2:]),
author = "Valery Febvre",
author_email = "vfebvre@easter-eggs.com",
license = "GNU GPL",
platforms = ["any"],
url = "http://lasso.labs.libre-entreprise.org",
ext_modules = [em],
py_modules = ["lasso", "lasso_strings"]
)

100
python/utils.c Normal file
View File

@ -0,0 +1,100 @@
/* $Id$
*
* PyXMLSec - Python bindings for XML Security library (XMLSec)
*
* Copyright (C) 2003-2004 Easter-eggs, Valery Febvre
* http://pyxmlsec.labs.libre-entreprise.org
*
* Author: Valery Febvre <vfebvre@easter-eggs.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdarg.h>
#include "utils.h"
int CheckArgs(PyObject *args, char *format) {
PyObject *obj;
int i, nb_args;
nb_args = PyTuple_GET_SIZE(args);
/* lowercase means that arg is optional */
/* O : Instance */
/* C : Callable */
/* S : String */
/* I : Integer */
/* F : File */
for (i = 0; i < nb_args; i++) {
obj = PyTuple_GET_ITEM(args, i);
/* O or o : instance */
if (format[i] == 'O' || format[i] == 'o') {
if (!PyInstance_Check(obj)) {
if (format[i] == 'o' && obj == Py_None) continue;
PyErr_Format(xmlsec_error,
"%s() argument %d must be an instance.",
format + nb_args, i+1);
return 0;
}
}
/* C or c : callable */
else if (format[i] == 'C' || format[i] == 'c') {
if (!PyCallable_Check(obj)) {
if (format[i] == 'c' && obj == Py_None) continue;
PyErr_Format(xmlsec_error,
"%s() argument %d must be callable.",
format + nb_args, i+1);
return 0;
}
}
/* S or s : string */
else if (format[i] == 'S' || format[i] == 's') {
if (!PyString_Check(obj)) {
if (format[i] == 's' && obj == Py_None) continue;
PyErr_Format(xmlsec_error,
"%s() argument %d must be a string.",
format + nb_args, i+1);
return 0;
}
}
/* I or i : integer */
else if (format[i] == 'I' || format[i] == 'i') {
if (!PyInt_Check(obj)) {
if (format[i] == 'i' && obj == Py_None) continue;
PyErr_Format(xmlsec_error,
"%s() argument %d must be an integer.",
format + nb_args, i+1);
return 0;
}
}
/* F or f : file */
else if (format[i] == 'F' || format[i] == 'f') {
if (!PyFile_Check(obj)) {
if (format[i] == 'f' && obj == Py_None) continue;
PyErr_Format(xmlsec_error,
"%s() argument %d must be a file.",
format + nb_args, i+1);
return 0;
}
}
/* type is variable */
else if (format[i] == '?') {
continue;
}
}
return 1;
}

11
python/utils.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef __PYXMLSEC_UTILS_H__
#define __PYXMLSEC_UTILS_H__
#undef _POSIX_C_SOURCE
#include <Python.h>
extern PyObject *xmlsec_error;
int CheckArgs(PyObject *args, char *format);
#endif /* __PYXMLSEC_UTILS_H__ */

164
python/wrap_objs.c Normal file
View File

@ -0,0 +1,164 @@
#include "wrap_objs.h"
/*****************************************************************************/
/* Functions to wrap Python objects -> C objects */
/*****************************************************************************/
xmlChar **PythonStringList_get(PyObject *list_obj) {
int i;
xmlChar **list = NULL;
if (list_obj == Py_None) return NULL;
/* convert Python list into a NULL terminated C list */
list = (xmlChar **) xmlMalloc ((PyList_Size(list_obj)+1)*sizeof (xmlChar *));
for (i=0; i<PyList_Size(list_obj); i++)
list[i] = PyString_AsString(PyList_GetItem(list_obj, i));
list[i] = NULL;
return list;
}
/*****************************************************************************/
/* Functions to wrap C objects -> Python objects */
/*****************************************************************************/
PyObject *wrap_int(int val) {
return (Py_BuildValue("i", val));
}
PyObject *wrap_charPtr(char *str) {
PyObject *ret;
if (str == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyString_FromString(str);
/* deallocation */
free (str);
return (ret);
}
PyObject *wrap_charPtrConst(const char *str) {
PyObject *ret;
if (str == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyString_FromString(str);
return (ret);
}
/*****************************************************************************/
/* Functions to wrap LibXML objects -> Python objects */
/*****************************************************************************/
PyObject *wrap_xmlCharPtr(xmlChar *str) {
PyObject *ret;
if (str == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyString_FromString((char *) str);
/* deallocation */
xmlFree(str);
return (ret);
}
PyObject *wrap_xmlCharPtrConst(const xmlChar *str) {
PyObject *ret;
if (str == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyString_FromString((char *) str);
return (ret);
}
PyObject *wrap_xmlDocPtr(xmlDocPtr doc) {
PyObject *ret;
if (doc == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyCObject_FromVoidPtrAndDesc((void *) doc, (char *) "xmlDocPtr", NULL);
return (ret);
}
PyObject *wrap_xmlNodePtr(xmlNodePtr node) {
PyObject *ret;
if (node == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "xmlNodePtr", NULL);
return (ret);
}
PyObject *wrap_xmlNodeSetPtr(xmlNodeSetPtr nset) {
PyObject *ret;
if (nset == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyCObject_FromVoidPtrAndDesc((void *) nset, (char *) "xmlNodeSetPtr", NULL);
return (ret);
}
PyObject *wrap_xmlOutputBufferPtr(xmlOutputBufferPtr buf) {
PyObject *ret;
if (buf == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyCObject_FromVoidPtrAndDesc((void *) buf,
(char *) "xmlOutputBufferPtr", NULL);
return (ret);
}
/*****************************************************************************/
/* Functions to wrap XMLSec objects -> Python objects */
/*****************************************************************************/
PyObject *wrap_xmlSecPtr(xmlSecPtr ptr) {
PyObject *ret;
if (ptr == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = (PyCObject_FromVoidPtr((void *) ptr, NULL));
return (ret);
}
PyObject *wrap_xmlSecBytePtr(xmlSecByte *str) {
PyObject *ret;
if (str == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyString_FromString(str);
xmlFree(str);
return (ret);
}
PyObject *wrap_xmlSecBytePtrConst(const xmlSecByte *str) {
PyObject *ret;
if (str == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret = PyString_FromString(str);
return (ret);
}

65
python/wrap_objs.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef __PYXMLSEC_WRAP_OBJS_H__
#define __PYXMLSEC_WRAP_OBJS_H__
#undef _POSIX_C_SOURCE
#include <Python.h>
#include <libxml/xpath.h>
#include <libxml/xmlmemory.h>
#include <xmlsec/xmlsec.h>
typedef struct {
PyObject_HEAD
xmlDocPtr obj;
} xmlDocPtr_object;
typedef struct {
PyObject_HEAD
xmlNodePtr obj;
} xmlNodePtr_object;
typedef struct {
PyObject_HEAD
xmlOutputBufferPtr obj;
} xmlOutputBufferPtr_object;
typedef struct {
PyObject_HEAD
xmlNodeSetPtr obj;
} xmlNodeSetPtr_object;
typedef struct {
PyObject_HEAD
xmlSecPtr obj;
} xmlSecPtr_object;
/* Functions to wrap LibXML Python objects -> LibXML C objects */
#define xmlDocPtr_get(v) (((v) == Py_None) ? NULL : (((xmlDocPtr_object *)(PyObject_GetAttr(v, PyString_FromString("_o"))))->obj))
#define xmlNodePtr_get(v) (((v) == Py_None) ? NULL : (((xmlNodePtr_object *)(PyObject_GetAttr(v, PyString_FromString("_o"))))->obj))
#define xmlNodeSetPtr_get(v) (((v) == Py_None) ? NULL : (((xmlNodeSetPtr_object *)(PyObject_GetAttr(v, PyString_FromString("_o"))))->obj))
#define xmlOutputBufferPtr_get(v) (((v) == Py_None) ? NULL : (((xmlOutputBufferPtr_object *)(PyObject_GetAttr(v, PyString_FromString("_o"))))->obj))
/* Functions to wrap XMLSec Python objects -> XMLSec C objects */
#define xmlSecPtr_get(v) (((v) == Py_None) ? NULL : (((xmlSecPtr_object *)(PyObject_GetAttr(v, PyString_FromString("_o"))))->obj))
/* Functions to wrap Python objects -> C objects */
#define PythonFile_get(v) (((v) == Py_None) ? NULL : (PyFile_Check(v) ? (PyFile_AsFile(v)) : stdout))
xmlChar **PythonStringList_get(PyObject *list_obj);
PyObject *wrap_int(int val);
PyObject *wrap_charPtr(char *str);
PyObject *wrap_charPtrConst(const char *str);
PyObject *wrap_xmlCharPtr(xmlChar *str);
PyObject *wrap_xmlCharPtrConst(const xmlChar *str);
PyObject *wrap_xmlDocPtr(xmlDocPtr doc);
PyObject *wrap_xmlNodePtr(xmlNodePtr node);
PyObject *wrap_xmlNodeSetPtr(xmlNodeSetPtr nset);
PyObject *wrap_xmlOutputBufferPtr(xmlOutputBufferPtr buf);
PyObject *wrap_xmlSecPtr(xmlSecPtr ptr);
PyObject *wrap_xmlSecBytePtr(xmlSecByte *str);
PyObject *wrap_xmlSecBytePtrConst(const xmlSecByte *str);
#endif /* __PYXMLSEC_WRAP_OBJS_H__ */