blob: 6fc460a3394e5e62d5a225e097175443af81c893 [file] [log] [blame]
Philipp Schrader7520ee62022-12-10 14:04:40 -08001commit 78913db0555d6b47449ca3cb7478c94663a1e553
2Author: Philipp Schrader <philipp.schrader@gmail.com>
3Date: Thu Oct 27 20:57:23 2022 -0700
4
5 Make matplotlib hermetic
6
7diff --git a/site-packages/matplotlib/__init__.py b/site-packages/matplotlib/__init__.py
8index 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 Schuhc2117c52024-09-02 13:19:54 -070039+ os.environ["FONTCONFIG_PATH"] = "etc/fonts"
Philipp Schrader7520ee62022-12-10 14:04:40 -080040+ 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():