Skip to content

Commit 0492086

Browse files
author
renierm
committed
initial import for python bindings 2.0
0 parents  commit 0492086

24 files changed

+9249
-0
lines changed

COPYING

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
Copyright (c) 2003, Intel Corporation
3+
(C) Copyright IBM Corp. 2003, 2004
4+
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or
8+
without modification, are permitted provided that the following
9+
conditions are met:
10+
11+
Redistributions of source code must retain the above copyright
12+
notice, this list of conditions and the following disclaimer.
13+
Redistributions in binary form must reproduce the above copyright
14+
notice, this list of conditions and the following disclaimer in
15+
the documentation and/or other materials provided with the distribution.
16+
17+
Neither the name of Intel Corporation, IBM Corp., nor the names
18+
of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28+
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
34+
35+

MANIFEST.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include Makefile
2+
include README
3+
include COPYING
4+
include *.i
5+
include swigify.py
6+
include deps.py
7+
include setup.py
8+
recursive-include examples *.py

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
all:
3+
python setup.py build
4+
ln -sf `find ./ -name "_openhpi.so" | head -n1`
5+
6+
install:
7+
python setup.py install
8+
9+
dist:
10+
python setup.py sdist
11+
12+
clean:
13+
rm -rf build dist *_wrap.c openhpi.py *.pyc MANIFEST *.so
14+

README

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
OpenHPI Python Extension (PyOpenHPI)
2+
====================================
3+
4+
Description
5+
-----------
6+
7+
An extension module that makes it possible to access
8+
HPI api functions and structures from the Python language.
9+
10+
It supports everything that is exposed through SaHpi.h,
11+
SaHpiAtca.h, SaHpiBladeCenter.h, oHpi.h, and oh_utils.h.
12+
13+
Requirements
14+
------------
15+
16+
Python >= 2.3
17+
SWIG >= 1.3.27
18+
GLIB >= 2.2.0
19+
OpenHPI >= 2.5.3
20+
21+
Note: If any of these requirements are installed on non-standard
22+
directories (e.g. not in /usr or /usr/local) the extension will not build.
23+
In that case you will need to edit setup.py and deps.py to look for the
24+
dependencies in the right directories.
25+
26+
Building and Installing
27+
-----------------------
28+
29+
To build:
30+
31+
make
32+
33+
- Or -
34+
35+
python setup.py build
36+
37+
To install:
38+
39+
make install # as root
40+
41+
- Or -
42+
43+
python setup.py install # as root
44+
45+
Using
46+
-----
47+
To use, import the openhpi python extension like this:
48+
49+
from openhpi import *
50+
51+
Then, you will be able to call the HPI API functions as you would in C.
52+
All the APIs and HPI structs are managed as in C, except for
53+
two exceptions:
54+
55+
1. HPI structs are translated to objects in Python. Here
56+
are some examples on how to create HPI objects:
57+
58+
buffer = SaHpiTextBufferT()
59+
print buffer.Data[3] # members are accessed like in C
60+
print buffer.Data # This fetches the entire Data string
61+
buffer.Language = SAHPI_LANG_ZULU
62+
63+
res = SaHpiRptEntryT()
64+
print res.ResourceEntity.Entry[4].EntityType
65+
res.ResourceEntity.Entry[4].EntityType = SAHPI_ENT_SWITCH
66+
67+
dinfo = SaHpiDomainInfoT()
68+
print dinfo.Guid[0] # prints first character of Guid
69+
print dinfo.Guid # prints a string
70+
71+
dentry = SaHpiDrtEntryT()
72+
dentry.IsPeer = SAHPI_TRUE
73+
74+
Struct members that are character arrays (like
75+
SaHpiTextBufferT.Data) can be accessed as a whole string
76+
by referencing the plain array as shown above.
77+
78+
2. Any output argument of an HPI API call whose type is a
79+
pointer to a number (e.g. SaHpiSessionIdT *SessionId) gets
80+
treated differently for that API call. That argument will
81+
be eliminated and returned along with the error code. So
82+
for that API, a list of two values is returned. It is done
83+
like this because, although possible, its cumbersome to
84+
provide a reference to a number for a function call in
85+
Python. This helps improves readability, since all output
86+
variables end up on the left side of the assignment, and all
87+
the inputs end up as function arguments on the right side.
88+
Output arguments of type (char *) have also been moved to the
89+
left side of the equation.
90+
Following, some examples of this change:
91+
92+
error, sid = saHpiSessionOpen(SAHPI_UNSPECIFIED_DOMAIN_ID, None)
93+
# if error == SA_OK, sid will contain the session id.
94+
event = SaHpiEventT()
95+
error, qstatus = saHpiEventGet(sid,
96+
SAHPI_TIMEOUT_IMMEDIATE,
97+
event, None, None)
98+
# if error == SA_OK, qstatus contains the event queue status
99+
# Also, event will have the returned event
100+
101+
# Printing the names of all plugins loaded
102+
error = SA_OK; plugin = None
103+
while error == SA_OK:
104+
error, plugin = oHpiPluginGetNext(plugin)
105+
print plugin
106+
107+
# Creating a handler (instance to a plugin)
108+
config = { 'plugin': 'libsimulator',
109+
'entity_root': '{SYSTEM_CHASSIS,1}',
110+
'name': 'test0' }
111+
# First argument is a dictionary which the extension
112+
# converts to the proper GHashTable type.
113+
error, hid = oHpiHandlerCreate(config)
114+
# returns id of new handler
115+
116+
References
117+
----------
118+
119+
All of the files ending in .i for this package can be used as a reference in order to learn how some api calls (among other things) have been mapped to Python.
120+
However, in order to understand those .i files another reference needs to be used:
121+
http://www.swig.org/Doc1.3/Contents.html (The SWIG 1.3 documentation)
122+

