From bfd6e872f2d227260b5ea28368227da3569f78e3 Mon Sep 17 00:00:00 2001 From: Pedro Bianchini de Quadros Date: Sun, 5 Jan 2025 21:18:57 -0300 Subject: [PATCH] fix(Build-system): Add __init__.py copy .so files to right folder --- Examples/graph_example.py | 2 +- MANIFEST.in | 2 ++ __init__.py | 0 build.py | 31 +++++++++++++++++++++++++++---- pyproject.toml | 3 ++- search_engine_cpp/__init__.py | 21 ++++++++++++++++++--- search_engine_cpp/crawler.py | 2 +- 7 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 MANIFEST.in delete mode 100644 __init__.py diff --git a/Examples/graph_example.py b/Examples/graph_example.py index 15b999c..c51141a 100644 --- a/Examples/graph_example.py +++ b/Examples/graph_example.py @@ -1,6 +1,6 @@ # EXEMPLO USANDO A BIBLIOTECA SEARCH_ENGINE -from search_engine.crawler import Crawler +from search_engine_cpp.crawler import Crawler # Rodar com o test_mode ativado ajuda a testar a lib e fazer com que o parametro limit seja usado diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..510d027 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include search_engine_cpp/lib/*.so +recursive-include search_engine_cpp/lib *.so diff --git a/__init__.py b/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/build.py b/build.py index db28b79..490717e 100644 --- a/build.py +++ b/build.py @@ -16,6 +16,7 @@ def build(setup_kwargs): import subprocess import glob import shutil + import pathlib BUILD_FOLDER = "build" @@ -29,10 +30,25 @@ def build_lib_cpp(path: str): subprocess.run(["make"]) os.chdir(old_path) +def copy_lib(root_path, path_lib): + extensions = [".so*", ".dll*", ".dylib*"] + for ext in extensions: + path_so = root_path.glob(f"{BUILD_FOLDER}/lib/*{ext}") + path_lib.mkdir(exist_ok=True, parents=True) + # Copy each .so file + for lib in path_so: + shutil.copy(lib, path_lib) + print(f"\n\n\n{path_lib}\n\n\n") + +# setup_kwargs def build(setup_kwargs): # Monta a biblioteca c++ para ser usada no Python build_lib_cpp(BUILD_FOLDER) - library_dir = os.path.abspath("build/lib") + root_path = pathlib.Path(__file__).parent + path_lib = pathlib.Path(__file__).parent / "search_engine_cpp/lib" + copy_lib(root_path, path_lib) + library_dir = os.path.abspath("search_engine_cpp/lib") + # Modulos que serão usados no Python libs_names = [ @@ -43,10 +59,9 @@ def build(setup_kwargs): extensions = [] - for lib in libs_names: ext = Extension( - f"{lib}", + f"search_engine_cpp.{lib}", language="c++", sources=[ f"lib/src/{lib}.pyx" @@ -60,4 +75,12 @@ def build(setup_kwargs): extensions.append(ext) # Monta o setup usando o extensions - setup_kwargs["ext_modules"] = cythonize(extensions, compiler_directives={"language_level": "3str"}, force=True) \ No newline at end of file + # setup_kwargs["cmdclass"] = {"build_ext": build_ext} + setup_kwargs["script_args"] = ["build_ext"] + # Inclua os arquivos .so na instalação + setup_kwargs["package_data"] = { + "search_engine_cpp": ["lib/*.so"] + } + setup_kwargs["include_package_data"] = True + + setup_kwargs["ext_modules"] = cythonize(extensions, compiler_directives={"language_level": "3str"}, force=True) diff --git a/pyproject.toml b/pyproject.toml index e6dad02..746d3db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,8 @@ authors = ["Pedro Bianchini de Quadros "] readme = "README.md" include = [ "lib/**", # Inclui o diretório lib com todos os arquivos - "tests/**", + "search_engine_cpp/lib/*.so", + "search_engine_cpp/.*so", "CMakeLists.txt", "README.md", # Inclui o README "LICENSE.md" # Inclui o arquivo de licença diff --git a/search_engine_cpp/__init__.py b/search_engine_cpp/__init__.py index 3cc9caa..237c83c 100644 --- a/search_engine_cpp/__init__.py +++ b/search_engine_cpp/__init__.py @@ -1,3 +1,18 @@ -# __all__ [ -# 'crawler', -# ] \ No newline at end of file +import ctypes +import os + +# Obtenha o caminho absoluto do .so +lib_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "lib", "libsearch_engine.so")) + +# Carregue o .so com ctypes +try: + ctypes.CDLL(lib_path) +except OSError as e: + raise ImportError(f"Erro ao carregar a biblioteca compartilhada '{lib_path}': {e}") + +# Agora, importe os submódulos normalmente +from . import _hello +from . import _page_rank +from . import _inverted_index + +__all__ = ["_hello", "_page_rank", "_inverted_index"] diff --git a/search_engine_cpp/crawler.py b/search_engine_cpp/crawler.py index c964b97..ef13c37 100644 --- a/search_engine_cpp/crawler.py +++ b/search_engine_cpp/crawler.py @@ -1,5 +1,5 @@ from .exceptions import UrlError -from _page_rank import PyGraph +from ._page_rank import PyGraph from .helper.converter import StringToIntConverter # Libs