Skip to content

Commit c69549a

Browse files
authored
[pyext] Add SWIG sairedis python submodule (#806)
Bring sairedis python modules for both python2 and python3 using SWIG and extra helper methods, as well as adding debian packages for those modules.
1 parent bc58b0f commit c69549a

29 files changed

+1484
-1
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SUBDIRS = meta lib vslib python
1+
SUBDIRS = meta lib vslib python pyext
22

33
if SYNCD
44
SUBDIRS += syncd saiplayer saidump saidiscovery saisdkdump saiasiccmp tests

configure.ac

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ AC_PROG_CC
1010
AC_PROG_CXX
1111
AC_PROG_LIBTOOL
1212
AC_HEADER_STDC
13+
AM_PATH_PYTHON
14+
AM_PATH_PYTHON3
1315

1416
AM_CONDITIONAL(sonic_asic_platform_barefoot, test x$CONFIGURED_PLATFORM = xbarefoot)
1517
AM_CONDITIONAL(sonic_asic_platform_mellanox, test x$CONFIGURED_PLATFORM = xmellanox)
@@ -76,6 +78,10 @@ AC_ARG_ENABLE(redis-test,
7678
esac],[rtest=false])
7779
AM_CONDITIONAL(RTEST, test x$rtest = xtrue)
7880

81+
AM_CONDITIONAL(ARCH64, test `getconf LONG_BIT` = "64")
82+
83+
AC_PATH_PROGS(SWIG, [swig3.0 swig])
84+
7985
CFLAGS_COMMON=""
8086
CFLAGS_COMMON+=" -ansi"
8187
CFLAGS_COMMON+=" -fPIC"
@@ -144,4 +150,7 @@ AC_OUTPUT(Makefile
144150
saidiscovery/Makefile
145151
saiasiccmp/Makefile
146152
tests/Makefile
153+
pyext/Makefile
154+
pyext/py2/Makefile
155+
pyext/py3/Makefile
147156
python/setup.py)

debian/control

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,16 @@ Depends:
114114
libsaimetadata (= ${binary:Version}),
115115
${misc:Depends}
116116
Description: debugging symbols for libsaimetadata
117+
118+
Package: python-pysairedis
119+
Architecture: any
120+
Depends: ${shlibs:Depends}, ${misc:Pre-Depends}
121+
Section: libs
122+
Description: This package contains Switch State Service sairedis Python2 library.
123+
124+
Package: python3-pysairedis
125+
Architecture: any
126+
Depends: ${shlibs:Depends}, ${misc:Pre-Depends}
127+
Section: libs
128+
Description: This package contains Switch State Service sairedis Python3 library.
129+

debian/python-pysairedis.dirs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
usr/lib/python2.7

