FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pytype/pytype/pytype_source_utils.py at master · i-spark/pytype · GitHub
i-spark
/
pytype
Public
forked from
google/pytype
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
pytype
/
pytype
/
pytype_source_utils.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
159 lines (129 loc) · 4.87 KB
Breadcrumbs
pytype
/
pytype
/
pytype_source_utils.py
Copy path
File metadata and controls
159 lines (129 loc) · 4.87 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
"""Utilities for working with pytype source files."""
# All functions with a dependence on __file__ should go here.
# This file should be kept under pytype/ so that __file__.dirname is the
# top-level pytype directory.
import
atexit
import
os
import
re
import
tempfile
class
NoSuchDirectory
(
Exception
):
# pylint: disable=g-bad-exception-name
pass
def
pytype_source_dir
():
"""The base directory of the pytype source tree."""
res
=
os
.
path
.
dirname
(
__file__
)
if
os
.
path
.
basename
(
res
)
==
"__pycache__"
:
# For source-less par files __file__ points at __pycache__ subdirectory...
res
=
os
.
path
.
dirname
(
res
)
return
res
def
get_full_path
(
path
):
"""Full path to a file or directory within the pytype source tree.
Arguments:
path: An absolute or relative path.
Returns:
path for absolute paths.
full path resolved relative to pytype/ for relative paths.
"""
# TODO(mdemello): Insist on relative paths here.
return
os
.
path
.
join
(
pytype_source_dir
(),
path
)
def
load_text_file
(
filename
):
return
_load_data_file
(
filename
,
text
=
True
)
def
load_binary_file
(
filename
):
return
_load_data_file
(
filename
,
text
=
False
)
def
_load_data_file
(
filename
,
text
):
"""Get the contents of a data file from the pytype installation.
Arguments:
filename: the path, relative to "pytype/"
text: whether to load the file as text or bytes.
Returns:
The contents of the file as a bytestring
Raises:
IOError: if file not found
"""
path
=
filename
if
os
.
path
.
isabs
(
filename
)
else
get_full_path
(
filename
)
# Check for a ResourceLoader (see comment under list_pytype_files).
loader
=
globals
().
get
(
"__loader__"
,
None
)
if
loader
:
# For an explanation of the args to loader.get_data, see
# https://www.python.org/dev/peps/pep-0302/#optional-extensions-to-the-importer-protocol
# https://docs.python.org/3/library/importlib.html#importlib.abc.ResourceLoader.get_data
data
=
loader
.
get_data
(
path
)
if
text
:
# Opening a file in text mode automatically converts carriage returns to
# newlines, so we manually simulate that behavior here.
return
re
.
sub
(
"
\r
\n
?"
,
"
\n
"
,
data
.
decode
(
"utf-8"
))
return
data
with
open
(
path
,
"r"
if
text
else
"rb"
)
as
fi
:
return
fi
.
read
()
def
list_files
(
basedir
):
"""List files in the directory rooted at |basedir|."""
if
not
os
.
path
.
isdir
(
basedir
):
raise
NoSuchDirectory
(
basedir
)
directories
=
[
""
]
while
directories
:
d
=
directories
.
pop
()
for
basename
in
os
.
listdir
(
os
.
path
.
join
(
basedir
,
d
)):
filename
=
os
.
path
.
join
(
d
,
basename
)
if
os
.
path
.
isdir
(
os
.
path
.
join
(
basedir
,
filename
)):
directories
.
append
(
filename
)
elif
os
.
path
.
exists
(
os
.
path
.
join
(
basedir
,
filename
)):
yield
filename
def
list_pytype_files
(
suffix
):
"""Recursively get the contents of a directory in the pytype installation.
This reports files in said directory as well as all subdirectories of it.
Arguments:
suffix: the path, relative to "pytype/"
Yields:
The filenames, relative to pytype/{suffix}
Raises:
NoSuchDirectory: if the directory doesn't exist.
"""
assert
not
suffix
.
endswith
(
"/"
)
loader
=
globals
().
get
(
"__loader__"
,
None
)
try
:
# List directory using __loader__.
# __loader__ exists only when this file is in a Python archive in Python 2
# but is always present in Python 3, so we can't use the presence or
# absence of the loader to determine whether calling get_zipfile is okay.
filenames
=
loader
.
get_zipfile
().
namelist
()
# pytype: disable=attribute-error
except
AttributeError
:
# List directory using the file system
for
f
in
list_files
(
get_full_path
(
suffix
)):
yield
f
else
:
for
filename
in
filenames
:
directory
=
"pytype/"
+
suffix
+
"/"
try
:
i
=
filename
.
rindex
(
directory
)
except
ValueError
:
pass
else
:
yield
filename
[
i
+
len
(
directory
):]
# We allow analyzing Python 2 code with a custom interpreter to facilitate use
# of the type annotations backport.
CUSTOM_PY2_EXE
=
None
def
get_custom_python_exe
(
python_version
):
"""Get the path to a custom python interpreter.
If the given version is (2, 7), either returns CUSTOM_PY2_EXE if it exists, or
extracts it from a par file into /tmp/pytype and returns that.
Arguments:
python_version: the requested version, e.g. (2, 7)
Returns:
None if the version is not (2, 7) or an error occurs while loading the file.
Else: the path to the extracted file.
"""
if
not
CUSTOM_PY2_EXE
or
python_version
!=
(
2
,
7
):
return
None
path
=
os
.
path
.
normpath
(
get_full_path
(
CUSTOM_PY2_EXE
))
if
os
.
path
.
exists
(
path
):
return
path
try
:
data
=
load_binary_file
(
path
)
except
IOError
:
return
None
with
tempfile
.
NamedTemporaryFile
(
delete
=
False
,
suffix
=
"python"
)
as
fi
:
fi
.
write
(
data
)
fi
.
close
()
exe_file
=
fi
.
name
os
.
chmod
(
exe_file
,
0o750
)
atexit
.
register
(
lambda
:
os
.
unlink
(
exe_file
))
return
exe_file
Back
|
FazBrowse Home
|
New Git URL