|
| 1 | +# bazel-runfiles library |
| 2 | + |
| 3 | +This is a Bazel Runfiles lookup library for Bazel-built Python binaries and tests. |
| 4 | + |
| 5 | +Typical Usage |
| 6 | +------------- |
| 7 | + |
| 8 | +1. Add the 'runfiles' dependency along with other third-party dependencies, for example in your |
| 9 | + `requirements.txt` file. |
| 10 | + |
| 11 | +2. Depend on this runfiles library from your build rule, like you would other third-party libraries. |
| 12 | + |
| 13 | + py_binary( |
| 14 | + name = "my_binary", |
| 15 | + ... |
| 16 | + deps = [requirement("runfiles")], |
| 17 | + ) |
| 18 | + |
| 19 | +3. Import the runfiles library. |
| 20 | + |
| 21 | + import runfiles # not "from runfiles import runfiles" |
| 22 | + |
| 23 | +4. Create a Runfiles object and use rlocation to look up runfile paths: |
| 24 | + |
| 25 | + r = runfiles.Create() |
| 26 | + ... |
| 27 | + with open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f: |
| 28 | + contents = f.readlines() |
| 29 | + ... |
| 30 | + |
| 31 | + The code above creates a manifest- or directory-based implementations based |
| 32 | + on the environment variables in os.environ. See `Create()` for more info. |
| 33 | + |
| 34 | + If you want to explicitly create a manifest- or directory-based |
| 35 | + implementations, you can do so as follows: |
| 36 | + |
| 37 | + r1 = runfiles.CreateManifestBased("path/to/foo.runfiles_manifest") |
| 38 | + |
| 39 | + r2 = runfiles.CreateDirectoryBased("path/to/foo.runfiles/") |
| 40 | + |
| 41 | + If you wnat to start subprocesses, and the subprocess can't automatically |
| 42 | + find the correct runfiles directory, you can explicitly set the right |
| 43 | + environment variables for them: |
| 44 | + |
| 45 | + import subprocess |
| 46 | + import runfiles |
| 47 | + |
| 48 | + r = runfiles.Create() |
| 49 | + env = {} |
| 50 | + ... |
| 51 | + env.update(r.EnvVars()) |
| 52 | + p = subprocess.Popen([r.Rlocation("path/to/binary")], env, ...) |
0 commit comments