Skip to content

Commit

Permalink
Try to handle Windows in making the fake python
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Dec 15, 2024
1 parent 7d2005a commit 6b1a74f
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions conda_lock/conda_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time

from contextlib import contextmanager
from textwrap import dedent
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -584,37 +585,62 @@ def fake_conda_environment(
yield prefix


FAKE_PYTHON_BINARY = """\
#!/usr/bin/env python
import sys
import shlex
cmd = shlex.join(sys.argv)
def make_fake_python_binary(prefix: str) -> None:
"""Create a fake python binary in the given prefix.
stderr_message = f'''\
This is a fake python binary generated by conda-lock.
This is intended to prevent failure of `PrefixData.load_site_packages`
which was introduced in libmamba v2. That function invokes the command
`python -q -m pip inspect --local` to check for installed pip packages,
where the `python` binary is the one in the conda prefix. Our fake
prefix only contains the package metadata records, not the actual
packages or binaries. At this stage for conda-lock, we are only
interested in the conda packages, so we spoof the `python` binary
to return an empty stdout so that things proceed without error.
"""
# Write the fake Python script to a file
fake_python_script = pathlib.Path(prefix) / "fake_python_script.py"
fake_python_script.write_text(
dedent(
"""\
import sys
import shlex
It prevents libmamba from failing when it tries to check for installed pip packages.
cmd = shlex.join(sys.argv)
This was called as:
{cmd}
'''
stderr_message = f'''\
This is a fake python binary generated by conda-lock.
print(stderr_message, file=sys.stderr, flush=True, end='')
It prevents libmamba from failing when it tries to check for installed \
pip packages.
if "-m pip" not in cmd:
raise RuntimeError("Expected to invoke pip module with `-m pip`.")
"""
This was called as:
{cmd}
'''
print(stderr_message, file=sys.stderr, flush=True, end='')
def make_fake_python_binary(prefix: str) -> None:
"""Create a fake python binary in the given prefix.
if "-m pip" not in cmd:
raise RuntimeError("Expected to invoke pip module with `-m pip`.")
"""
)
)

This is intended to prevent failure of `PrefixData.load_site_packages`
which was introduced in libmamba v2.
"""
fake_python_binary = pathlib.Path(prefix) / "bin" / "python"
fake_python_binary.parent.mkdir(parents=True, exist_ok=True)
fake_python_binary.write_text(FAKE_PYTHON_BINARY)
fake_python_binary.chmod(0o755)
if sys.platform == "win32":
# On Windows, create a batch file that calls the script
fake_python_binary = pathlib.Path(prefix) / "Scripts" / "python.bat"
fake_python_binary.parent.mkdir(parents=True, exist_ok=True)
batch_content = dedent(f"""\
@echo off
"{sys.executable}" "{fake_python_script}" %*
""")
fake_python_binary.write_text(batch_content)
else:
# On Unix-like systems, create a shell script that calls the script
fake_python_binary = pathlib.Path(prefix) / "bin" / "python"
fake_python_binary.parent.mkdir(parents=True, exist_ok=True)
shell_script_content = dedent(f"""\
#!/usr/bin/env sh
"{sys.executable}" "{fake_python_script}" "$@"
""")
fake_python_binary.write_text(shell_script_content)
fake_python_binary.chmod(0o755)

0 comments on commit 6b1a74f

Please sign in to comment.