Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 1 | # Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Runfiles lookup library for Bazel-built Python binaries and tests. |
| 15 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 16 | USAGE: |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 17 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 18 | 1. Depend on this runfiles library from your build rule: |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 19 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 20 | py_binary( |
| 21 | name = "my_binary", |
| 22 | ... |
brandjon | e4ccba4 | 2019-08-01 14:27:50 -0700 | [diff] [blame] | 23 | deps = ["@rules_python//python/runfiles"], |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 24 | ) |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 25 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 26 | 2. Import the runfiles library. |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 27 | |
brandjon | e4ccba4 | 2019-08-01 14:27:50 -0700 | [diff] [blame] | 28 | from rules_python.python.runfiles import runfiles |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 29 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 30 | 3. Create a Runfiles object and use rlocation to look up runfile paths: |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 31 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 32 | r = runfiles.Create() |
| 33 | ... |
| 34 | with open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f: |
| 35 | contents = f.readlines() |
| 36 | ... |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 37 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 38 | The code above creates a manifest- or directory-based implementations based |
| 39 | on the environment variables in os.environ. See `Create()` for more info. |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 40 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 41 | If you want to explicitly create a manifest- or directory-based |
| 42 | implementations, you can do so as follows: |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 43 | |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 44 | r1 = runfiles.CreateManifestBased("path/to/foo.runfiles_manifest") |
| 45 | |
| 46 | r2 = runfiles.CreateDirectoryBased("path/to/foo.runfiles/") |
| 47 | |
| 48 | If you want to start subprocesses that also need runfiles, you need to set |
| 49 | the right environment variables for them: |
| 50 | |
| 51 | import subprocess |
brandjon | e4ccba4 | 2019-08-01 14:27:50 -0700 | [diff] [blame] | 52 | from rules_python.python.runfiles import runfiles |
Laszlo Csomor | 44646c2 | 2018-06-27 05:09:38 -0700 | [diff] [blame] | 53 | |
| 54 | r = runfiles.Create() |
| 55 | env = {} |
| 56 | ... |
| 57 | env.update(r.EnvVars()) |
| 58 | p = subprocess.Popen([r.Rlocation("path/to/binary")], env, ...) |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 59 | """ |
| 60 | |
| 61 | import os |
| 62 | import posixpath |
| 63 | |
| 64 | |
| 65 | def CreateManifestBased(manifest_path): |
| 66 | return _Runfiles(_ManifestBased(manifest_path)) |
| 67 | |
| 68 | |
| 69 | def CreateDirectoryBased(runfiles_dir_path): |
| 70 | return _Runfiles(_DirectoryBased(runfiles_dir_path)) |
| 71 | |
| 72 | |
| 73 | def Create(env=None): |
| 74 | """Returns a new `Runfiles` instance. |
| 75 | |
| 76 | The returned object is either: |
| 77 | - manifest-based, meaning it looks up runfile paths from a manifest file, or |
| 78 | - directory-based, meaning it looks up runfile paths under a given directory |
| 79 | path |
| 80 | |
| 81 | If `env` contains "RUNFILES_MANIFEST_FILE" with non-empty value, this method |
| 82 | returns a manifest-based implementation. The object eagerly reads and caches |
| 83 | the whole manifest file upon instantiation; this may be relevant for |
| 84 | performance consideration. |
| 85 | |
Laszlo Csomor | 13c3373 | 2018-02-08 09:56:04 -0800 | [diff] [blame] | 86 | Otherwise, if `env` contains "RUNFILES_DIR" with non-empty value (checked in |
| 87 | this priority order), this method returns a directory-based implementation. |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 88 | |
| 89 | If neither cases apply, this method returns null. |
| 90 | |
| 91 | Args: |
| 92 | env: {string: string}; optional; the map of environment variables. If None, |
| 93 | this function uses the environment variable map of this process. |
| 94 | Raises: |
| 95 | IOError: if some IO error occurs. |
| 96 | """ |
| 97 | env_map = os.environ if env is None else env |
| 98 | manifest = env_map.get("RUNFILES_MANIFEST_FILE") |
| 99 | if manifest: |
| 100 | return CreateManifestBased(manifest) |
| 101 | |
| 102 | directory = env_map.get("RUNFILES_DIR") |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 103 | if directory: |
| 104 | return CreateDirectoryBased(directory) |
| 105 | |
| 106 | return None |
| 107 | |
| 108 | |
| 109 | class _Runfiles(object): |
| 110 | """Returns the runtime location of runfiles. |
| 111 | |
| 112 | Runfiles are data-dependencies of Bazel-built binaries and tests. |
| 113 | """ |
| 114 | |
| 115 | def __init__(self, strategy): |
| 116 | self._strategy = strategy |
| 117 | |
| 118 | def Rlocation(self, path): |
| 119 | """Returns the runtime path of a runfile. |
| 120 | |
| 121 | Runfiles are data-dependencies of Bazel-built binaries and tests. |
| 122 | |
| 123 | The returned path may not be valid. The caller should check the path's |
| 124 | validity and that the path exists. |
| 125 | |
| 126 | The function may return None. In that case the caller can be sure that the |
| 127 | rule does not know about this data-dependency. |
| 128 | |
| 129 | Args: |
| 130 | path: string; runfiles-root-relative path of the runfile |
| 131 | Returns: |
| 132 | the path to the runfile, which the caller should check for existence, or |
| 133 | None if the method doesn't know about this runfile |
| 134 | Raises: |
| 135 | TypeError: if `path` is not a string |
Laszlo Csomor | f9cb859 | 2018-04-24 05:52:37 -0700 | [diff] [blame] | 136 | ValueError: if `path` is None or empty, or it's absolute or not normalized |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 137 | """ |
| 138 | if not path: |
| 139 | raise ValueError() |
| 140 | if not isinstance(path, str): |
| 141 | raise TypeError() |
Laszlo Csomor | f9cb859 | 2018-04-24 05:52:37 -0700 | [diff] [blame] | 142 | if (path.startswith("../") or "/.." in path or path.startswith("./") or |
| 143 | "/./" in path or path.endswith("/.") or "//" in path): |
| 144 | raise ValueError("path is not normalized: \"%s\"" % path) |
Laszlo Csomor | b961b0a | 2018-03-09 01:02:45 -0800 | [diff] [blame] | 145 | if path[0] == "\\": |
| 146 | raise ValueError("path is absolute without a drive letter: \"%s\"" % path) |
| 147 | if os.path.isabs(path): |
| 148 | return path |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 149 | return self._strategy.RlocationChecked(path) |
| 150 | |
Laszlo Csomor | 1d46d62 | 2018-02-09 02:36:35 -0800 | [diff] [blame] | 151 | def EnvVars(self): |
| 152 | """Returns environment variables for subprocesses. |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 153 | |
Laszlo Csomor | 1d46d62 | 2018-02-09 02:36:35 -0800 | [diff] [blame] | 154 | The caller should set the returned key-value pairs in the environment of |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 155 | subprocesses in case those subprocesses are also Bazel-built binaries that |
| 156 | need to use runfiles. |
| 157 | |
| 158 | Returns: |
Laszlo Csomor | 1d46d62 | 2018-02-09 02:36:35 -0800 | [diff] [blame] | 159 | {string: string}; a dict; keys are environment variable names, values are |
| 160 | the values for these environment variables |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 161 | """ |
Laszlo Csomor | 1d46d62 | 2018-02-09 02:36:35 -0800 | [diff] [blame] | 162 | return self._strategy.EnvVars() |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 163 | |
| 164 | |
| 165 | class _ManifestBased(object): |
| 166 | """`Runfiles` strategy that parses a runfiles-manifest to look up runfiles.""" |
| 167 | |
| 168 | def __init__(self, path): |
| 169 | if not path: |
| 170 | raise ValueError() |
| 171 | if not isinstance(path, str): |
| 172 | raise TypeError() |
| 173 | self._path = path |
| 174 | self._runfiles = _ManifestBased._LoadRunfiles(path) |
| 175 | |
| 176 | def RlocationChecked(self, path): |
Fabian Meumertzheim | 486d153 | 2022-02-07 05:49:47 -0800 | [diff] [blame] | 177 | """Returns the runtime path of a runfile.""" |
| 178 | exact_match = self._runfiles.get(path) |
| 179 | if exact_match: |
| 180 | return exact_match |
| 181 | # If path references a runfile that lies under a directory that itself is a |
| 182 | # runfile, then only the directory is listed in the manifest. Look up all |
| 183 | # prefixes of path in the manifest and append the relative path from the |
| 184 | # prefix to the looked up path. |
| 185 | prefix_end = len(path) |
| 186 | while True: |
| 187 | prefix_end = path.rfind("/", 0, prefix_end - 1) |
| 188 | if prefix_end == -1: |
| 189 | return None |
| 190 | prefix_match = self._runfiles.get(path[0:prefix_end]) |
| 191 | if prefix_match: |
| 192 | return prefix_match + "/" + path[prefix_end + 1:] |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 193 | |
| 194 | @staticmethod |
| 195 | def _LoadRunfiles(path): |
| 196 | """Loads the runfiles manifest.""" |
| 197 | result = {} |
| 198 | with open(path, "r") as f: |
| 199 | for line in f: |
| 200 | line = line.strip() |
| 201 | if line: |
| 202 | tokens = line.split(" ", 1) |
| 203 | if len(tokens) == 1: |
| 204 | result[line] = line |
| 205 | else: |
| 206 | result[tokens[0]] = tokens[1] |
| 207 | return result |
| 208 | |
Laszlo Csomor | bb1d085 | 2018-02-15 02:59:13 -0800 | [diff] [blame] | 209 | def _GetRunfilesDir(self): |
| 210 | if self._path.endswith("/MANIFEST") or self._path.endswith("\\MANIFEST"): |
| 211 | return self._path[:-len("/MANIFEST")] |
| 212 | elif self._path.endswith(".runfiles_manifest"): |
| 213 | return self._path[:-len("_manifest")] |
| 214 | else: |
| 215 | return "" |
| 216 | |
Laszlo Csomor | 1d46d62 | 2018-02-09 02:36:35 -0800 | [diff] [blame] | 217 | def EnvVars(self): |
Laszlo Csomor | bb1d085 | 2018-02-15 02:59:13 -0800 | [diff] [blame] | 218 | directory = self._GetRunfilesDir() |
| 219 | return { |
| 220 | "RUNFILES_MANIFEST_FILE": self._path, |
| 221 | "RUNFILES_DIR": directory, |
| 222 | # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can |
| 223 | # pick up RUNFILES_DIR. |
| 224 | "JAVA_RUNFILES": directory, |
| 225 | } |
Laszlo Csomor | a610a2b | 2018-02-05 05:24:34 -0800 | [diff] [blame] | 226 | |
| 227 | |
| 228 | class _DirectoryBased(object): |
| 229 | """`Runfiles` strategy that appends runfiles paths to the runfiles root.""" |
| 230 | |
| 231 | def __init__(self, path): |
| 232 | if not path: |
| 233 | raise ValueError() |
| 234 | if not isinstance(path, str): |
| 235 | raise TypeError() |
| 236 | self._runfiles_root = path |
| 237 | |
| 238 | def RlocationChecked(self, path): |
| 239 | # Use posixpath instead of os.path, because Bazel only creates a runfiles |
| 240 | # tree on Unix platforms, so `Create()` will only create a directory-based |
| 241 | # runfiles strategy on those platforms. |
| 242 | return posixpath.join(self._runfiles_root, path) |
| 243 | |
Laszlo Csomor | 1d46d62 | 2018-02-09 02:36:35 -0800 | [diff] [blame] | 244 | def EnvVars(self): |
Laszlo Csomor | bb1d085 | 2018-02-15 02:59:13 -0800 | [diff] [blame] | 245 | return { |
| 246 | "RUNFILES_DIR": self._runfiles_root, |
| 247 | # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can |
| 248 | # pick up RUNFILES_DIR. |
| 249 | "JAVA_RUNFILES": self._runfiles_root, |
| 250 | } |
Laszlo Csomor | c29f34f | 2018-05-22 05:01:41 -0700 | [diff] [blame] | 251 | |
| 252 | |
| 253 | def _PathsFrom(argv0, runfiles_mf, runfiles_dir, is_runfiles_manifest, |
| 254 | is_runfiles_directory): |
| 255 | """Discover runfiles manifest and runfiles directory paths. |
| 256 | |
| 257 | Args: |
| 258 | argv0: string; the value of sys.argv[0] |
| 259 | runfiles_mf: string; the value of the RUNFILES_MANIFEST_FILE environment |
| 260 | variable |
| 261 | runfiles_dir: string; the value of the RUNFILES_DIR environment variable |
| 262 | is_runfiles_manifest: lambda(string):bool; returns true if the argument is |
| 263 | the path of a runfiles manifest file |
| 264 | is_runfiles_directory: lambda(string):bool; returns true if the argument is |
| 265 | the path of a runfiles directory |
| 266 | |
| 267 | Returns: |
| 268 | (string, string) pair, first element is the path to the runfiles manifest, |
| 269 | second element is the path to the runfiles directory. If the first element |
| 270 | is non-empty, then is_runfiles_manifest returns true for it. Same goes for |
| 271 | the second element and is_runfiles_directory respectively. If both elements |
| 272 | are empty, then this function could not find a manifest or directory for |
| 273 | which is_runfiles_manifest or is_runfiles_directory returns true. |
| 274 | """ |
| 275 | mf_alid = is_runfiles_manifest(runfiles_mf) |
| 276 | dir_valid = is_runfiles_directory(runfiles_dir) |
| 277 | |
| 278 | if not mf_alid and not dir_valid: |
| 279 | runfiles_mf = argv0 + ".runfiles/MANIFEST" |
| 280 | runfiles_dir = argv0 + ".runfiles" |
| 281 | mf_alid = is_runfiles_manifest(runfiles_mf) |
| 282 | dir_valid = is_runfiles_directory(runfiles_dir) |
| 283 | if not mf_alid: |
| 284 | runfiles_mf = argv0 + ".runfiles_manifest" |
| 285 | mf_alid = is_runfiles_manifest(runfiles_mf) |
| 286 | |
| 287 | if not mf_alid and not dir_valid: |
| 288 | return ("", "") |
| 289 | |
| 290 | if not mf_alid: |
| 291 | runfiles_mf = runfiles_dir + "/MANIFEST" |
| 292 | mf_alid = is_runfiles_manifest(runfiles_mf) |
| 293 | if not mf_alid: |
| 294 | runfiles_mf = runfiles_dir + "_manifest" |
| 295 | mf_alid = is_runfiles_manifest(runfiles_mf) |
| 296 | |
| 297 | if not dir_valid: |
| 298 | runfiles_dir = runfiles_mf[:-9] # "_manifest" or "/MANIFEST" |
| 299 | dir_valid = is_runfiles_directory(runfiles_dir) |
| 300 | |
| 301 | return (runfiles_mf if mf_alid else "", runfiles_dir if dir_valid else "") |