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

module: improve version method and add new methods #2403

Merged
merged 1 commit into from
May 18, 2024
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
7 changes: 2 additions & 5 deletions cx_Freeze/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ def load_bcrypt(finder: ModuleFinder, module: Module) -> None:
(loaded implicitly).
"""
include_cffi = True
if (
module.distribution
and int(module.distribution.version.split(".")[0]) >= 4
):
if module.distribution and module.distribution.version[0] >= 4:
include_cffi = False
if include_cffi:
finder.include_module("_cffi_backend")
Expand Down Expand Up @@ -309,7 +306,7 @@ def load_jpype(finder: ModuleFinder, module: Module) -> None:

def load_lazy_loader(finder: ModuleFinder, module: Module) -> None:
"""Use load_lazy_loader 0.2+ to work with .pyc files."""
if module.distribution.version < "0.2":
if module.distribution.version < (0, 2):
msg = "Please upgrade 'lazy_loader>=0.2' to support cx_Freeze"
raise SystemExit(msg)

Expand Down
3 changes: 1 addition & 2 deletions cx_Freeze/hooks/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def load_numpy__distributor_init(finder: ModuleFinder, module: Module) -> None:
module_dir = module.file.parent
exclude_dependent_files = False
if (IS_MACOS or IS_WINDOWS) and not IS_CONDA:
version = float(module.parent.distribution.version.rpartition(".")[0])
if version >= 1.26:
if module.parent.distribution.version >= (1, 26):
return # for numpy >= 1.26.0 is handled in top module
# numpy < 1.26.0 pypi
libs_dir = module_dir.joinpath(".dylibs" if IS_MACOS else ".libs")
Expand Down
61 changes: 47 additions & 14 deletions cx_Freeze/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_MINGW, IS_WINDOWS
from cx_Freeze._importlib import metadata
from cx_Freeze.exception import ModuleError, OptionError

Expand Down Expand Up @@ -56,9 +57,12 @@ def __init__(self, cache_path: Path, name: str) -> None:
if source_path is None or not source_path.exists():
raise ModuleError(name)

target_name = f"{normalized_name}-{distribution.version}.dist-info"
target_path = cache_path / target_name
dist_name = f"{normalized_name}-{distribution.version}.dist-info"
target_path = cache_path / dist_name
super().__init__(target_path)
self.original = distribution
self.normalized_name = normalized_name
self.distinfo_name = dist_name
if target_path.exists(): # cached
return
target_path.mkdir(parents=True)
Expand Down Expand Up @@ -88,15 +92,14 @@ def __init__(self, cache_path: Path, name: str) -> None:
target.write_bytes(source.read_bytes())
purelib = not source_path.joinpath("not-zip-safe").is_file()

self._write_wheel_distinfo(target_path, purelib)
self._write_record_distinfo(target_path)
self._write_wheel_distinfo(purelib)
self._write_record_distinfo()

@staticmethod
def _write_wheel_distinfo(target_path: Path, purelib: bool) -> None:
def _write_wheel_distinfo(self, purelib: bool) -> None:
"""Create a WHEEL file if it doesn't exist."""
target = target_path / "WHEEL"
target = self.locate_file(f"{self.distinfo_name}/WHEEL")
if not target.exists():
project = Path(__file__).parent.name
project = Path(__file__).parent.name # cx_Freeze
version = metadata.version(project)
root_is_purelib = "true" if purelib else "false"
text = [
Expand All @@ -108,22 +111,52 @@ def _write_wheel_distinfo(target_path: Path, purelib: bool) -> None:
with target.open(mode="w", encoding="utf_8", newline="") as file:
file.write("\n".join(text))

@staticmethod
def _write_record_distinfo(target_path: Path) -> None:
def _write_record_distinfo(self) -> None:
"""Recreate a minimal RECORD file."""
target_name = target_path.name
distinfo_name = self.distinfo_name
target = self.locate_file(f"{distinfo_name}/RECORD")
target_dir = target.parent
record = [
f"{target_name}/{file.name},," for file in target_path.iterdir()
f"{distinfo_name}/{file.name},," for file in target_dir.iterdir()
]
record.append(f"{target_name}/RECORD,,")
target = target_path / "RECORD"
record.append(f"{distinfo_name}/RECORD,,")
with target.open(mode="w", encoding="utf_8", newline="") as file:
file.write("\n".join(record))

@property
def binary_files(self) -> list[str]:
"""Return the binary files included in the package."""
if IS_MINGW or IS_WINDOWS:
return [
file
for file in self.original.files
if file.name.lower().endswith("*.dll")
]
extensions = tuple([ext for ext in EXTENSION_SUFFIXES if ext != ".so"])
return [
file
for file in self.original.files
if file.match("*.so*") and not file.name.endswith(extensions)
]

@property
def installer(self) -> str:
"""Return the installer (pip, conda) for the distribution package."""
return self.read_text("INSTALLER") or "pip"

@property
def requires(self) -> list[str]:
"""Generated requirements specified for this Distribution."""
return super().requires or []

@property
def version(self) -> tuple[int] | str:
"""Return the 'Version' metadata for the distribution package."""
_version = super().version
with suppress(ValueError):
_version = tuple(map(int, _version.split(".")))
return _version


class Module:
"""The Module class."""
Expand Down