debian/python-pysairedis.install

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
usr/lib/python2.7/dist-packages/pysairedis/*

debian/python3-pysairedis.dirs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
usr/lib/python3

debian/python3-pysairedis.install

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
usr/lib/python3/dist-packages/pysairedis/*

m4/python3.m4

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
## ------------------------ -*- Autoconf -*-
2+
## Python 3 file handling, adapted from:
3+
## Python file handling
4+
## From Andrew Dalke
5+
## Updated by James Henstridge
6+
## ------------------------
7+
# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
8+
# Free Software Foundation, Inc.
9+
#
10+
# This file is free software; the Free Software Foundation
11+
# gives unlimited permission to copy and/or distribute it,
12+
# with or without modifications, as long as this notice is preserved.
13+
14+
# AM_PATH_PYTHON3([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
15+
# ---------------------------------------------------------------------------
16+
# Adds support for distributing Python modules and packages. To
17+
# install modules, copy them to $(pythondir), using the python_PYTHON
18+
# automake variable. To install a package with the same name as the
19+
# automake package, install to $(pkgpythondir), or use the
20+
# pkgpython_PYTHON automake variable.
21+
#
22+
# The variables $(pyexecdir) and $(pkgpyexecdir) are provided as
23+
# locations to install python extension modules (shared libraries).
24+
# Another macro is required to find the appropriate flags to compile
25+
# extension modules.
26+
#
27+
# If your package is configured with a different prefix to python,
28+
# users will have to add the install directory to the PYTHONPATH
29+
# environment variable, or create a .pth file (see the python
30+
# documentation for details).
31+
#
32+
# If the MINIMUM-VERSION argument is passed, AM_PATH_PYTHON will
33+
# cause an error if the version of python installed on the system
34+
# doesn't meet the requirement. MINIMUM-VERSION should consist of
35+
# numbers and dots only.
36+
AC_DEFUN([AM_PATH_PYTHON3],
37+
[
38+
dnl Find a Python 3 interpreter
39+
m4_define_default([_AM_PYTHON3_INTERPRETER_LIST],
40+
[python3 python3.0 python3.1])
41+
42+
m4_if([$1],[],[
43+
dnl No version check is needed.
44+
# Find any Python 3 interpreter.
45+
if test -z "$PYTHON3"; then
46+
AC_PATH_PROGS([PYTHON3], _AM_PYTHON3_INTERPRETER_LIST, :)
47+
fi
48+
am_display_PYTHON3=python3
49+
], [
50+
dnl A version check is needed.
51+
if test -n "$PYTHON3"; then
52+
# If the user set $PYTHON3, use it and don't search something else.
53+
AC_MSG_CHECKING([whether $PYTHON3 version >= $1])
54+
AM_PYTHON3_CHECK_VERSION([$PYTHON3], [$1],
55+
[AC_MSG_RESULT(yes)],
56+
[AC_MSG_ERROR(too old)])
57+
am_display_PYTHON3=$PYTHON3
58+
else
59+
# Otherwise, try each interpreter until we find one that satisfies
60+
# VERSION.
61+
AC_CACHE_CHECK([for a Python3 interpreter with version >= $1],
62+
[am_cv_pathless_PYTHON3],[
63+
for am_cv_pathless_PYTHON3 in _AM_PYTHON3_INTERPRETER_LIST none; do
64+
test "$am_cv_pathless_PYTHON3" = none && break
65+
AM_PYTHON3_CHECK_VERSION([$am_cv_pathless_PYTHON3], [$1], [break])
66+
done])
67+
# Set $PYTHON3 to the absolute path of $am_cv_pathless_PYTHON3.
68+
if test "$am_cv_pathless_PYTHON3" = none; then
69+
PYTHON3=:
70+
else
71+
AC_PATH_PROG([PYTHON3], [$am_cv_pathless_PYTHON3])
72+
fi
73+
am_display_PYTHON3=$am_cv_pathless_PYTHON3
74+
fi
75+
])
76+
77+
if test "$PYTHON3" = :; then
78+
dnl Run any user-specified action, or abort.
79+
m4_default([$3], [AC_MSG_ERROR([no suitable Python3 interpreter found])])
80+
else
81+
82+
dnl Query Python3 for its version number. Getting [:3] seems to be
83+
dnl the best way to do this; it's what "site.py" does in the standard
84+
dnl library.
85+
86+
AC_CACHE_CHECK([for $am_display_PYTHON3 version], [am_cv_python3_version],
87+
[am_cv_python3_version=`$PYTHON3 -c "import sys; sys.stdout.write(sys.version[[:3]])"`])
88+
AC_SUBST([PYTHON3_VERSION], [$am_cv_python3_version])
89+
90+
dnl Use the values of $prefix and $exec_prefix for the corresponding
91+
dnl values of PYTHON3_PREFIX and PYTHON3_EXEC_PREFIX. These are made
92+
dnl distinct variables so they can be overridden if need be. However,
93+
dnl general consensus is that you shouldn't need this ability.
94+
95+
AC_SUBST([PYTHON3_PREFIX], ['${prefix}'])
96+
AC_SUBST([PYTHON3_EXEC_PREFIX], ['${exec_prefix}'])
97+
98+
dnl At times (like when building shared libraries) you may want
99+
dnl to know which OS platform Python3 thinks this is.
100+
101+
AC_CACHE_CHECK([for $am_display_PYTHON3 platform], [am_cv_python3_platform],
102+
[am_cv_python3_platform=`$PYTHON3 -c "import sys; sys.stdout.write(sys.platform)"`])
103+
AC_SUBST([PYTHON3_PLATFORM], [$am_cv_python3_platform])
104+
105+
AC_CACHE_CHECK([for $am_display_PYTHON3 bldlibrary], [am_cv_python3_bldlibrary],
106+
[am_cv_python3_bldlibrary=`$PYTHON3 -c "import sys; import sysconfig; sys.stdout.write(sysconfig.get_config_var('BLDLIBRARY'))"`])
107+
AC_SUBST([PYTHON3_BLDLIBRARY], [$am_cv_python3_bldlibrary])
108+
109+
dnl Set up 4 directories:
110+
111+
dnl python3dir -- where to install python3 scripts. This is the
112+
dnl site-packages directory, not the python3 standard library
113+
dnl directory like in previous automake betas. This behavior
114+
dnl is more consistent with lispdir.m4 for example.
115+
dnl Query distutils for this directory. distutils does not exist in
116+
dnl Python3 1.5, so we fall back to the hardcoded directory if it
117+
dnl doesn't work.
118+
AC_CACHE_CHECK([for $am_display_PYTHON3 script directory],
119+
[am_cv_python3_python3dir],
120+
[if test "x$prefix" = xNONE
121+
then
122+
am_py_prefix=$ac_default_prefix
123+
else
124+
am_py_prefix=$prefix
125+
fi
126+
am_cv_python3_python3dir=`$PYTHON3 -c "import sys; from distutils import sysconfig; sys.stdout.write(sysconfig.get_python_lib(0,0,prefix='$am_py_prefix'))" 2>/dev/null ||
127+
echo "$PYTHON3_PREFIX/lib/python$PYTHON3_VERSION/site-packages"`
128+
case $am_cv_python3_python3dir in
129+
$am_py_prefix*)
130+
am__strip_prefix=`echo "$am_py_prefix" | sed 's|.|.|g'`
131+
am_cv_python3_python3dir=`echo "$am_cv_python3_python3dir" | sed "s,^$am__strip_prefix,$PYTHON3_PREFIX,"`
132+
;;
133+
esac
134+
])
135+
AC_SUBST([python3dir], [$am_cv_python3_python3dir])
136+
137+
dnl pkgpython3dir -- $PACKAGE directory under python3dir. Was
138+
dnl PYTHON3_SITE_PACKAGE in previous betas, but this naming is
139+
dnl more consistent with the rest of automake.
140+
141+
AC_SUBST([pkgpython3dir], [\${python3dir}/$PACKAGE])
142+
143+
dnl pyexecdir -- directory for installing python3 extension modules
144+
dnl (shared libraries)
145+
dnl Query distutils for this directory. distutils does not exist in
146+
dnl Python3 1.5, so we fall back to the hardcoded directory if it
147+
dnl doesn't work.
148+
AC_CACHE_CHECK([for $am_display_PYTHON3 extension module directory],
149+
[am_cv_python3_pyexecdir],
150+
[if test "x$exec_prefix" = xNONE
151+
then
152+
am_py_exec_prefix=$am_py_prefix
153+
else
154+
am_py_exec_prefix=$exec_prefix
155+
fi
156+
am_cv_python3_pyexecdir=`$PYTHON3 -c "import sys; from distutils import sysconfig; sys.stdout.write(sysconfig.get_python_lib(1,0,prefix='$am_py_exec_prefix'))" 2>/dev/null ||
157+
echo "$PYTHON3_EXEC_PREFIX/lib/python$PYTHON3_VERSION/site-packages"`
158+
case $am_cv_python3_pyexecdir in
159+
$am_py_exec_prefix*)
160+
am__strip_prefix=`echo "$am_py_exec_prefix" | sed 's|.|.|g'`
161+
am_cv_python3_pyexecdir=`echo "$am_cv_python3_pyexecdir" | sed "s,^$am__strip_prefix,$PYTHON3_EXEC_PREFIX,"`
162+
;;
163+
esac
164+
])
165+
AC_SUBST([py3execdir], [$am_cv_python3_pyexecdir])
166+
167+
dnl pkgpy3execdir -- $(py3execdir)/$(PACKAGE)
168+
169+
AC_SUBST([pkgpy3execdir], [\${py3execdir}/$PACKAGE])
170+
171+
dnl Run any user-specified action.
172+
$2
173+
fi
174+
175+
])
176+
177+
178+
# AM_PYTHON3_CHECK_VERSION(PROG, VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
179+
# ---------------------------------------------------------------------------
180+
# Run ACTION-IF-TRUE if the Python3 interpreter PROG has version >= VERSION.
181+
# Run ACTION-IF-FALSE otherwise.
182+
# This test uses sys.hexversion instead of the string equivalent (first
183+
# word of sys.version), in order to cope with versions such as 2.2c1.
184+
# This supports Python3 2.0 or higher. (2.0 was released on October 16, 2000).
185+
AC_DEFUN([AM_PYTHON3_CHECK_VERSION],
186+
[prog="import sys
187+
# split strings by '.' and convert to numeric. Append some zeros
188+
# because we need at least 4 digits for the hex conversion.
189+
# map returns an iterator in Python3 3.0 and a list in 2.x
190+
minver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
191+
minverhex = 0
192+
# xrange is not present in Python3yy 3.0 and range returns an iterator
193+
for i in list(range(0, 4)): minverhex = (minverhex << 8) + minver[[i]]
194+
sys.exit(sys.hexversion < minverhex)"
195+
AS_IF([AM_RUN_LOG([$1 -c "$prog"])], [$3], [$4])])

meta/sai_serialize.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ void sai_deserialize_object_meta_key(
308308
_In_ const std::string &s,
309309
_Out_ sai_object_meta_key_t& meta_key);
310310

311+
void sai_deserialize_ip_address(
312+
_In_ const std::string& s,
313+
_Out_ sai_ip_address_t& ipaddr);
314+
315+
void sai_deserialize_ip_prefix(
316+
_In_ const std::string &s,
317+
_Out_ sai_ip_prefix_t &ip_prefix);
318+
319+
void sai_deserialize_mac(
320+
_In_ const std::string& s,
321+
_Out_ sai_mac_t& mac);
322+
311323
// deserialize notifications
312324

313325
void sai_deserialize_fdb_event_ntf(

pyext/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SUBDIRS = py2 py3
2+
3+
ACLOCAL_AMFLAGS = -I m4

pyext/fdb.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
// TODO auto generate
3+
4+
typedef struct _sai_fdb_api_t
5+
{
6+
sai_status_t create_fdb_entry(
7+
_In_ const sai_fdb_entry_t *fdb_entry,
8+
_In_ uint32_t attr_count,
9+
_In_ const sai_attribute_t *attr_list);
10+
11+
sai_status_t remove_fdb_entry(
12+
_In_ const sai_fdb_entry_t *fdb_entry);
13+
14+
sai_status_t set_fdb_entry_attribute(
15+
_In_ const sai_fdb_entry_t *fdb_entry,
16+
_In_ const sai_attribute_t *attr);
17+
18+
sai_status_t get_fdb_entry_attribute(
19+
_In_ const sai_fdb_entry_t *fdb_entry,
20+
_In_ uint32_t attr_count,
21+
_Inout_ sai_attribute_t *attr_list);
22+
23+
sai_status_t flush_fdb_entries(
24+
_In_ sai_object_id_t switch_id,
25+
_In_ uint32_t attr_count,
26+
_In_ const sai_attribute_t *attr_list);
27+
28+
sai_status_t create_fdb_entries(
29+
_In_ uint32_t object_count,
30+
_In_ const sai_fdb_entry_t *fdb_entry,
31+
_In_ const uint32_t *attr_count,
32+
_In_ const sai_attribute_t **attr_list,
33+
_In_ sai_bulk_op_error_mode_t mode,
34+
_Out_ sai_status_t *object_statuses);
35+
36+
sai_status_t remove_fdb_entries(
37+
_In_ uint32_t object_count,
38+
_In_ const sai_fdb_entry_t *fdb_entry,
39+
_In_ sai_bulk_op_error_mode_t mode,
40+
_Out_ sai_status_t *object_statuses);
41+
42+
sai_status_t set_fdb_entries_attribute(
43+
_In_ uint32_t object_count,
44+
_In_ const sai_fdb_entry_t *fdb_entry,
45+
_In_ const sai_attribute_t *attr_list,
46+
_In_ sai_bulk_op_error_mode_t mode,
47+
_Out_ sai_status_t *object_statuses);
48+
49+
sai_status_t get_fdb_entries_attribute(
50+
_In_ uint32_t object_count,
51+
_In_ const sai_fdb_entry_t *fdb_entry,
52+
_In_ const uint32_t *attr_count,
53+
_Inout_ sai_attribute_t **attr_list,
54+
_In_ sai_bulk_op_error_mode_t mode,
55+
_Out_ sai_status_t *object_statuses);
56+
57+
} sai_fdb_api_t;

pyext/getapi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// need to be extern C
3+
4+
sai_status_t sai_get_switch_api(sai_switch_api_t* out);
5+
sai_status_t sai_get_lag_api(sai_lag_api_t* out);
6+
sai_status_t sai_get_router_interface_api(sai_router_interface_api_t* out);
7+
sai_status_t sai_get_next_hop_api(sai_next_hop_api_t* out);
8+
sai_status_t sai_get_route_api(sai_route_api_t* out);
9+
sai_status_t sai_get_vlan_api(sai_vlan_api_t* out);
10+
sai_status_t sai_get_fdb_api(sai_fdb_api_t* out);

0 commit comments

Comments
 (0)