| 1 |
""" |
| 2 |
Environments are a way to activate different Python versions or Virtualenvs for |
| 3 |
static analysis. The Python binary in that environment is going to be executed. |
| 4 |
""" |
| 5 |
import os |
| 6 |
import sys |
| 7 |
import hashlib |
| 8 |
import filecmp |
| 9 |
from collections import namedtuple |
| 10 |
from shutil import which |
| 11 |
from typing import TYPE_CHECKING, Any |
| 12 |
|
| 13 |
from jedi.cache import memoize_method, time_cache |
| 14 |
from jedi.inference.compiled.subprocess import CompiledSubprocess, \ |
| 15 |
InferenceStateSameProcess, InferenceStateSubprocess |
| 16 |
|
| 17 |
import parso |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from jedi.inference import InferenceState |
| 21 |
|
| 22 |
|
| 23 |
_VersionInfo = namedtuple('VersionInfo', 'major minor micro') |
| 24 |
|
| 25 |
_SUPPORTED_PYTHONS = ['3.14', '3.13', '3.12', '3.11', '3.10'] |
| 26 |
_SAFE_PATHS = ['/usr/bin', '/usr/local/bin'] |
| 27 |
_CONDA_VAR = 'CONDA_PREFIX' |
| 28 |
_CURRENT_VERSION = '%s.%s' % (sys.version_info.major, sys.version_info.minor) |
| 29 |
|
| 30 |
|
| 31 |
class InvalidPythonEnvironment(Exception): |
| 32 |
""" |
| 33 |
If you see this exception, the Python executable or Virtualenv you have |
| 34 |
been trying to use is probably not a correct Python version. |
| 35 |
""" |
| 36 |
|
| 37 |
|
| 38 |
class _BaseEnvironment: |
| 39 |
version_info: Any |
| 40 |
executable: Any |
| 41 |
|
| 42 |
@memoize_method |
| 43 |
def get_grammar(self): |
| 44 |
version_string = '%s.%s' % (self.version_info.major, self.version_info.minor) |
| 45 |
return parso.load_grammar(version=version_string) |
| 46 |
|
| 47 |
@property |
| 48 |
def _sha256(self): |
| 49 |
try: |
| 50 |
return self._hash |
| 51 |
except AttributeError: |
| 52 |
self._hash = _calculate_sha256_for_file(self.executable) |
| 53 |
return self._hash |
| 54 |
|
| 55 |
|
| 56 |
def _get_info(): |
| 57 |
return ( |
| 58 |
sys.executable, |
| 59 |
sys.prefix, |
| 60 |
sys.version_info[:3], |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
class Environment(_BaseEnvironment): |
| 65 |
""" |
| 66 |
This class is supposed to be created by internal Jedi architecture. You |
| 67 |
should not create it directly. Please use create_environment or the other |
| 68 |
functions instead. It is then returned by that function. |
| 69 |
""" |
| 70 |
_subprocess = None |
| 71 |
|
| 72 |
def __init__(self, executable, env_vars=None): |
| 73 |
self._start_executable = executable |
| 74 |
self._env_vars = env_vars |
| 75 |
|
| 76 |
self._get_subprocess() |
| 77 |
|
| 78 |
def _get_subprocess(self): |
| 79 |
if self._subprocess is not None and not self._subprocess.is_crashed: |
| 80 |
return self._subprocess |
| 81 |
|
| 82 |
try: |
| 83 |
self._subprocess = CompiledSubprocess(self._start_executable, |
| 84 |
env_vars=self._env_vars) |
| 85 |
info = self._subprocess._send(None, _get_info) |
| 86 |
except Exception as exc: |
| 87 |
raise InvalidPythonEnvironment( |
| 88 |
"Could not get version information for %r: %r" % ( |
| 89 |
self._start_executable, |
| 90 |
exc)) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
self.executable = info[0] |
| 95 |
""" |
| 96 |
The Python executable, matches ``sys.executable``. |
| 97 |
""" |
| 98 |
self.path = info[1] |
| 99 |
""" |
| 100 |
The path to an environment, matches ``sys.prefix``. |
| 101 |
""" |
| 102 |
self.version_info = _VersionInfo(*info[2]) |
| 103 |
""" |
| 104 |
Like :data:`sys.version_info`: a tuple to show the current |
| 105 |
Environment's Python version. |
| 106 |
""" |
| 107 |
return self._subprocess |
| 108 |
|
| 109 |
def __repr__(self): |
| 110 |
version = '.'.join(str(i) for i in self.version_info) |
| 111 |
return '<%s: %s in %s>' % (self.__class__.__name__, version, self.path) |
| 112 |
|
| 113 |
def get_inference_state_subprocess( |
| 114 |
self, |
| 115 |
inference_state: 'InferenceState', |
| 116 |
) -> InferenceStateSubprocess: |
| 117 |
return InferenceStateSubprocess(inference_state, self._get_subprocess()) |
| 118 |
|
| 119 |
@memoize_method |
| 120 |
def get_sys_path(self): |
| 121 |
""" |
| 122 |
The sys path for this environment. Does not include potential |
| 123 |
modifications from e.g. appending to :data:`sys.path`. |
| 124 |
|
| 125 |
:returns: list of str |
| 126 |
""" |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
return self._get_subprocess().get_sys_path() |
| 133 |
|
| 134 |
|
| 135 |
class _SameEnvironmentMixin: |
| 136 |
def __init__(self): |
| 137 |
self._start_executable = self.executable = sys.executable |
| 138 |
self.path = sys.prefix |
| 139 |
self.version_info = _VersionInfo(*sys.version_info[:3]) |
| 140 |
self._env_vars = None |
| 141 |
|
| 142 |
|
| 143 |
class SameEnvironment(_SameEnvironmentMixin, Environment): |
| 144 |
pass |
| 145 |
|
| 146 |
|
| 147 |
class InterpreterEnvironment(_SameEnvironmentMixin, _BaseEnvironment): |
| 148 |
def get_inference_state_subprocess( |
| 149 |
self, |
| 150 |
inference_state: 'InferenceState', |
| 151 |
) -> InferenceStateSameProcess: |
| 152 |
return InferenceStateSameProcess(inference_state) |
| 153 |
|
| 154 |
def get_sys_path(self): |
| 155 |
return sys.path |
| 156 |
|
| 157 |
|
| 158 |
def _get_virtual_env_from_var(env_var='VIRTUAL_ENV'): |
| 159 |
"""Get virtualenv environment from VIRTUAL_ENV environment variable. |
| 160 |
|
| 161 |
It uses `safe=False` with ``create_environment``, because the environment |
| 162 |
variable is considered to be safe / controlled by the user solely. |
| 163 |
""" |
| 164 |
var = os.environ.get(env_var) |
| 165 |
if var: |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if os.path.realpath(var) == os.path.realpath(sys.prefix): |
| 171 |
return _try_get_same_env() |
| 172 |
|
| 173 |
try: |
| 174 |
return create_environment(var, safe=False) |
| 175 |
except InvalidPythonEnvironment: |
| 176 |
pass |
| 177 |
|
| 178 |
|
| 179 |
def _calculate_sha256_for_file(path): |
| 180 |
sha256 = hashlib.sha256() |
| 181 |
with open(path, 'rb') as f: |
| 182 |
for block in iter(lambda: f.read(filecmp.BUFSIZE), b''): |
| 183 |
sha256.update(block) |
| 184 |
return sha256.hexdigest() |
| 185 |
|
| 186 |
|
| 187 |
def get_default_environment(): |
| 188 |
""" |
| 189 |
Tries to return an active Virtualenv or conda environment. |
| 190 |
If there is no VIRTUAL_ENV variable or no CONDA_PREFIX variable set |
| 191 |
set it will return the latest Python version installed on the system. This |
| 192 |
makes it possible to use as many new Python features as possible when using |
| 193 |
autocompletion and other functionality. |
| 194 |
|
| 195 |
:returns: :class:`.Environment` |
| 196 |
""" |
| 197 |
virtual_env = _get_virtual_env_from_var() |
| 198 |
if virtual_env is not None: |
| 199 |
return virtual_env |
| 200 |
|
| 201 |
conda_env = _get_virtual_env_from_var(_CONDA_VAR) |
| 202 |
if conda_env is not None: |
| 203 |
return conda_env |
| 204 |
|
| 205 |
return _try_get_same_env() |
| 206 |
|
| 207 |
|
| 208 |
def _try_get_same_env(): |
| 209 |
env = SameEnvironment() |
| 210 |
if not os.path.basename(env.executable).lower().startswith('python'): |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if os.name == 'nt': |
| 226 |
|
| 227 |
|
| 228 |
checks = (r'Scripts\python.exe', 'python.exe') |
| 229 |
else: |
| 230 |
|
| 231 |
checks = ( |
| 232 |
'bin/python%s.%s' % (sys.version_info[0], sys.version[1]), |
| 233 |
'bin/python%s' % (sys.version_info[0]), |
| 234 |
'bin/python', |
| 235 |
) |
| 236 |
for check in checks: |
| 237 |
guess = os.path.join(sys.exec_prefix, check) |
| 238 |
if os.path.isfile(guess): |
| 239 |
|
| 240 |
return Environment(guess) |
| 241 |
|
| 242 |
return InterpreterEnvironment() |
| 243 |
|
| 244 |
|
| 245 |
return env |
| 246 |
|
| 247 |
|
| 248 |
def get_cached_default_environment(): |
| 249 |
var = os.environ.get('VIRTUAL_ENV') or os.environ.get(_CONDA_VAR) |
| 250 |
environment = _get_cached_default_environment() |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
if var and os.path.realpath(var) != os.path.realpath(environment.path): |
| 257 |
_get_cached_default_environment.clear_cache() |
| 258 |
return _get_cached_default_environment() |
| 259 |
return environment |
| 260 |
|
| 261 |
|
| 262 |
@time_cache(seconds=10 * 60) |
| 263 |
def _get_cached_default_environment(): |
| 264 |
try: |
| 265 |
return get_default_environment() |
| 266 |
except InvalidPythonEnvironment: |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
return InterpreterEnvironment() |
| 272 |
|
| 273 |
|
| 274 |
def find_virtualenvs(paths=None, *, safe=True, use_environment_vars=True): |
| 275 |
""" |
| 276 |
:param paths: A list of paths in your file system to be scanned for |
| 277 |
Virtualenvs. It will search in these paths and potentially execute the |
| 278 |
Python binaries. |
| 279 |
:param safe: Default True. In case this is False, it will allow this |
| 280 |
function to execute potential `python` environments. An attacker might |
| 281 |
be able to drop an executable in a path this function is searching by |
| 282 |
default. If the executable has not been installed by root, it will not |
| 283 |
be executed. |
| 284 |
:param use_environment_vars: Default True. If True, the VIRTUAL_ENV |
| 285 |
variable will be checked if it contains a valid VirtualEnv. |
| 286 |
CONDA_PREFIX will be checked to see if it contains a valid conda |
| 287 |
environment. |
| 288 |
|
| 289 |
:yields: :class:`.Environment` |
| 290 |
""" |
| 291 |
if paths is None: |
| 292 |
paths = [] |
| 293 |
|
| 294 |
_used_paths = set() |
| 295 |
|
| 296 |
if use_environment_vars: |
| 297 |
|
| 298 |
|
| 299 |
virtual_env = _get_virtual_env_from_var() |
| 300 |
if virtual_env is not None: |
| 301 |
yield virtual_env |
| 302 |
_used_paths.add(virtual_env.path) |
| 303 |
|
| 304 |
conda_env = _get_virtual_env_from_var(_CONDA_VAR) |
| 305 |
if conda_env is not None: |
| 306 |
yield conda_env |
| 307 |
_used_paths.add(conda_env.path) |
| 308 |
|
| 309 |
for directory in paths: |
| 310 |
if not os.path.isdir(directory): |
| 311 |
continue |
| 312 |
|
| 313 |
directory = os.path.abspath(directory) |
| 314 |
for path in os.listdir(directory): |
| 315 |
path = os.path.join(directory, path) |
| 316 |
if path in _used_paths: |
| 317 |
|
| 318 |
continue |
| 319 |
_used_paths.add(path) |
| 320 |
|
| 321 |
try: |
| 322 |
executable = _get_executable_path(path, safe=safe) |
| 323 |
yield Environment(executable) |
| 324 |
except InvalidPythonEnvironment: |
| 325 |
pass |
| 326 |
|
| 327 |
|
| 328 |
def find_system_environments(*, env_vars=None): |
| 329 |
""" |
| 330 |
Ignores virtualenvs and returns the Python versions that were installed on |
| 331 |
your system. This might return nothing, if you're running Python e.g. from |
| 332 |
a portable version. |
| 333 |
|
| 334 |
The environments are sorted from latest to oldest Python version. |
| 335 |
|
| 336 |
:yields: :class:`.Environment` |
| 337 |
""" |
| 338 |
for version_string in _SUPPORTED_PYTHONS: |
| 339 |
try: |
| 340 |
yield get_system_environment(version_string, env_vars=env_vars) |
| 341 |
except InvalidPythonEnvironment: |
| 342 |
pass |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
def get_system_environment(version, *, env_vars=None): |
| 348 |
""" |
| 349 |
Return the first Python environment found for a string of the form 'X.Y' |
| 350 |
where X and Y are the major and minor versions of Python. |
| 351 |
|
| 352 |
:raises: :exc:`.InvalidPythonEnvironment` |
| 353 |
:returns: :class:`.Environment` |
| 354 |
""" |
| 355 |
exe = which('python' + version) |
| 356 |
if exe: |
| 357 |
if exe == sys.executable: |
| 358 |
return SameEnvironment() |
| 359 |
return Environment(exe) |
| 360 |
|
| 361 |
if sys.platform == "win32": |
| 362 |
for exe in _get_executables_from_windows_registry(version): |
| 363 |
try: |
| 364 |
return Environment(exe, env_vars=env_vars) |
| 365 |
except InvalidPythonEnvironment: |
| 366 |
pass |
| 367 |
raise InvalidPythonEnvironment("Cannot find executable python%s." % version) |
| 368 |
|
| 369 |
|
| 370 |
def create_environment(path, *, safe=True, env_vars=None): |
| 371 |
""" |
| 372 |
Make it possible to manually create an Environment object by specifying a |
| 373 |
Virtualenv path or an executable path and optional environment variables. |
| 374 |
|
| 375 |
:raises: :exc:`.InvalidPythonEnvironment` |
| 376 |
:returns: :class:`.Environment` |
| 377 |
""" |
| 378 |
if os.path.isfile(path): |
| 379 |
_assert_safe(path, safe) |
| 380 |
return Environment(path, env_vars=env_vars) |
| 381 |
return Environment(_get_executable_path(path, safe=safe), env_vars=env_vars) |
| 382 |
|
| 383 |
|
| 384 |
def _get_executable_path(path, safe=True): |
| 385 |
""" |
| 386 |
Returns None if it's not actually a virtual env. |
| 387 |
""" |
| 388 |
|
| 389 |
if sys.platform == "win32": |
| 390 |
pythons = [os.path.join(path, 'Scripts', 'python.exe'), os.path.join(path, 'python.exe')] |
| 391 |
else: |
| 392 |
pythons = [os.path.join(path, 'bin', 'python')] |
| 393 |
for python in pythons: |
| 394 |
if os.path.exists(python): |
| 395 |
break |
| 396 |
else: |
| 397 |
raise InvalidPythonEnvironment("%s seems to be missing." % python) |
| 398 |
|
| 399 |
_assert_safe(python, safe) |
| 400 |
return python |
| 401 |
|
| 402 |
|
| 403 |
if sys.platform == "win32": |
| 404 |
def _get_executables_from_windows_registry(version): |
| 405 |
import winreg |
| 406 |
|
| 407 |
|
| 408 |
sub_keys = [ |
| 409 |
r'SOFTWARE\Python\PythonCore\{version}\InstallPath', |
| 410 |
r'SOFTWARE\Wow6432Node\Python\PythonCore\{version}\InstallPath', |
| 411 |
r'SOFTWARE\Python\PythonCore\{version}-32\InstallPath', |
| 412 |
r'SOFTWARE\Wow6432Node\Python\PythonCore\{version}-32\InstallPath' |
| 413 |
] |
| 414 |
for root_key in [winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE]: |
| 415 |
for sub_key in sub_keys: |
| 416 |
sub_key = sub_key.format(version=version) |
| 417 |
try: |
| 418 |
with winreg.OpenKey(root_key, sub_key) as key: |
| 419 |
prefix = winreg.QueryValueEx(key, '')[0] |
| 420 |
exe = os.path.join(prefix, 'python.exe') |
| 421 |
if os.path.isfile(exe): |
| 422 |
yield exe |
| 423 |
except WindowsError: |
| 424 |
pass |
| 425 |
|
| 426 |
|
| 427 |
def _assert_safe(executable_path, safe): |
| 428 |
if safe and not _is_safe(executable_path): |
| 429 |
raise InvalidPythonEnvironment( |
| 430 |
"The python binary is potentially unsafe.") |
| 431 |
|
| 432 |
|
| 433 |
def _is_safe(executable_path): |
| 434 |
|
| 435 |
|
| 436 |
real_path = os.path.realpath(executable_path) |
| 437 |
|
| 438 |
if _is_unix_safe_simple(real_path): |
| 439 |
return True |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
for environment in find_system_environments(): |
| 445 |
if environment.executable == real_path: |
| 446 |
return True |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
if environment._sha256 == _calculate_sha256_for_file(real_path): |
| 457 |
return True |
| 458 |
return False |
| 459 |
|
| 460 |
|
| 461 |
def _is_unix_safe_simple(real_path): |
| 462 |
if _is_unix_admin(): |
| 463 |
|
| 464 |
|
| 465 |
return any(real_path.startswith(p) for p in _SAFE_PATHS) |
| 466 |
|
| 467 |
uid = os.stat(real_path).st_uid |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
return uid == 0 |
| 478 |
|
| 479 |
|
| 480 |
def _is_unix_admin(): |
| 481 |
try: |
| 482 |
return os.getuid() == 0 |
| 483 |
except AttributeError: |
| 484 |
return False |