Skip to content

Commit

Permalink
Define a fake Python binary in fake Conda env
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Feb 3, 2025
1 parent 0a1e1c0 commit 30d7dcf
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions conda_lock/conda_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,41 @@ def fake_conda_environment(

with open(conda_meta / (truncated_path.name + ".json"), "w") as f:
json.dump(entry, f, indent=2)
make_fake_python_binary(prefix)
yield prefix


FAKE_PYTHON_BINARY = """\
#!/usr/bin/env python
import sys
import shlex
cmd = shlex.join(sys.argv)
stderr_message = f'''\
This is a fake python binary generated by conda-lock.
It prevents libmamba from failing when it tries to check for installed pip packages.
This was called as:
{cmd}
'''
print(stderr_message, file=sys.stderr, flush=True, end='')
if "-m pip" not in cmd:
raise RuntimeError("Expected to invoke pip module with `-m pip`.")
"""


def make_fake_python_binary(prefix: str) -> None:
"""Create a fake python binary in the given prefix.
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)

0 comments on commit 30d7dcf

Please sign in to comment.