announcement_utils.i

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* -*- linux-c -*-
2+
*
3+
* (C) Copyright IBM Corp. 2004
4+
*
5+
* This program is distributed in the hope that it will be useful,
6+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
7+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This
8+
* file and program are licensed under a BSD style license. See
9+
* the Copying file included with the OpenHPI distribution for
10+
* full licensing terms.
11+
*
12+
* Authors:
13+
* David Ashley <[email protected]>
14+
* Renier Morales <[email protected]>
15+
*/
16+
17+
#ifndef __ANNOUNCEMENT_UTILS_H
18+
#define __ANNOUNCEMENT_UTILS_H
19+
20+
#include <SaHpi.h>
21+
#include <glib.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#define OH_ANNOUNCEMENT_MAX_SIZE 0
28+
29+
/* this struct encapsulates all the data for annunciator announcements */
30+
/* the log records themselves are stored in the el GList */
31+
typedef struct {
32+
SaHpiEventLogEntryIdT nextId; // next generated Id i.e. number of entries
33+
GList *annentries; // list of oh_ann_entry structs
34+
} oh_announcement;
35+
36+
/* this structure encapsulates the actual log entry and its context */
37+
typedef struct {
38+
SaHpiAnnouncementT annentry;
39+
SaHpiAnnunciatorNumT num; // associated annunciator
40+
} oh_ann_entry;
41+
42+
/* General EL utility calls */
43+
oh_announcement *oh_announcement_create(void);
44+
SaErrorT oh_announcement_close(oh_announcement *ann);
45+
SaErrorT oh_announcement_append(oh_announcement *ann, SaHpiAnnouncementT *myann);
46+
SaErrorT oh_announcement_clear(oh_announcement *ann);
47+
SaErrorT oh_announcement_get(oh_announcement *ann, SaHpiEntryIdT srchid,
48+
SaHpiAnnouncementT *myann);
49+
SaErrorT oh_announcement_get_next(oh_announcement *ann, SaHpiSeverityT sev,
50+
SaHpiBoolT ack, SaHpiAnnouncementT *entry);
51+
SaErrorT oh_announcement_ack(oh_announcement *ann, SaHpiEntryIdT entry,
52+
SaHpiSeverityT sev);
53+
SaErrorT oh_announcement_del(oh_announcement *ann, SaHpiEntryIdT entry,
54+
SaHpiSeverityT sev);
55+
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
60+
61+
#endif
62+

