FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-python/pythonFiles/jedi/api/environment.py at master · plusls/vscode-python · GitHub
plusls
/
vscode-python
Public
forked from
microsoft/vscode-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-python
/
pythonFiles
/
jedi
/
api
/
environment.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
393 lines (315 loc) · 13.3 KB
Breadcrumbs
vscode-python
/
pythonFiles
/
jedi
/
api
/
environment.py
Copy path
File metadata and controls
393 lines (315 loc) · 13.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""
Environments are a way to activate different Python versions or Virtualenvs for
static analysis. The Python binary in that environment is going to be executed.
"""
import
os
import
re
import
sys
import
hashlib
import
filecmp
from
subprocess
import
PIPE
from
collections
import
namedtuple
# When dropping Python 2.7 support we should consider switching to
# `shutil.which`.
from
distutils
.
spawn
import
find_executable
from
jedi
.
_compatibility
import
GeneralizedPopen
from
jedi
.
cache
import
memoize_method
,
time_cache
from
jedi
.
evaluate
.
compiled
.
subprocess
import
get_subprocess
, \
EvaluatorSameProcess
,
EvaluatorSubprocess
import
parso
_VersionInfo
=
namedtuple
(
'VersionInfo'
,
'major minor micro'
)
_SUPPORTED_PYTHONS
=
[
'3.6'
,
'3.5'
,
'3.4'
,
'3.3'
,
'2.7'
]
_SAFE_PATHS
=
[
'/usr/bin'
,
'/usr/local/bin'
]
_CURRENT_VERSION
=
'%s.%s'
%
(
sys
.
version_info
.
major
,
sys
.
version_info
.
minor
)
class
InvalidPythonEnvironment
(
Exception
):
"""
If you see this exception, the Python executable or Virtualenv you have
been trying to use is probably not a correct Python version.
"""
class
_BaseEnvironment
(
object
):
@
memoize_method
def
get_grammar
(
self
):
version_string
=
'%s.%s'
%
(
self
.
version_info
.
major
,
self
.
version_info
.
minor
)
return
parso
.
load_grammar
(
version
=
version_string
)
@
property
def
_sha256
(
self
):
try
:
return
self
.
_hash
except
AttributeError
:
self
.
_hash
=
_calculate_sha256_for_file
(
self
.
executable
)
return
self
.
_hash
class
Environment
(
_BaseEnvironment
):
"""
This class is supposed to be created by internal Jedi architecture. You
should not create it directly. Please use create_environment or the other
functions instead. It is then returned by that function.
"""
def
__init__
(
self
,
path
,
executable
):
self
.
path
=
os
.
path
.
abspath
(
path
)
"""
The path to an environment, matches ``sys.prefix``.
"""
self
.
executable
=
os
.
path
.
abspath
(
executable
)
"""
The Python executable, matches ``sys.executable``.
"""
self
.
version_info
=
self
.
_get_version
()
"""
Like ``sys.version_info``. A tuple to show the current Environment's
Python version.
"""
def
_get_version
(
self
):
try
:
process
=
GeneralizedPopen
([
self
.
executable
,
'--version'
],
stdout
=
PIPE
,
stderr
=
PIPE
)
stdout
,
stderr
=
process
.
communicate
()
retcode
=
process
.
poll
()
if
retcode
:
raise
InvalidPythonEnvironment
()
except
OSError
:
raise
InvalidPythonEnvironment
()
# Until Python 3.4 wthe version string is part of stderr, after that
# stdout.
output
=
stdout
+
stderr
match
=
re
.
match
(
br'Python (\d+)\.(\d+)\.(\d+)'
,
output
)
if
match
is
None
:
raise
InvalidPythonEnvironment
(
"--version not working"
)
return
_VersionInfo
(
*
[
int
(
m
)
for
m
in
match
.
groups
()])
def
__repr__
(
self
):
version
=
'.'
.
join
(
str
(
i
)
for
i
in
self
.
version_info
)
return
'<%s: %s in %s>'
%
(
self
.
__class__
.
__name__
,
version
,
self
.
path
)
def
get_evaluator_subprocess
(
self
,
evaluator
):
return
EvaluatorSubprocess
(
evaluator
,
self
.
_get_subprocess
())
def
_get_subprocess
(
self
):
return
get_subprocess
(
self
.
executable
)
@
memoize_method
def
get_sys_path
(
self
):
"""
The sys path for this environment. Does not include potential
modifications like ``sys.path.append``.
:returns: list of str
"""
# It's pretty much impossible to generate the sys path without actually
# executing Python. The sys path (when starting with -S) itself depends
# on how the Python version was compiled (ENV variables).
# If you omit -S when starting Python (normal case), additionally
# site.py gets executed.
return
self
.
_get_subprocess
().
get_sys_path
()
class
SameEnvironment
(
Environment
):
def
__init__
(
self
):
super
(
SameEnvironment
,
self
).
__init__
(
sys
.
prefix
,
sys
.
executable
)
def
_get_version
(
self
):
return
_VersionInfo
(
*
sys
.
version_info
[:
3
])
class
InterpreterEnvironment
(
_BaseEnvironment
):
def
__init__
(
self
):
self
.
version_info
=
_VersionInfo
(
*
sys
.
version_info
[:
3
])
def
get_evaluator_subprocess
(
self
,
evaluator
):
return
EvaluatorSameProcess
(
evaluator
)
def
get_sys_path
(
self
):
return
sys
.
path
def
_get_virtual_env_from_var
():
var
=
os
.
environ
.
get
(
'VIRTUAL_ENV'
)
if
var
is
not
None
:
if
var
==
sys
.
prefix
:
return
SameEnvironment
()
try
:
return
create_environment
(
var
)
except
InvalidPythonEnvironment
:
pass
def
_calculate_sha256_for_file
(
path
):
sha256
=
hashlib
.
sha256
()
with
open
(
path
,
'rb'
)
as
f
:
for
block
in
iter
(
lambda
:
f
.
read
(
filecmp
.
BUFSIZE
),
b''
):
sha256
.
update
(
block
)
return
sha256
.
hexdigest
()
def
get_default_environment
():
"""
Tries to return an active Virtualenv. If there is no VIRTUAL_ENV variable
set it will return the latest Python version installed on the system. This
makes it possible to use as many new Python features as possible when using
autocompletion and other functionality.
:returns: :class:`Environment`
"""
virtual_env
=
_get_virtual_env_from_var
()
if
virtual_env
is
not
None
:
return
virtual_env
for
environment
in
find_system_environments
():
return
environment
# If no Python Environment is found, use the environment we're already
# using.
return
SameEnvironment
()
@
time_cache
(
seconds
=
10
*
60
)
# 10 Minutes
def
get_cached_default_environment
():
return
get_default_environment
()
def
find_virtualenvs
(
paths
=
None
,
**
kwargs
):
"""
:param paths: A list of paths in your file system to be scanned for
Virtualenvs. It will search in these paths and potentially execute the
Python binaries. Also the VIRTUAL_ENV variable will be checked if it
contains a valid Virtualenv.
:param safe: Default True. In case this is False, it will allow this
function to execute potential `python` environments. An attacker might
be able to drop an executable in a path this function is searching by
default. If the executable has not been installed by root, it will not
be executed.
:yields: :class:`Environment`
"""
def
py27_comp
(
paths
=
None
,
safe
=
True
):
if
paths
is
None
:
paths
=
[]
_used_paths
=
set
()
# Using this variable should be safe, because attackers might be able
# to drop files (via git) but not environment variables.
virtual_env
=
_get_virtual_env_from_var
()
if
virtual_env
is
not
None
:
yield
virtual_env
_used_paths
.
add
(
virtual_env
.
path
)
for
directory
in
paths
:
if
not
os
.
path
.
isdir
(
directory
):
continue
directory
=
os
.
path
.
abspath
(
directory
)
for
path
in
os
.
listdir
(
directory
):
path
=
os
.
path
.
join
(
directory
,
path
)
if
path
in
_used_paths
:
# A path shouldn't be evaluated twice.
continue
_used_paths
.
add
(
path
)
try
:
executable
=
_get_executable_path
(
path
,
safe
=
safe
)
yield
Environment
(
path
,
executable
)
except
InvalidPythonEnvironment
:
pass
return
py27_comp
(
paths
,
**
kwargs
)
def
find_system_environments
():
"""
Ignores virtualenvs and returns the Python versions that were installed on
your system. This might return nothing, if you're running Python e.g. from
a portable version.
The environments are sorted from latest to oldest Python version.
:yields: :class:`Environment`
"""
for
version_string
in
_SUPPORTED_PYTHONS
:
try
:
yield
get_system_environment
(
version_string
)
except
InvalidPythonEnvironment
:
pass
# TODO: the logic to find the Python prefix is much more complicated than that.
# See Modules/getpath.c for UNIX and PC/getpathp.c for Windows in CPython's
# source code. A solution would be to deduce it by running the Python
# interpreter and printing the value of sys.prefix.
def
_get_python_prefix
(
executable
):
if
os
.
name
!=
'nt'
:
return
os
.
path
.
dirname
(
os
.
path
.
dirname
(
executable
))
landmark
=
os
.
path
.
join
(
'Lib'
,
'os.py'
)
prefix
=
os
.
path
.
dirname
(
executable
)
while
prefix
:
if
os
.
path
.
join
(
prefix
,
landmark
):
return
prefix
prefix
=
os
.
path
.
dirname
(
prefix
)
raise
InvalidPythonEnvironment
(
"Cannot find prefix of executable %s."
%
executable
)
# TODO: this function should probably return a list of environments since
# multiple Python installations can be found on a system for the same version.
def
get_system_environment
(
version
):
"""
Return the first Python environment found for a string of the form 'X.Y'
where X and Y are the major and minor versions of Python.
:raises: :exc:`.InvalidPythonEnvironment`
:returns: :class:`Environment`
"""
exe
=
find_executable
(
'python'
+
version
)
if
exe
:
if
exe
==
sys
.
executable
:
return
SameEnvironment
()
return
Environment
(
_get_python_prefix
(
exe
),
exe
)
if
os
.
name
==
'nt'
:
for
prefix
,
exe
in
_get_executables_from_windows_registry
(
version
):
return
Environment
(
prefix
,
exe
)
raise
InvalidPythonEnvironment
(
"Cannot find executable python%s."
%
version
)
def
create_environment
(
path
,
safe
=
True
):
"""
Make it possible to create an environment by hand.
:raises: :exc:`.InvalidPythonEnvironment`
:returns: :class:`Environment`
"""
return
Environment
(
path
,
_get_executable_path
(
path
,
safe
=
safe
))
def
_get_executable_path
(
path
,
safe
=
True
):
"""
Returns None if it's not actually a virtual env.
"""
if
os
.
name
==
'nt'
:
python
=
os
.
path
.
join
(
path
,
'Scripts'
,
'python.exe'
)
else
:
python
=
os
.
path
.
join
(
path
,
'bin'
,
'python'
)
if
not
os
.
path
.
exists
(
python
):
raise
InvalidPythonEnvironment
(
"%s seems to be missing."
%
python
)
if
safe
and
not
_is_safe
(
python
):
raise
InvalidPythonEnvironment
(
"The python binary is potentially unsafe."
)
return
python
def
_get_executables_from_windows_registry
(
version
):
# The winreg module is named _winreg on Python 2.
try
:
import
winreg
except
ImportError
:
import
_winreg
as
winreg
# TODO: support Python Anaconda.
sub_keys
=
[
r'SOFTWARE\Python\PythonCore\{version}\InstallPath'
,
r'SOFTWARE\Wow6432Node\Python\PythonCore\{version}\InstallPath'
,
r'SOFTWARE\Python\PythonCore\{version}-32\InstallPath'
,
r'SOFTWARE\Wow6432Node\Python\PythonCore\{version}-32\InstallPath'
]
for
root_key
in
[
winreg
.
HKEY_CURRENT_USER
,
winreg
.
HKEY_LOCAL_MACHINE
]:
for
sub_key
in
sub_keys
:
sub_key
=
sub_key
.
format
(
version
=
version
)
try
:
with
winreg
.
OpenKey
(
root_key
,
sub_key
)
as
key
:
prefix
=
winreg
.
QueryValueEx
(
key
,
''
)[
0
]
exe
=
os
.
path
.
join
(
prefix
,
'python.exe'
)
if
os
.
path
.
isfile
(
exe
):
yield
prefix
,
exe
except
WindowsError
:
pass
def
_is_safe
(
executable_path
):
# Resolve sym links. A venv typically is a symlink to a known Python
# binary. Only virtualenvs copy symlinks around.
real_path
=
os
.
path
.
realpath
(
executable_path
)
if
_is_unix_safe_simple
(
real_path
):
return
True
# Just check the list of known Python versions. If it's not in there,
# it's likely an attacker or some Python that was not properly
# installed in the system.
for
environment
in
find_system_environments
():
if
environment
.
executable
==
real_path
:
return
True
# If the versions don't match, just compare the binary files. If we
# don't do that, only venvs will be working and not virtualenvs.
# venvs are symlinks while virtualenvs are actual copies of the
# Python files.
# This still means that if the system Python is updated and the
# virtualenv's Python is not (which is probably never going to get
# upgraded), it will not work with Jedi. IMO that's fine, because
# people should just be using venv. ~ dave
if
environment
.
_sha256
==
_calculate_sha256_for_file
(
real_path
):
return
True
return
False
def
_is_unix_safe_simple
(
real_path
):
if
_is_unix_admin
():
# In case we are root, just be conservative and
# only execute known paths.
return
any
(
real_path
.
startswith
(
p
)
for
p
in
_SAFE_PATHS
)
uid
=
os
.
stat
(
real_path
).
st_uid
# The interpreter needs to be owned by root. This means that it wasn't
# written by a user and therefore attacking Jedi is not as simple.
# The attack could look like the following:
# 1. A user clones a repository.
# 2. The repository has an innocent looking folder called foobar. jedi
# searches for the folder and executes foobar/bin/python --version if
# there's also a foobar/bin/activate.
# 3. The bin/python is obviously not a python script but a bash script or
# whatever the attacker wants.
return
uid
==
0
def
_is_unix_admin
():
try
:
return
os
.
getuid
()
==
0
except
AttributeError
:
return
False
# Windows
Back
|
FazBrowse Home
|
New Git URL