• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

localstack / localstack / 20129546909

11 Dec 2025 09:09AM UTC coverage: 86.867% (+0.001%) from 86.866%
20129546909

push

github

web-flow
Skip failing DNS unit test (#13501)

69915 of 80485 relevant lines covered (86.87%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/localstack-core/localstack/utils/venv.py
1
import io
1✔
2
import os
1✔
3
import sys
1✔
4
from functools import cached_property
1✔
5
from pathlib import Path
1✔
6
from typing import Union
1✔
7

8

9
class VirtualEnvironment:
1✔
10
    """
11
    Encapsulates methods to operate and navigate on a python virtual environment.
12
    """
13

14
    def __init__(self, venv_dir: Union[str, os.PathLike]):
1✔
15
        self._venv_dir = venv_dir
1✔
16

17
    def create(self) -> None:
1✔
18
        """
19
        Uses the virtualenv cli to create the virtual environment.
20
        :return:
21
        """
22
        self.venv_dir.mkdir(parents=True, exist_ok=True)
1✔
23
        from venv import main
1✔
24

25
        main([str(self.venv_dir)])
1✔
26

27
    @property
1✔
28
    def exists(self) -> bool:
1✔
29
        """
30
        Checks whether the virtual environment exists by checking whether the site-package directory of the venv exists.
31
        :return: the if the venv exists
32
        :raises NotADirectoryError: if the venv path exists but is not a directory
33
        """
34
        try:
1✔
35
            return True if self.site_dir else False
1✔
36
        except FileNotFoundError:
1✔
37
            return False
1✔
38

39
    @cached_property
1✔
40
    def venv_dir(self) -> Path:
1✔
41
        """
42
        Returns the path of the virtual environment directory
43
        :return: the path to the venv
44
        """
45
        return Path(self._venv_dir).absolute()
1✔
46

47
    @cached_property
1✔
48
    def site_dir(self) -> Path:
1✔
49
        """
50
        Resolves and returns the site-packages directory of the virtual environment. Once resolved successfully the
51
        result is cached.
52

53
        :return: the path to the site-packages dir.
54
        :raise FileNotFoundError: if the venv does not exist or the site-packages could not be found, or there are
55
         multiple lib/python* directories.
56
        :raise NotADirectoryError: if the venv is not a directory
57
        """
58
        venv = self.venv_dir
1✔
59

60
        if not venv.exists():
1✔
61
            raise FileNotFoundError(f"expected venv directory to exist at {venv}")
1✔
62

63
        if not venv.is_dir():
1✔
64
            raise NotADirectoryError(f"expected {venv} to be a directory")
×
65

66
        matches = list(venv.glob("lib/python*/site-packages"))
1✔
67

68
        if not matches:
1✔
69
            raise FileNotFoundError(f"could not find site-packages directory in {venv}")
×
70

71
        if len(matches) > 1:
1✔
72
            raise FileNotFoundError(f"multiple python versions found in {venv}: {matches}")
×
73

74
        return matches[0]
1✔
75

76
    def inject_to_sys_path(self) -> None:
1✔
77
        path = str(self.site_dir)
1✔
78
        if path and path not in sys.path:
1✔
79
            sys.path.append(path)
×
80

81
    def add_pth(self, name, path: Union[str, os.PathLike, "VirtualEnvironment"]) -> None:
1✔
82
        """
83
        Add a <name>.pth file into the virtual environment and append the given path to it. Does nothing if the path
84
        is already in the file.
85

86
        :param name: the name of the path file (without the .pth extensions)
87
        :param path: the path to be appended
88
        """
89
        pth_file = self.site_dir / f"{name}.pth"
×
90

91
        if isinstance(path, VirtualEnvironment):
×
92
            path = path.site_dir
×
93

94
        line = io.text_encoding(str(path)) + "\n"
×
95

96
        if pth_file.exists() and line in pth_file.read_text():
×
97
            return
×
98

99
        with open(pth_file, "a") as fd:
×
100
            fd.write(line)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc