Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Build-system): Add __init__.py copy .so files to right folder #34

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/graph_example.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include search_engine_cpp/lib/*.so
recursive-include search_engine_cpp/lib *.so
Empty file removed __init__.py
Empty file.
31 changes: 27 additions & 4 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def build(setup_kwargs):
import subprocess
import glob
import shutil
import pathlib


BUILD_FOLDER = "build"
Expand All @@ -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 = [
Expand All @@ -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"
Expand All @@ -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)
# 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)
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ authors = ["Pedro Bianchini de Quadros <pedrobiqua@gmail.com>"]
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
Expand Down
21 changes: 18 additions & 3 deletions search_engine_cpp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# __all__ [
# 'crawler',
# ]
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"]
2 changes: 1 addition & 1 deletion search_engine_cpp/crawler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .exceptions import UrlError
from _page_rank import PyGraph
from ._page_rank import PyGraph
from .helper.converter import StringToIntConverter

# Libs
Expand Down
Loading