via_template.py 3.1 KB

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