|
1 | 1 | import numpy as np |
2 | 2 | from bokeh.io import export_png |
3 | 3 |
|
| 4 | +import atexit |
4 | 5 | import os |
| 6 | +import shutil |
5 | 7 | import logging |
6 | 8 |
|
| 9 | +from selenium import webdriver |
| 10 | + |
7 | 11 | logger = logging.getLogger('KCWI') |
8 | 12 |
|
| 13 | +_firefox_driver = None |
| 14 | + |
| 15 | + |
| 16 | +def configure_plot_driver(firefox_compat=False, prewarm=False): |
| 17 | + """Optionally pre-warm the Firefox WebDriver at pipeline startup. |
| 18 | +
|
| 19 | + Called by StartBokeh when both plot_firefox_compat and plot_prewarm_firefox |
| 20 | + are True in kcwi.cfg. Once the driver is created here, save_plot will |
| 21 | + automatically use the Firefox-compatible export path for all subsequent |
| 22 | + calls. |
| 23 | + """ |
| 24 | + if firefox_compat and prewarm: |
| 25 | + logger.info("Pre-warming Firefox WebDriver for plot export") |
| 26 | + _get_driver() |
| 27 | + |
| 28 | + |
| 29 | +def _get_driver(): |
| 30 | + """Return a cached Firefox WebDriver, creating it on first call.""" |
| 31 | + global _firefox_driver |
| 32 | + if _firefox_driver is not None: |
| 33 | + try: |
| 34 | + # Navigate to about:blank to cancel any pending navigation left by |
| 35 | + # Bokeh's reset step (it navigates to http://0.0.0.1/ after each |
| 36 | + # screenshot; on some networks that address times out rather than |
| 37 | + # refusing immediately, blocking the next export call). |
| 38 | + _firefox_driver.get("about:blank") |
| 39 | + return _firefox_driver |
| 40 | + except Exception: |
| 41 | + _firefox_driver = None |
| 42 | + |
| 43 | + from selenium.webdriver.firefox.service import Service as FirefoxService |
| 44 | + options = webdriver.FirefoxOptions() |
| 45 | + options.add_argument("--headless") |
| 46 | + options.add_argument("--no-sandbox") |
| 47 | + options.add_argument("--disable-dev-shm-usage") |
| 48 | + options.add_argument("--hide-scrollbars") |
| 49 | + options.add_argument("--force-device-scale-factor=1") |
| 50 | + options.add_argument("--force-color-profile=srgb") |
| 51 | + firefox_bin = _find_firefox_binary() |
| 52 | + if firefox_bin: |
| 53 | + options.binary_location = firefox_bin |
| 54 | + geckodriver = _find_geckodriver() |
| 55 | + serv = FirefoxService(executable_path=geckodriver) if geckodriver else FirefoxService() |
| 56 | + _firefox_driver = webdriver.Firefox(options=options, service=serv) |
| 57 | + atexit.register(_close_driver) |
| 58 | + return _firefox_driver |
| 59 | + |
| 60 | + |
| 61 | +def _close_driver(): |
| 62 | + global _firefox_driver |
| 63 | + if _firefox_driver is not None: |
| 64 | + try: |
| 65 | + _firefox_driver.quit() |
| 66 | + except Exception: |
| 67 | + pass |
| 68 | + _firefox_driver = None |
| 69 | + |
9 | 70 |
|
10 | 71 | def get_plot_lims(data, padding=0.05, clip=True): |
11 | 72 | """Get plot limits using data range plus padding fraction""" |
@@ -33,12 +94,78 @@ def set_plot_lims(fig, xlim=None, ylim=None): |
33 | 94 | fig.y_range.start = ylim[0] |
34 | 95 | fig.y_range.end = ylim[1] |
35 | 96 |
|
| 97 | +def _find_firefox_binary(): |
| 98 | + """Return path to the real Firefox binary, handling snap wrapper installs.""" |
| 99 | + # On Ubuntu 24.04, /usr/bin/firefox is a shell script wrapper for the snap. |
| 100 | + # Selenium requires the actual ELF binary, not a wrapper script. |
| 101 | + candidates = [ |
| 102 | + '/snap/firefox/current/usr/lib/firefox/firefox', |
| 103 | + '/usr/lib/firefox/firefox', |
| 104 | + ] |
| 105 | + for path in candidates: |
| 106 | + if os.path.isfile(path) and os.access(path, os.X_OK): |
| 107 | + return path |
| 108 | + # Fall back to whatever is on PATH (works on most non-snap installs) |
| 109 | + return shutil.which('firefox') |
| 110 | + |
| 111 | + |
| 112 | +def _find_geckodriver(): |
| 113 | + """Return path to geckodriver, searching common locations.""" |
| 114 | + candidates = [ |
| 115 | + '/snap/bin/geckodriver', |
| 116 | + '/usr/bin/geckodriver', |
| 117 | + '/usr/local/bin/geckodriver', |
| 118 | + ] |
| 119 | + for path in candidates: |
| 120 | + if os.path.isfile(path) and os.access(path, os.X_OK): |
| 121 | + return path |
| 122 | + return shutil.which('geckodriver') |
| 123 | + |
36 | 124 |
|
37 | 125 | def save_plot(fig, filename=None): |
| 126 | + """Save a Bokeh figure to a PNG file. |
| 127 | +
|
| 128 | + Dispatches to the Firefox-compatible path or the simple path depending on |
| 129 | + whether configure_plot_driver() was called with firefox_compat=True. |
| 130 | + """ |
| 131 | + if _firefox_driver is not None: |
| 132 | + _save_plot_firefox(fig, filename) |
| 133 | + else: |
| 134 | + _save_plot_simple(fig, filename) |
| 135 | + |
| 136 | + |
| 137 | +def _save_plot_firefox(fig, filename=None): |
| 138 | + """Firefox-compatible export for systems with snap-confined Firefox.""" |
38 | 139 | if filename is None: |
39 | 140 | fnam = os.path.join('plots', 'kcwi_drp_plot.png') |
40 | 141 | else: |
41 | 142 | fnam = os.path.join('plots', filename) |
42 | | - export_png(fig, filename=fnam) |
43 | 143 |
|
| 144 | + # Resolve to absolute path before chdir so the PNG lands in the right place. |
| 145 | + fnam = os.path.abspath(fnam) |
| 146 | + os.makedirs(os.path.dirname(fnam), exist_ok=True) |
| 147 | + |
| 148 | + driver = _get_driver() |
| 149 | + # Snap-confined Firefox on Ubuntu 24.04 uses a private /tmp mount namespace, |
| 150 | + # so file:///tmp/... URLs are invisible to it. $HOME is accessible via the |
| 151 | + # snap 'home' interface. Bokeh writes its scratch HTML relative to the |
| 152 | + # process CWD, so chdir to $HOME to land the temp file somewhere Firefox |
| 153 | + # can actually reach. fnam is already absolute so the PNG output goes to |
| 154 | + # the right place regardless. |
| 155 | + old_cwd = os.getcwd() |
| 156 | + try: |
| 157 | + os.chdir(os.path.expanduser('~')) |
| 158 | + export_png(fig, filename=fnam, webdriver=driver) |
| 159 | + finally: |
| 160 | + os.chdir(old_cwd) |
| 161 | + logger.info(">>> Saving to %s" % fnam) |
| 162 | + |
| 163 | + |
| 164 | +def _save_plot_simple(fig, filename=None): |
| 165 | + """Simple export using Bokeh's built-in browser detection.""" |
| 166 | + if filename is None: |
| 167 | + fnam = os.path.join('plots', 'kcwi_drp_plot.png') |
| 168 | + else: |
| 169 | + fnam = os.path.join('plots', filename) |
| 170 | + export_png(fig, filename=fnam) |
44 | 171 | logger.info(">>> Saving to %s" % fnam) |
0 commit comments