def __init__(self, executable: str, path: str) -> None: self.path = path paths = get_sys_config_paths(executable, vars={ "base": path, "platbase": path }) self.bin_dir = paths["scripts"] self.lib_dirs = [paths["platlib"], paths["purelib"]] self.site_dir = os.path.join(path, "site") if not os.path.isdir(self.site_dir): os.makedirs(self.site_dir) with open(os.path.join(self.site_dir, "sitecustomize.py"), "w") as fp: fp.write( textwrap.dedent(f""" import sys, os, site original_sys_path = sys.path[:] known_paths = set() site.addusersitepackages(known_paths) site.addsitepackages(known_paths) known_paths = {{os.path.normcase(p) for p in known_paths}} original_sys_path = [ p for p in original_sys_path if os.path.normcase(p) not in known_paths] sys.path[:] = original_sys_path for lib_path in {self.lib_dirs!r}: site.addsitedir(lib_path) """))
def get_paths(self) -> dict[str, str]: is_venv = is_venv_python(self.interpreter.executable) paths = get_sys_config_paths( str(self.interpreter.executable), kind="user" if not is_venv and self.project.global_config["global_project.user_site"] else "default", ) if is_venv: python_xy = f"python{self.interpreter.identifier}" paths["include"] = os.path.join(paths["data"], "include", "site", python_xy) paths["prefix"] = paths["data"] paths["headers"] = paths["include"] return paths
def __enter__(self): self._path = tempfile.mkdtemp(prefix="pdm-build-env-") paths = get_sys_config_paths( self.executable, vars={"base": self._path, "platbase": self._path} ) old_path = os.getenv("PATH") self._saved_env = { "PYTHONPATH": paths["purelib"], "PATH": paths["scripts"] if not old_path else os.pathsep.join([paths["scripts"], old_path]), "PYTHONNOUSERSITE": "1", "PDM_PYTHON_PEP582": "0", } stream.logger.debug("Preparing isolated env for PEP 517 build...") return self
def __init__(self, executable: str, shared: str, overlay: str) -> None: self.bin_dirs: list[str] = [] self.lib_dirs: list[str] = [] for path in (overlay, shared): paths = get_sys_config_paths(executable, vars={ "base": path, "platbase": path }, kind="prefix") self.bin_dirs.append(paths["scripts"]) self.lib_dirs.extend([paths["platlib"], paths["purelib"]]) self.site_dir = os.path.join(overlay, "site") if os.path.isdir(self.site_dir): # Clear existing site dir as .pyc may be cached. shutil.rmtree(self.site_dir) os.makedirs(self.site_dir) with open(os.path.join(self.site_dir, "sitecustomize.py"), "w") as fp: fp.write( textwrap.dedent(f""" import sys, os, site original_sys_path = sys.path[:] known_paths = set() site.addusersitepackages(known_paths) site.addsitepackages(known_paths) known_paths = {{os.path.normcase(p) for p in known_paths}} original_sys_path = [ p for p in original_sys_path if os.path.normcase(p) not in known_paths] sys.path[:] = original_sys_path for lib_path in {self.lib_dirs!r}: site.addsitedir(lib_path) """)) self.shared = shared self.overlay = overlay
def get_paths(self) -> Dict[str, str]: paths = get_sys_config_paths(self.python_executable) paths["prefix"] = paths["data"] paths["headers"] = paths["include"] return paths