-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
virtualenv.py
87 lines (79 loc) · 2.9 KB
/
virtualenv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
from pathlib import Path
import sys
from typing import Dict, Mapping
class Virtualenv:
def __init__(self, path: Path):
self.path = path.resolve()
self._is_windows = sys.platform == "win32"
def exists(self) -> bool:
"""
Check if the configured path points to a directory
"""
return self.path.is_dir()
def bin_dir(self) -> Path:
"""
Path to where the directory for installed executables should be
"""
if self._is_windows:
return self.path.joinpath("Scripts")
return self.path.joinpath("bin")
def resolve_executable(self, executable: str) -> str:
"""
If the given executable can be found in the bin_dir then return its absolute path
"""
bin_dir = self.bin_dir()
if bin_dir.joinpath(executable).is_file():
return str(bin_dir.joinpath(executable))
if self._is_windows:
if bin_dir.joinpath(f"{executable}.com").is_file():
return str(bin_dir.joinpath(f"{executable}.com"))
if bin_dir.joinpath(f"{executable}.exe").is_file():
return str(bin_dir.joinpath(f"{executable}.exe"))
if bin_dir.joinpath(f"{executable}.bat").is_file():
return str(bin_dir.joinpath(f"{executable}.bat"))
return executable
@staticmethod
def detect(parent_dir: Path) -> bool:
"""
Check whether there seems to be a valid virtualenv within the given directory at
either ./venv, or ./.venv
"""
return (
Virtualenv(parent_dir.joinpath("venv")).valid()
or Virtualenv(parent_dir.joinpath(".venv")).valid()
)
def valid(self) -> bool:
"""
Check that the path points to a dir that really is a virtualenv with reasonable
certainty
"""
if not self.path:
return False
bin_dir = self.bin_dir()
if self._is_windows:
return (
bin_dir.joinpath("activate").is_file()
and bin_dir.joinpath("python.exe").is_file()
and self.path.joinpath("Lib", "site-packages").is_dir()
)
return (
bin_dir.joinpath("activate").is_file()
and bin_dir.joinpath("python").is_file()
and bool(
next(
self.path.glob(os.path.sep.join(("lib", "python3*", "site-packages"))), # type: ignore
False,
)
)
)
def get_env_vars(self, base_env: Mapping[str, str]) -> Dict[str, str]:
path_delim = ";" if self._is_windows else ":"
result = dict(
base_env,
VIRTUAL_ENV=str(self.path),
PATH=f"{self.bin_dir()}{path_delim}{os.environ.get('PATH', '')}",
)
if "PYTHONHOME" in result:
result.pop("PYTHONHOME")
return result