/
pyinfo.py
48 lines (41 loc) · 1.84 KB
/
pyinfo.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
from __future__ import print_function
"""Utilities to find the site and prefix information of a Python executable, which may be Python 2.
This file MUST remain compatible with Python 2. Since we cannot make any assumptions about the
Python being executed, this module should not use *any* dependencies outside of the standard
library found in Python 2. This file is run each mypy run, so it should be kept as fast as
possible.
"""
import os
import sys
import sysconfig
MYPY = False
if MYPY:
from typing import List
def getsearchdirs():
# type: () -> List[str]
# Do not include things from the standard library
# because those should come from typeshed.
stdlib_zip = os.path.join(
sys.base_exec_prefix,
getattr(sys, "platlibdir", "lib"),
"python{}{}.zip".format(sys.version_info.major, sys.version_info.minor)
)
stdlib = sysconfig.get_path("stdlib")
stdlib_ext = os.path.join(stdlib, "lib-dynload")
excludes = set([stdlib_zip, stdlib, stdlib_ext])
# Drop the first entry of sys.path
# - If pyinfo.py is executed as a script (in a subprocess), this is the directory
# containing pyinfo.py
# - Otherwise, if mypy launched via console script, this is the directory of the script
# - Otherwise, if mypy launched via python -m mypy, this is the current directory
# In all cases, this is safe to drop
# Note that mypy adds the cwd to SearchPaths.python_path, so we still find things on the
# cwd consistently (the return value here sets SearchPaths.package_path)
abs_sys_path = (os.path.abspath(p) for p in sys.path[1:])
return [p for p in abs_sys_path if p not in excludes]
if __name__ == '__main__':
if sys.argv[-1] == 'getsearchdirs':
print(repr(getsearchdirs()))
else:
print("ERROR: incorrect argument to pyinfo.py.", file=sys.stderr)
sys.exit(1)