From 5190a1ac661bc4ae04b338fea02daba7d3af9218 Mon Sep 17 00:00:00 2001 From: Marcelo Duarte Date: Fri, 7 Jul 2023 18:07:48 -0300 Subject: [PATCH] hooks: move sklearn hook to a submodule --- cx_Freeze/hooks/__init__.py | 16 ---------------- cx_Freeze/hooks/sklearn.py | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 cx_Freeze/hooks/sklearn.py diff --git a/cx_Freeze/hooks/__init__.py b/cx_Freeze/hooks/__init__.py index 3883bef08..1a0ae28f2 100644 --- a/cx_Freeze/hooks/__init__.py +++ b/cx_Freeze/hooks/__init__.py @@ -466,22 +466,6 @@ def load_sentry(finder: ModuleFinder, module: Module) -> None: finder.include_module("sentry_sdk.integrations.threading") -def load_sklearn__distributor_init( - finder: ModuleFinder, module: Module -) -> None: - """On Windows the sklearn/.libs directory is not copied.""" - source_dir = module.parent.path[0] / ".libs" - if source_dir.exists(): - # msvcp140 and vcomp140 dlls should be copied - finder.include_files(source_dir, "lib") - # patch the code to search the correct directory - code_string = module.file.read_text(encoding="utf-8") - code_string = code_string.replace( - "libs_path =", "libs_path = __import__('sys').frozen_dir #" - ) - module.code = compile(code_string, os.fspath(module.file), "exec") - - def load_setuptools(finder: ModuleFinder, module: Module) -> None: """The setuptools must be loaded as a package, to prevent it to break in the future. diff --git a/cx_Freeze/hooks/sklearn.py b/cx_Freeze/hooks/sklearn.py new file mode 100644 index 000000000..f15391db7 --- /dev/null +++ b/cx_Freeze/hooks/sklearn.py @@ -0,0 +1,26 @@ +"""A collection of functions which are triggered automatically by finder when +scipy package is included. +""" + +from __future__ import annotations + +import os + +from ..finder import ModuleFinder +from ..module import Module + + +def load_sklearn__distributor_init( + finder: ModuleFinder, module: Module +) -> None: + """Fix the location of dependent files in Windows.""" + source_dir = module.parent.path[0] / ".libs" + if source_dir.exists(): + # msvcp140 and vcomp140 dlls should be copied + finder.include_files(source_dir, "lib") + # patch the code to search the correct directory + code_string = module.file.read_text(encoding="utf-8") + code_string = code_string.replace( + "libs_path =", "libs_path = __import__('sys').frozen_dir #" + ) + module.code = compile(code_string, os.fspath(module.file), "exec")