Philipp Schrader | 7520ee6 | 2022-12-10 14:04:40 -0800 | [diff] [blame] | 1 | commit 78913db0555d6b47449ca3cb7478c94663a1e553 |
| 2 | Author: Philipp Schrader <philipp.schrader@gmail.com> |
| 3 | Date: Thu Oct 27 20:57:23 2022 -0700 |
| 4 | |
| 5 | Make matplotlib hermetic |
| 6 | |
| 7 | diff --git a/site-packages/matplotlib/__init__.py b/site-packages/matplotlib/__init__.py |
| 8 | index ba9cd6c..083ef5b 100644 |
| 9 | --- a/site-packages/matplotlib/__init__.py |
| 10 | +++ b/site-packages/matplotlib/__init__.py |
| 11 | @@ -104,6 +104,46 @@ import warnings |
| 12 | import numpy |
| 13 | from packaging.version import parse as parse_version |
| 14 | |
| 15 | + |
| 16 | +from bazel_tools.tools.python.runfiles import runfiles |
| 17 | + |
| 18 | +def _hermeticity_fixup(): |
| 19 | + _runfiles = runfiles.Create() |
| 20 | + runfiles_dir = _runfiles.EnvVars()["RUNFILES_DIR"] |
| 21 | + if not runfiles_dir: |
| 22 | + raise FileNotFoundError("Failed runfiles lookup.") |
| 23 | + |
| 24 | + matplotlib_base = os.path.dirname(os.path.dirname(__file__)) |
| 25 | + |
| 26 | + # Hack to point matplotlib at its data. |
| 27 | + os.environ["MATPLOTLIBDATA"] = os.path.join(matplotlib_base, "matplotlib", "mpl-data") |
| 28 | + # Avoid reading /etc/matplotlib in all cases. Matplotlib is pretty happy to |
| 29 | + # escape the sandbox by using absolute paths. |
| 30 | + os.environ["MATPLOTLIBRC"] = os.path.join(os.environ["MATPLOTLIBDATA"], "matplotlibrc") |
| 31 | + # There's a bug where the temp directory gets set if MATPLOTLIBRC isn't set. |
| 32 | + # That causes the directory to not be created in time. We set the variable |
| 33 | + # manually here to work around the bug. |
| 34 | + os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib-nobody" |
| 35 | + |
| 36 | + gtk_runtime_base = os.path.join(runfiles_dir, "gtk_runtime") |
| 37 | + |
| 38 | + # Tell fontconfig where to find matplotlib's sandboxed font files. |
Austin Schuh | c2117c5 | 2024-09-02 13:19:54 -0700 | [diff] [blame] | 39 | + os.environ["FONTCONFIG_PATH"] = "etc/fonts" |
Philipp Schrader | 7520ee6 | 2022-12-10 14:04:40 -0800 | [diff] [blame] | 40 | + os.environ["FONTCONFIG_FILE"] = "fonts.conf" |
| 41 | + os.environ["FONTCONFIG_SYSROOT"] = gtk_runtime_base |
| 42 | + |
| 43 | + ld_library_path = os.getenv("LD_LIBRARY_PATH") or "" |
| 44 | + if ld_library_path: |
| 45 | + ld_library_path = ":" + ld_library_path |
| 46 | + |
| 47 | + os.environ["LD_LIBRARY_PATH"] = ":".join([ |
| 48 | + gtk_runtime_base + "/lib/x86_64-linux-gnu", |
| 49 | + gtk_runtime_base + "/usr/lib/x86_64-linux-gnu", |
| 50 | + ]) + ld_library_path |
| 51 | + |
| 52 | +_hermeticity_fixup() |
| 53 | + |
| 54 | + |
| 55 | # cbook must import matplotlib only within function |
| 56 | # definitions, so it is safe to import from it here. |
| 57 | from . import _api, _version, cbook, docstring, rcsetup |
| 58 | @@ -524,7 +564,7 @@ def get_cachedir(): |
| 59 | @_logged_cached('matplotlib data path: %s') |
| 60 | def get_data_path(): |
| 61 | """Return the path to Matplotlib data.""" |
| 62 | - return str(Path(__file__).with_name("mpl-data")) |
| 63 | + return str(Path(os.environ["MATPLOTLIBDATA"])) |
| 64 | |
| 65 | |
| 66 | def matplotlib_fname(): |