| Server IP : 198.38.94.67 / Your IP : 216.73.217.178 Web Server : LiteSpeed System : Linux d6054.dxb1.stableserver.net 5.14.0-570.25.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jul 9 04:57:09 EDT 2025 x86_64 User : azfilmst ( 1070) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /opt/saltstack/salt/lib/python3.10/site-packages/virtualenv/create/ |
Upload File : |
from __future__ import annotations
import logging
import os
from collections import OrderedDict
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
LOGGER = logging.getLogger(__name__)
class PyEnvCfg:
def __init__(self, content: OrderedDict[str, str], path: Path) -> None:
self.content = content
self.path = path
@classmethod
def from_folder(cls, folder: Path) -> PyEnvCfg:
return cls.from_file(folder / "pyvenv.cfg")
@classmethod
def from_file(cls, path: Path) -> PyEnvCfg:
content = cls._read_values(path) if path.exists() else OrderedDict()
return PyEnvCfg(content, path)
@staticmethod
def _read_values(path: Path) -> OrderedDict[str, str]:
content = OrderedDict()
for line in path.read_text(encoding="utf-8").splitlines():
equals_at = line.index("=")
key = line[:equals_at].strip()
value = line[equals_at + 1 :].strip()
if len(value) > 1 and value[0] in {"'", '"'} and value[0] == value[-1]:
value = value[1:-1]
content[key] = value
return content
def write(self) -> None:
LOGGER.debug("write %s", self.path)
text = ""
for key, value in self.content.items():
# Use abspath to normalize relative paths but preserve symlinks (match venv behavior)
# See issue #2770 - realpath resolves symlinks which breaks prefix symlinks
if key == "prompt" and value:
normalized_value = f'"{value}"'
else:
normalized_value = os.path.abspath(value) if value and os.path.exists(value) else value
line = f"{key} = {normalized_value}"
LOGGER.debug("\t%s", line)
text += line
text += "\n"
self.path.write_text(text, encoding="utf-8")
def refresh(self) -> OrderedDict[str, str]:
self.content = self._read_values(self.path)
return self.content
def __setitem__(self, key: str, value: str) -> None:
self.content[key] = value
def __getitem__(self, key: str) -> str:
return self.content[key]
def __contains__(self, item: str) -> bool:
return item in self.content
def update(self, other: dict[str, str]) -> PyEnvCfg:
self.content.update(other)
return self
def __repr__(self) -> str:
return f"{self.__class__.__name__}(path={self.path})"
__all__ = [
"PyEnvCfg",
]