deps.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
2+
#
3+
# (C) Copyright IBM Corp. 2006
4+
#
5+
# This program is distributed in the hope that it will be useful,
6+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
7+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This
8+
# file and program are licensed under a BSD style license. See
9+
# the Copying file included with the OpenHPI distribution for
10+
# full licensing terms.
11+
#
12+
"""
13+
Provides dependency information on requred versions and cflags
14+
"""
15+
__author__ = 'Renier Morales <[email protected]>'
16+
17+
import sys, os, re
18+
19+
def get_glib_cflags():
20+
"""Gets the cflags needed to use glib"""
21+
pkgcmd = os.popen('PKG_CONFIG_PATH=$PKG_CONFIG_PATH:' +
22+
'/usr/local/lib/pkgconfig ' +
23+
'pkg-config --cflags glib-2.0', 'r')
24+
pkgcmd_text = pkgcmd.read()
25+
pkgcmd.close()
26+
includes = pkgcmd_text.split()
27+
for x in range(len(includes)):
28+
includes[x] = includes[x][2:]
29+
30+
return includes
31+
32+
def check_pkgcfg_ver(reqver_text, pkgname):
33+
"""Check for requried version using pkg-config"""
34+
reqver = map(int, reqver_text.split('.'))
35+
pkgcmd = os.popen('PKG_CONFIG_PATH=$PKG_CONFIG_PATH:' +
36+
'/usr/local/lib/pkgconfig ' +
37+
'pkg-config --modversion ' + pkgname, 'r')
38+
pkgcmd_text = pkgcmd.read()
39+
pkgcmd.close()
40+
match = re.search(r'^([0-9]+)\.([0-9]+)\.([0-9]+)', pkgcmd_text)
41+
if match:
42+
pkgver_str = match.groups()
43+
pkgver = map(int, pkgver_str)
44+
return check_ver(pkgver, reqver)
45+
else:
46+
return '<not found>'
47+
48+
def check_glibver(reqver_text):
49+
"""Check for required glib version"""
50+
return check_pkgcfg_ver(reqver_text, 'glib-2.0')
51+
52+
def check_openhpiver(reqver_text):
53+
"""Check for required OpenHPI version"""
54+
return check_pkgcfg_ver(reqver_text, 'openhpi')
55+
56+
def check_swigver(reqver_text):
57+
"""Check for required SWIG version"""
58+
reqver = map(int, reqver_text.split('.'))
59+
swigcmd = os.popen('swig -version', 'r')
60+
swigcmd_text = swigcmd.read()
61+
swigcmd.close()
62+
match = re.search(r'\sSWIG\sVersion\s([0-9]+)\.([0-9]+)\.([0-9]+)',
63+
swigcmd_text)
64+
if match:
65+
swigver_str = match.groups()
66+
swigver = map(int, swigver_str)
67+
return check_ver(swigver, reqver)
68+
else:
69+
return '<not found>'
70+
71+
def check_pythonver(reqver_text):
72+
"""Check for required Python version"""
73+
reqver = map(int, reqver_text.split('.'))
74+
pythonver = sys.version_info[:3]
75+
return check_ver(pythonver, reqver)
76+
77+
def check_ver(ver, reqver):
78+
if ver[0] < reqver[0]:
79+
return '.'.join(ver)
80+
elif ver[0] == reqver[0]:
81+
if ver[1] < reqver[1]:
82+
return '.'.join(ver)
83+
elif ver[1] == reqver[1]:
84+
if ver[2] < reqver[2]:
85+
return '.'.join(ver)
86+
87+
return None
88+

0 commit comments

Comments
 (0)