Philipp Schrader | 7520ee6 | 2022-12-10 14:04:40 -0800 | [diff] [blame] | 1 | commit fe03a5f1aa619f4c1208ca1dce7d98db513dbec8 |
| 2 | Author: Philipp Schrader <philipp.schrader@gmail.com> |
| 3 | Date: Thu Oct 27 20:28:03 2022 -0700 |
| 4 | |
| 5 | Make pygobject hermetic |
| 6 | |
| 7 | diff --git a/site-packages/gi/__init__.py b/site-packages/gi/__init__.py |
| 8 | index 3454790..a6f3246 100644 |
| 9 | --- a/site-packages/gi/__init__.py |
| 10 | +++ b/site-packages/gi/__init__.py |
| 11 | @@ -27,6 +27,49 @@ import os |
| 12 | import importlib |
| 13 | import types |
| 14 | |
| 15 | +from bazel_tools.tools.python.runfiles import runfiles |
| 16 | + |
| 17 | +def _hermeticity_fixup(): |
| 18 | + _runfiles = runfiles.Create() |
| 19 | + runfiles_dir = _runfiles.EnvVars()["RUNFILES_DIR"] |
| 20 | + if not runfiles_dir: |
| 21 | + raise FileNotFoundError("Failed runfiles lookup.") |
| 22 | + |
| 23 | + gtk_runtime_dir = os.path.join(runfiles_dir, "gtk_runtime") |
| 24 | + |
| 25 | + ld_library_path = os.getenv("LD_LIBRARY_PATH") or "" |
| 26 | + if ld_library_path: |
| 27 | + ld_library_path = ":" + ld_library_path |
| 28 | + |
| 29 | + os.environ["LD_LIBRARY_PATH"] = ":".join([ |
| 30 | + gtk_runtime_dir + "/lib/x86_64-linux-gnu", |
| 31 | + gtk_runtime_dir + "/usr/lib/x86_64-linux-gnu", |
| 32 | + ]) + ld_library_path |
| 33 | + |
| 34 | + os.environ["GI_GIR_PATH"] = os.path.join(gtk_runtime_dir, "usr", "share", "gir-1.0") |
| 35 | + os.environ["GI_TYPELIB_PATH"] = os.path.join(gtk_runtime_dir, "usr", "lib", |
| 36 | + "x86_64-linux-gnu", |
| 37 | + "girepository-1.0") |
| 38 | + |
| 39 | + os.environ["GIO_EXTRA_MODULES"] = os.path.join(gtk_runtime_dir, "usr", "lib", "x86_64-linux-gnu", "gio", "modules") |
| 40 | + |
| 41 | + # Tell fontconfig where to find the sandboxed font files. |
| 42 | + os.environ["FONTCONFIG_PATH"] = os.path.join(gtk_runtime_dir, "etc/fonts/") |
| 43 | + os.environ["FONTCONFIG_FILE"] = os.path.join(gtk_runtime_dir, "etc/fonts/fonts.conf") |
| 44 | + # The sysroot here needs to be "/". If it were _base, then the font caches |
| 45 | + # would contain _base-relative paths in them. Unfortunately pango interprets |
| 46 | + # those as absolute paths and ends up failing to find all fonts. |
| 47 | + os.environ["FONTCONFIG_SYSROOT"] = "/" |
| 48 | + os.environ["GDK_PIXBUF_MODULEDIR"] = os.path.join(gtk_runtime_dir, "usr", "lib", |
| 49 | + "x86_64-linux-gnu", "gdk-pixbuf-2.0", |
| 50 | + "2.10.0", "loaders") |
| 51 | + os.environ["GDK_PIXBUF_MODULE_FILE"] = os.path.join(os.environ["GDK_PIXBUF_MODULEDIR"], "loaders.cache") |
| 52 | + os.system(os.path.join(gtk_runtime_dir, "usr/lib/x86_64-linux-gnu/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders") + " --update-cache") |
| 53 | + |
| 54 | + |
| 55 | +_hermeticity_fixup() |
| 56 | + |
| 57 | + |
| 58 | _static_binding_error = ('When using gi.repository you must not import static ' |
| 59 | 'modules like "gobject". Please change all occurrences ' |
| 60 | 'of "import gobject" to "from gi.repository import GObject". ' |