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