Skip to content

Commit

Permalink
cli: fix sys.path for cxfreeze command line (#2439)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte committed Jun 5, 2024
1 parent 9de2ded commit 406dc8b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 17 deletions.
11 changes: 10 additions & 1 deletion cx_Freeze/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from __future__ import annotations

import argparse
import os
import sys
from pathlib import Path

from cx_Freeze import __version__, setup
from cx_Freeze._pyproject import get_pyproject_tool_data
Expand Down Expand Up @@ -220,9 +222,16 @@ def main() -> None:
if script_args[0] == "build" and "build_exe" not in script_args:
script_args.insert(1, "build_exe")

# fix sys.path for cxfreeze command line
command = Path(sys.argv[0])
if command.stem == "cxfreeze":
path_to_remove = os.fspath(command.parent)
if path_to_remove in sys.path:
sys.path.remove(path_to_remove)
sys.path.insert(0, os.getcwd())

# get options from pyproject.toml
options = get_pyproject_tool_data()

executables.extend(options.pop("executables", []))

setup(
Expand Down
118 changes: 102 additions & 16 deletions tests/test_executables.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
TOP_DIR = Path(__file__).resolve().parent.parent

SOURCE_SETUP_TOML = """
test_simple1.py
test_1.py
print("Hello from cx_Freeze")
pyproject.toml
[project]
Expand All @@ -31,11 +31,11 @@
description = "Sample cx_Freeze script"
[[tool.cxfreeze.executables]]
script = "test_simple1.py"
script = "test_1.py"
[[tool.cxfreeze.executables]]
script = "test_simple1.py"
target_name = "test_simple2"
script = "test_1.py"
target_name = "test_2"
[tool.cxfreeze.build_exe]
excludes = ["tkinter", "unittest"]
Expand All @@ -45,15 +45,15 @@
"""

SOURCE_SETUP_PY = """
test_simple1.py
test_1.py
print("Hello from cx_Freeze")
setup.py
from cx_Freeze import Executable, setup
executables = [
"test_simple1.py",
{"script": "test_simple1.py", "target_name": "test_simple2"},
Executable("test_simple1.py", target_name="test_simple3"),
"test_1.py",
{"script": "test_1.py", "target_name": "test_2"},
Executable("test_1.py", target_name="test_3"),
]
setup(
Expand All @@ -67,7 +67,7 @@
"""

SOURCE_SETUP_CFG = """
test_simple1.py
test_1.py
print("Hello from cx_Freeze")
setup.cfg
[metadata]
Expand All @@ -79,11 +79,11 @@
excludes = tkinter,unittest
silent = true
command
cxfreeze test_simple1.py
cxfreeze test_1.py
"""

SOURCE_SETUP_MIX = """
test_simple1.py
test_1.py
print("Hello from cx_Freeze")
pyproject.toml
[project]
Expand All @@ -92,20 +92,97 @@
description = "Sample cx_Freeze script"
[[tool.cxfreeze.executables]]
script = "test_simple1.py"
target_name = "test_simple2"
script = "test_1.py"
target_name = "test_2"
[tool.cxfreeze.build_exe]
excludes = ["tkinter", "unittest"]
silent = true
setup.py
from cx_Freeze import setup
setup(executables=["test_simple1.py"])
setup(executables=["test_1.py"])
command
python setup.py build
"""

SOURCE_ADV_SETUP_TOML = """
test_1.py
from modules.testfreeze_1 import func1
print("Hello from cx_Freeze #1")
func1()
test_2.py
from modules.testfreeze_2 import func2
print("Hello from cx_Freeze #2")
func2()
test_3.py
def say_hello():
print("Hello from cx_Freeze #3")
if __name__ == "__main__":
say_hello()
modules/testfreeze_1.py
def func1():
print("Test freeze module #1")
modules/testfreeze_2.py
def func2():
print("Test freeze module #2")
pyproject.toml
[project]
name = "advanced"
version = "0.1.2.3"
description = "Sample cx_Freeze script"
[[tool.cxfreeze.executables]]
script = "test_1.py"
[[tool.cxfreeze.executables]]
script = "test_2.py"
[[tool.cxfreeze.executables]]
script = "test_3.py"
[tool.cxfreeze.build_exe]
excludes = ["tkinter", "unittest"]
silent = true
command
cxfreeze build
"""

SOURCE_ADV_SETUP_PY = """
test_1.py
from modules.testfreeze_1 import func1
print("Hello from cx_Freeze #1")
func1()
test_2.py
from modules.testfreeze_2 import func2
print("Hello from cx_Freeze #2")
func2()
test_3.py
def say_hello():
print("Hello from cx_Freeze #3")
if __name__ == "__main__":
say_hello()
modules/testfreeze_1.py
def func1():
print("Test freeze module #1")
modules/testfreeze_2.py
def func2():
print("Test freeze module #2")
setup.py
from cx_Freeze import Executable, setup
executables = ["test_1.py", "test_2.py", "test_3.py"]
setup(
name="advanced",
version="0.1.2.3",
description="Sample cx_Freeze script",
executables=executables,
)
command
python setup.py build_exe --excludes=tkinter,unittest --silent
"""


@pytest.mark.parametrize(
("source", "number_of_executables"),
Expand All @@ -114,8 +191,17 @@
(SOURCE_SETUP_PY, 3),
(SOURCE_SETUP_CFG, 1),
(SOURCE_SETUP_MIX, 2),
(SOURCE_ADV_SETUP_TOML, 3),
(SOURCE_ADV_SETUP_PY, 3),
],
ids=[
"setup_toml",
"setup_py",
"setup_cfg",
"setup_mix",
"setup_adv_toml",
"setup_adv_py",
],
ids=["setup_toml", "setup_py", "setup_cfg", "setup_mix"],
)
def test_executables(
tmp_path: Path, source: str, number_of_executables: int
Expand All @@ -125,7 +211,7 @@ def test_executables(
output = run_command(tmp_path)

for i in range(1, number_of_executables):
file_created = tmp_path / BUILD_EXE_DIR / f"test_simple{i}{SUFFIX}"
file_created = tmp_path / BUILD_EXE_DIR / f"test_{i}{SUFFIX}"
assert file_created.is_file(), f"file not found: {file_created}"

output = run_command(tmp_path, file_created, timeout=10)
Expand Down

0 comments on commit 406dc8b

Please sign in to comment.