-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7a5d47
commit 7911280
Showing
7 changed files
with
160 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
import re | ||
import argparse | ||
import subprocess | ||
|
||
def find_libraries(library_stems, search_paths, extension): | ||
library_paths = [] | ||
for library_stem in library_stems: | ||
library = [] | ||
for search_path in search_paths: | ||
try: | ||
files = os.listdir(search_path) | ||
pattern = r"^(?:lib)?{}(?:-(?:\d+\.)*|\d+\.|\.){}$".format(library_stem, extension) | ||
library.extend([ | ||
{"filename": filename, "base_path": search_path} for filename in files | ||
if re.search(pattern, filename) | ||
]) | ||
except FileNotFoundError: | ||
print(f"Warning: The path {search_path} does not exist.") | ||
continue | ||
|
||
if not library: | ||
raise AssertionError(f"No libraries found in {search_paths} which match \"{library_stem}\" stem") | ||
if len(library) != 1: | ||
raise AssertionError(f"Multiple libraries found which match \"{library_stem}\" stem:\n{library}") | ||
library_paths.append(os.path.join(library[0]["base_path"], library[0]["filename"])) | ||
|
||
return library_paths | ||
|
||
def find_dll_dependencies_recursively(dll_path, search_paths): | ||
try: | ||
dumpbin = subprocess.run(['dumpbin.exe', '/dependents', dll_path], capture_output=True, text=True) | ||
dependency_names = re.findall(r'^\s{4}(\S*\.dll)$', dumpbin.stdout, re.MULTILINE) | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error running dumpbin: {e}") | ||
return [] | ||
|
||
dependencies = [] | ||
for dependency_name in dependency_names: | ||
for search_path in search_paths: | ||
dependency_path = os.path.join(search_path, dependency_name) | ||
if os.path.isfile(dependency_path): | ||
dependencies.append(dependency_path) | ||
found = True | ||
break | ||
|
||
dependencies_recursive = [] | ||
for dependency in dependencies: | ||
dependencies_recursive.extend(find_dll_dependencies_recursively(dependency, search_paths)) | ||
|
||
return list(set(dependencies + dependencies_recursive)) | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Find library files and their dependencies.") | ||
parser.add_argument('-l', '--libraries', nargs='+', required=True, help='Library base names to search for (e.g., tesseract)') | ||
parser.add_argument('-s', '--search-paths', nargs='+', required=True, help='Directories to search within') | ||
parser.add_argument('-e', '--extension', default='dll', help='File extension to match (default: dll)') | ||
args = parser.parse_args() | ||
|
||
try: | ||
runtime_library_paths = find_libraries(args.libraries, args.search_paths, args.extension) | ||
runtime_library_paths.extend(find_dll_dependencies_recursively(runtime_library_paths[0], args.search_paths)) | ||
print(";".join(runtime_library_paths)) | ||
except AssertionError as e: | ||
print(f"Error: {e}") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
diff --git a/setup.py b/setup.py | ||
index f7a1c46..2945e59 100644 | ||
--- a/setup.py | ||
+++ b/setup.py | ||
@@ -7,6 +7,7 @@ import os | ||
import re | ||
import subprocess | ||
import sys | ||
+import shutil | ||
from os.path import abspath, dirname | ||
from os.path import join as pjoin | ||
from os.path import split as psplit | ||
@@ -24,6 +25,7 @@ _LOGGER.addHandler(logging.StreamHandler(sys.stderr)) | ||
|
||
_TESSERACT_MIN_VERSION = "3.04.00" | ||
_CYTHON_COMPILE_TIME_ENV = None | ||
+_DLL_PATHS = [] | ||
|
||
# find_version from pip https://github.com/pypa/pip/blob/1.5.6/setup.py#L33 | ||
here = abspath(dirname(__file__)) | ||
@@ -192,6 +194,12 @@ def get_tesseract_version(): | ||
"TESSERACT_VERSION": version_to_int(version), | ||
} | ||
if sys.platform == "win32": | ||
+ dllpaths = os.getenv("DLLPATHS", None) | ||
+ if dllpaths: | ||
+ dllpaths = list(filter(None, dllpaths.split(";"))) | ||
+ else: | ||
+ dllpaths = [] | ||
+ | ||
libpaths = os.getenv("LIBPATH", None) | ||
if libpaths: | ||
libpaths = list(filter(None, libpaths.split(";"))) | ||
@@ -225,6 +233,7 @@ def get_tesseract_version(): | ||
else: | ||
includepaths = [] | ||
|
||
+ config["dll_paths"] = dllpaths | ||
config["libraries"] = [tess_lib, lept_lib] | ||
config["library_dirs"] = libpaths | ||
config["include_dirs"] = includepaths | ||
@@ -253,6 +262,7 @@ def make_extension(): | ||
global _CYTHON_COMPILE_TIME_ENV | ||
build_args = get_build_args() | ||
_CYTHON_COMPILE_TIME_ENV = build_args.pop("compile_time_env") | ||
+ _DLL_PATHS.extend(build_args.pop("dll_paths")) | ||
return Extension("tesserocr.tesserocr", sources=["tesserocr/tesserocr.pyx"], language="c++", **build_args) | ||
|
||
|
||
@@ -275,6 +285,11 @@ class my_build_ext(build_ext, object): | ||
_LOGGER.debug("tesseract >= 03.05.02 requires c++11 compiler support") | ||
extension.extra_compile_args = extra_args | ||
|
||
+ # copy dlls to the build directory | ||
+ dll_dest_dir = os.path.dirname(self.get_ext_fullpath(extension.name)) | ||
+ for dll_path in _DLL_PATHS: | ||
+ shutil.copy(dll_path, dll_dest_dir) | ||
+ | ||
build_ext.build_extensions(self) | ||
|
||
def finalize_options(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.