68
68
import os
69
69
import posixpath
70
70
71
+ if False :
72
+ # Mypy needs these symbols imported, but since they only exist in python 3.5+,
73
+ # this import may fail at runtime. Luckily mypy can follow this conditional import.
74
+ from typing import Callable , Dict , Optional , Tuple , Union
71
75
72
76
def CreateManifestBased (manifest_path ):
77
+ # type: (str) -> _Runfiles
73
78
return _Runfiles (_ManifestBased (manifest_path ))
74
79
75
80
76
81
def CreateDirectoryBased (runfiles_dir_path ):
82
+ # type: (str) -> _Runfiles
77
83
return _Runfiles (_DirectoryBased (runfiles_dir_path ))
78
84
79
85
80
86
def Create (env = None ):
87
+ # type: (Optional[Dict[str, str]]) -> Optional[_Runfiles]
81
88
"""Returns a new `Runfiles` instance.
82
89
83
90
The returned object is either:
@@ -120,9 +127,11 @@ class _Runfiles(object):
120
127
"""
121
128
122
129
def __init__ (self , strategy ):
130
+ # type: (Union[_ManifestBased, _DirectoryBased]) -> None
123
131
self ._strategy = strategy
124
132
125
133
def Rlocation (self , path ):
134
+ # type: (str) -> Optional[str]
126
135
"""Returns the runtime path of a runfile.
127
136
128
137
Runfiles are data-dependencies of Bazel-built binaries and tests.
@@ -162,6 +171,7 @@ def Rlocation(self, path):
162
171
return self ._strategy .RlocationChecked (path )
163
172
164
173
def EnvVars (self ):
174
+ # type: () -> Dict[str, str]
165
175
"""Returns environment variables for subprocesses.
166
176
167
177
The caller should set the returned key-value pairs in the environment of
@@ -179,6 +189,7 @@ class _ManifestBased(object):
179
189
"""`Runfiles` strategy that parses a runfiles-manifest to look up runfiles."""
180
190
181
191
def __init__ (self , path ):
192
+ # type: (str) -> None
182
193
if not path :
183
194
raise ValueError ()
184
195
if not isinstance (path , str ):
@@ -187,10 +198,12 @@ def __init__(self, path):
187
198
self ._runfiles = _ManifestBased ._LoadRunfiles (path )
188
199
189
200
def RlocationChecked (self , path ):
201
+ # type: (str) -> Optional[str]
190
202
return self ._runfiles .get (path )
191
203
192
204
@staticmethod
193
205
def _LoadRunfiles (path ):
206
+ # type: (str) -> Dict[str, str]
194
207
"""Loads the runfiles manifest."""
195
208
result = {}
196
209
with open (path , "r" ) as f :
@@ -205,6 +218,7 @@ def _LoadRunfiles(path):
205
218
return result
206
219
207
220
def _GetRunfilesDir (self ):
221
+ # type: () -> str
208
222
if self ._path .endswith ("/MANIFEST" ) or self ._path .endswith ("\\ MANIFEST" ):
209
223
return self ._path [: - len ("/MANIFEST" )]
210
224
elif self ._path .endswith (".runfiles_manifest" ):
@@ -213,6 +227,7 @@ def _GetRunfilesDir(self):
213
227
return ""
214
228
215
229
def EnvVars (self ):
230
+ # type: () -> Dict[str, str]
216
231
directory = self ._GetRunfilesDir ()
217
232
return {
218
233
"RUNFILES_MANIFEST_FILE" : self ._path ,
@@ -227,19 +242,23 @@ class _DirectoryBased(object):
227
242
"""`Runfiles` strategy that appends runfiles paths to the runfiles root."""
228
243
229
244
def __init__ (self , path ):
245
+ # type: (str) -> None
230
246
if not path :
231
247
raise ValueError ()
232
248
if not isinstance (path , str ):
233
249
raise TypeError ()
234
250
self ._runfiles_root = path
235
251
236
252
def RlocationChecked (self , path ):
253
+ # type: (str) -> str
254
+
237
255
# Use posixpath instead of os.path, because Bazel only creates a runfiles
238
256
# tree on Unix platforms, so `Create()` will only create a directory-based
239
257
# runfiles strategy on those platforms.
240
258
return posixpath .join (self ._runfiles_root , path )
241
259
242
260
def EnvVars (self ):
261
+ # type: () -> Dict[str, str]
243
262
return {
244
263
"RUNFILES_DIR" : self ._runfiles_root ,
245
264
# TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
@@ -251,6 +270,7 @@ def EnvVars(self):
251
270
def _PathsFrom (
252
271
argv0 , runfiles_mf , runfiles_dir , is_runfiles_manifest , is_runfiles_directory
253
272
):
273
+ # type: (str, str, str, Callable[[str], bool], Callable[[str], bool]) -> Tuple[str, str]
254
274
"""Discover runfiles manifest and runfiles directory paths.
255
275
256
276
Args:
0 commit comments