Skip to content

Commit 593c089

Browse files
committed
Dirty hack around pip fake-venv
Signed-off-by: Cristian Le <git@lecris.dev>
1 parent 9a11346 commit 593c089

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/scikit_build_core/repair_wheel/__init__.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,43 @@
3131
"WindowsWheelRepairer",
3232
]
3333

34+
DIR = Path(__file__).parent.resolve()
35+
36+
37+
@functools.lru_cache(1)
38+
def _get_buildenv_platlib() -> str:
39+
# Normally we could `sysconfig.get_path("platlib")` directly, but pip fake-venv breaks it
40+
platlib_path = sysconfig.get_path("platlib")
41+
purelib_path = sysconfig.get_path("purelib")
42+
real_purelib_path = DIR.parent.parent
43+
if real_purelib_path.samefile(purelib_path):
44+
# Here is the normal state if we are in a real venv
45+
return str(Path(platlib_path).resolve())
46+
# Otherwise we need to trick it to giving us the real path
47+
data_path = sysconfig.get_path("data")
48+
platlib_relative_path = Path(platlib_path).relative_to(data_path)
49+
purelib_relative_path = Path(purelib_path).relative_to(data_path)
50+
51+
# removesuffix(purelib_relative_path)
52+
if str(real_purelib_path).rfind(str(purelib_relative_path)) == -1:
53+
logger.warning(
54+
"Could not figure out the true build-env path:\n"
55+
"sysconfig_purelib = {sysconfig_purelib}\n"
56+
"scikit-build-core_purelib = {real_purelib}\n",
57+
sysconfig_purelib=purelib_path,
58+
real_purelib=real_purelib_path,
59+
)
60+
return platlib_path
61+
real_root = str(real_purelib_path)[: -len(str(purelib_relative_path))]
62+
real_platlib_path = str(Path(real_root) / platlib_relative_path)
63+
# Yet another dirty trick necessary
64+
real_platlib_path = real_platlib_path.replace(
65+
os.path.normpath("/overlay/"),
66+
os.path.normpath("/normal/"),
67+
)
68+
logger.debug("Calculated real_platlib_path = {}", real_platlib_path)
69+
return str(real_platlib_path)
70+
3471

3572
@dataclasses.dataclass()
3673
class WheelRepairer(ABC):
@@ -103,7 +140,7 @@ def path_relative_site_packages(
103140
path.relative_to(self.wheel_dirs["platlib"])
104141
except ValueError:
105142
# Otherwise check if the path is relative to build environment
106-
path = path.relative_to(sysconfig.get_path("platlib"))
143+
path = path.relative_to(_get_buildenv_platlib())
107144
# Mock the path to be in the wheel install platlib
108145
path = self.wheel_dirs["platlib"] / path
109146
return Path(os.path.relpath(path, relative_to))

0 commit comments

Comments
 (0)