Skip to content

Commit

Permalink
add wedpr-python-gateway-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Sep 26, 2024
1 parent 1692e6b commit c22435b
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 78 deletions.
3 changes: 2 additions & 1 deletion cpp/cmake/TargetSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ set(WEDPR_TRANSPORT_SDK_TARGET wedpr-transport-sdk)

# ==== the swig wrapper =====
set(WEDPR_PYTHON_TRANSPORT "wedpr_python_transport")
set(WEDPR_PYTHON_TRANSPORT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TRANSPORT})
set(WEDPR_PYTHON_TRANSPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/python/generated/${WEDPR_PYTHON_TRANSPORT})
set(WEDPR_PYTHON_TRANSPORT_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wedpr-transport/sdk-wrapper/python/bindings/libs)

set(WEDPR_JAVA_TRANSPORT "wedpr_java_transport_jni")
set(WEDPR_JAVA_TRANSPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/java/generated/${WEDPR_JAVA_TRANSPORT})
Expand Down
8 changes: 7 additions & 1 deletion cpp/cmake/python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ if(UNIX AND NOT APPLE)
endif()

# Find Python 3
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
find_package(Python REQUIRED COMPONENTS Interpreter Development)
message("Python_FOUND:${Python_FOUND}")
message("Python_VERSION:${Python_VERSION}")
message("Python_Development_FOUND:${Python_Development_FOUND}")
message("Python_LIBRARIES:${Python_LIBRARIES}")
message("Python_INCLUDE_DIRS:${Python_INCLUDE_DIRS}")

list(APPEND CMAKE_SWIG_FLAGS "-py3" "-DPY3")

function(search_python_module)
Expand Down
57 changes: 26 additions & 31 deletions cpp/wedpr-transport/sdk-wrapper/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
file(GLOB_RECURSE SRCS *.i)
set_source_files_properties(${SRCS} PROPERTIES CPLUSPLUS ON)
option(AUTO_GENERATE "Auto generate the targes" OFF)
if(AUTO_GENERATE)
file(GLOB_RECURSE SRCS *.i)
set_source_files_properties(${SRCS} PROPERTIES CPLUSPLUS ON)

swig_add_library(
${WEDPR_PYTHON_TRANSPORT}
TYPE MODULE
LANGUAGE python
OUTPUT_DIR ${WEDPR_PYTHON_TRANSPORT_DIR}
SOURCES ${SRCS}
)

message("#### Python3_INCLUDE_DIRS: ${Python3_INCLUDE_DIRS}")
target_include_directories(
${WEDPR_PYTHON_TRANSPORT}
PRIVATE
../include
${Python3_INCLUDE_DIRS}
)
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON)
file(MAKE_DIRECTORY ${WEDPR_PYTHON_TRANSPORT_DIR})
file(MAKE_DIRECTORY ${WEDPR_PYTHON_TRANSPORT_LIB_DIR})

