via_template.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from __future__ import annotations
  2. import os
  3. import shlex
  4. import sys
  5. from abc import ABC, abstractmethod
  6. from typing import TYPE_CHECKING
  7. from .activator import Activator
  8. if TYPE_CHECKING:
  9. from collections.abc import Iterable, Iterator
  10. from pathlib import Path
  11. from virtualenv.create.creator import Creator
  12. if sys.version_info >= (3, 10):
  13. from importlib.resources import files
  14. def read_binary(module_name: str, filename: str) -> bytes:
  15. return (files(module_name) / filename).read_bytes()
  16. else:
  17. from importlib.resources import read_binary
  18. class ViaTemplateActivator(Activator, ABC):
  19. @abstractmethod
  20. def templates(self) -> Iterator[str]:
  21. raise NotImplementedError
  22. @staticmethod
  23. def quote(string: str) -> str:
  24. """Quote strings in the activation script.
  25. :param string: the string to quote
  26. :returns: quoted string that works in the activation script
  27. """
  28. return shlex.quote(string)
  29. def generate(self, creator: Creator) -> list[Path]:
  30. dest_folder = creator.bin_dir
  31. replacements = self.replacements(creator, dest_folder)
  32. generated = self._generate(replacements, self.templates(), dest_folder, creator)
  33. if self.flag_prompt is not None:
  34. creator.pyenv_cfg["prompt"] = self.flag_prompt
  35. return generated
  36. def replacements(self, creator: Creator, dest_folder: Path) -> dict[str, str]: # noqa: ARG002
  37. return {
  38. "__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
  39. "__VIRTUAL_ENV__": str(creator.dest),
  40. "__VIRTUAL_NAME__": creator.env_name,
  41. "__BIN_NAME__": str(creator.bin_dir.relative_to(creator.dest)),
  42. "__PATH_SEP__": os.pathsep,
  43. "__TCL_LIBRARY__": getattr(creator.interpreter, "tcl_lib", None) or "",
  44. "__TK_LIBRARY__": getattr(creator.interpreter, "tk_lib", None) or "",
  45. }
  46. def _generate(
  47. self, replacements: dict[str, str], templates: Iterable[str], to_folder: Path, creator: Creator
  48. ) -> list[Path]:
  49. generated = []
  50. for template in templates:
  51. text = self.instantiate_template(replacements, template, creator)
  52. dest = to_folder / self.as_name(template)
  53. # remove the file if it already exists - this prevents permission
  54. # errors when the dest is not writable
  55. if dest.exists():
  56. dest.unlink()
  57. # Powershell assumes Windows 1252 encoding when reading files without BOM
  58. encoding = "utf-8-sig" if str(template).endswith(".ps1") else "utf-8"
  59. # use write_bytes to avoid platform specific line normalization (\n -> \r\n)
  60. dest.write_bytes(text.encode(encoding))
  61. generated.append(dest)
  62. return generated
  63. def as_name(self, template: str) -> str:
  64. return template
  65. def instantiate_template(self, replacements: dict[str, str], template: str, creator: Creator) -> str:
  66. # read content as binary to avoid platform specific line normalization (\n -> \r\n)
  67. binary = read_binary(self.__module__, template)
  68. text = binary.decode("utf-8", errors="strict")
  69. for key, value in replacements.items():
  70. value_uni = self._repr_unicode(creator, value)
  71. text = text.replace(key, self.quote(value_uni))
  72. return text
  73. @staticmethod
  74. def _repr_unicode(creator: Creator, value: str) -> str: # noqa: ARG004
  75. return value # by default, we just let it be unicode
  76. __all__ = [
  77. "ViaTemplateActivator",
  78. ]