swig_add_library(
${WEDPR_PYTHON_TRANSPORT}
TYPE MODULE
LANGUAGE python
OUTPUT_DIR ${WEDPR_PYTHON_TRANSPORT_DIR}
SOURCES ${SRCS}
)
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON)
else()
file(GLOB_RECURSE SRCS src/*.cxx)
add_library(${WEDPR_PYTHON_TRANSPORT} SHARED ${SRCS})
endif()

target_include_directories(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${Python_INCLUDE_DIRS})

# note: macOS is APPLE and also UNIX !
if(APPLE)
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} APPEND PROPERTY
LINK_FLAGS "-flat_namespace -undefined suppress"
)
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES
SUFFIX ".so"
INSTALL_RPATH "@loader_path;@loader_path/../../${WEDPR_PYTHON_TRANSPORT}/.libs"
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES
INSTALL_RPATH "@loader_path;@loader_path/../../${WEDPR_PYTHON_TRANSPORT}/.libs"
)
elseif(UNIX)
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES
INSTALL_RPATH "$ORIGIN:$ORIGIN/../../${WEDPR_PYTHON_TRANSPORT}/.libs"
)
endif()
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${WEDPR_TRANSPORT_SDK_TARGET})

SET(LIBRARY_OUTPUT_PATH ${WEDPR_PYTHON_TRANSPORT_DIR}/)

# Variable PYTHON_LIBRARIES can contains keyword `optimized`
# which won't be interpreted inside a generator expression.
# i.e. we can't use: $<$<PLATFORM_ID:Windows>:${PYTHON_LIBRARIES}>
# see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries
if(MSVC)
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${Python3_LIBRARIES})
else()
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${WEDPR_TRANSPORT_SDK_TARGET} ${Python_LIBRARIES})
endif()

# Configure setup.py and copy to output directory
file(GENERATE OUTPUT ${WEDPR_PYTHON_TRANSPORT_DIR}/__init__.py CONTENT "__version__ = \"${PYTHON_TOOLKIT_VERSION}\"\n")
set(SETUP_PY_IN ${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in)
set(SETUP_PY_OUT ${WEDPR_PYTHON_TRANSPORT_DIR}/setup.py)
configure_file(${SETUP_PY_IN} ${SETUP_PY_OUT})
SET(LIBRARY_OUTPUT_PATH ${WEDPR_PYTHON_TRANSPORT_LIB_DIR})

message(STATUS "CMAKE_INSTALL_INCLUDEDIR => ${CMAKE_INSTALL_INCLUDEDIR}")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR => ${CMAKE_CURRENT_SOURCE_DIR}")
62 changes: 62 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/python/bindings/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sys

from setuptools import find_packages, setup
from setuptools.command.bdist_egg import bdist_egg


class bdist_egg_disabled(bdist_egg):
"""Disabled version of bdist_egg
Prevents setup.py install from performing setuptools' default easy_install,
which it should never ever do.
"""

def run(self):
sys.exit(
"Aborting implicit building of eggs. Use `pip install .` to install from source."
)


setup_args = dict(
name='wedpr-python-gateway-sdk',
packages=find_packages(),
version="1.0.0-rc1",
description="wedpr-python-gateway-sdk: The gateway sdk for WeDPR",
long_description_content_type="text/markdown",
author="Jupyter Development Team",
author_email="[email protected]",
url="https://github.com/WeBankBlockchain/WeDPR-Component",
license="Apache-2.0",
platforms="Linux, Mac OS X",
keywords=['Interactive', 'Interpreter', 'Shell', 'Web'],
python_requires=">=3.8",
include_package_data=True,
classifiers=[
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: Apache License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
],
)

setup_args['cmdclass'] = {
'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled,
}

setup_args['install_requires'] = install_requires = []
with open('requirements.txt') as f:
for line in f.readlines():
req = line.strip()
if not req or req.startswith(('-e', '#')):
continue
install_requires.append(req)


def main():
setup(**setup_args)


if __name__ == '__main__':
main()
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-

import platform
import os
from enum import Enum
import sys
import ctypes


class OSType(Enum):
MACOS_X64 = 1
MACOS_ARM64 = 2
WINDOWS = 3
LINUX_X64 = 4
LINUX_ARM64 = 5


class ArchType(Enum):
ARM64 = 1
X86_64 = 2


class LibLoader:
# the library information
TRANSPORT_LIB_PATH = "libs"
TRANSPORT_LIB_NAME = "wedpr_python_transport"
LIB_PREFIX = "lib"
MACOS_LIB_POSTFIX = "dylib"
WINDOWS_LIB_POSTFIX = "dll"
LINUX_LIB_PORSTFIX = "so"
# the OS name
MACOS_OS = "darwin"
WINDOWS_OS = "windows"
LINUX_OS = "linux"

AARCH64 = "aarch64"
ARM64 = "arm64"
X86_64 = "x86_64"

@staticmethod
def get_arch_type():
arch_type = platform.machine().lower()
if LibLoader.AARCH64 in arch_type or LibLoader.ARM64 in arch_type:
return ArchType.ARM64
if LibLoader.X86_64 in arch_type:
return ArchType.X86_64
raise Exception(f"Unsupported arch type: {arch_type}")

@staticmethod
def get_os_type():
os_type = platform.system().lower()
arch_type = LibLoader.get_arch_type()
if LibLoader.MACOS_OS in os_type:
if arch_type == ArchType.ARM64:
return OSType.MACOS_ARM64
else:
return OSType.MACOS_X64
if LibLoader.LINUX_OS in os_type:
if arch_type == ArchType.ARM64:
return OSType.LINUX_ARM64
else:
return OSType.LINUX_X64
if LibLoader.WINDOWS_OS in os_type:
return OSType.WINDOWS
raise Exception(f"Unsupported os {os_type}, arch: {arch_type}")

@staticmethod
def get_lib_name():
os_type = LibLoader.get_os_type()
if os_type == OSType.WINDOWS:
return "{}{}.{}".format(LibLoader.LIB_PREFIX, LibLoader.TRANSPORT_LIB_NAME, LibLoader.WINDOWS_LIB_POSTFIX)
if os_type == OSType.MACOS_ARM64:
return "{}{}-arm64.{}".format(LibLoader.LIB_PREFIX, LibLoader.TRANSPORT_LIB_NAME, LibLoader.MACOS_LIB_POSTFIX)
if os_type == OSType.MACOS_X64:
return "{}{}.{}".format(LibLoader.LIB_PREFIX, LibLoader.TRANSPORT_LIB_NAME, LibLoader.MACOS_LIB_POSTFIX)
if os_type == OSType.LINUX_ARM64:
return "{}{}.{}-arm64".format(LibLoader.LIB_PREFIX, LibLoader.TRANSPORT_LIB_NAME, LibLoader.LINUX_LIB_PORSTFIX)
if os_type == OSType.LINUX_X64:
return "{}{}.{}".format(LibLoader.LIB_PREFIX, LibLoader.TRANSPORT_LIB_NAME, LibLoader.LINUX_LIB_PORSTFIX)
raise Exception(
f"get_lib_name failed for not support the os_type: {os_type}")

@staticmethod
def load_lib():
lib_path = os.path.join(
LibLoader.TRANSPORT_LIB_PATH, LibLoader.get_lib_name())
print(f"#### lib_path: {lib_path}")
lib = ctypes.cdll.LoadLibrary(lib_path)
return lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
import unittest


class TestLibLoader(unittest.TestCase):
def test_load_lib(self):
from utils.lib_loader import LibLoader
_wedpr_python_transport = LibLoader.load_lib()


if __name__ == '__main__':
unittest.main()
35 changes: 0 additions & 35 deletions cpp/wedpr-transport/sdk-wrapper/python/setup.py.in

This file was deleted.

Loading

0 comments on commit c22435b

Please sign in to comment.