fix: ensure bench setup-chrome respects site config path for chromium

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
Akhil Narang 2025-12-31 16:49:23 +05:30
parent e7598ec3a8
commit 19ccfcc453
No known key found for this signature in database
GPG key ID: 9DCC61E211BF645F
2 changed files with 6 additions and 36 deletions

View file

@ -9,17 +9,12 @@ import requests
import frappe
from frappe import _
from frappe.utils.print_utils import find_or_download_chromium_executable
# TODO: close browser when worker is killed.
class ChromePDFGenerator:
EXECUTABLE_PATHS: ClassVar[dict[str, list[str]]] = {
"linux": ["chrome-linux", "headless_shell"],
"darwin": ["chrome-mac", "headless_shell"],
"windows": ["chrome-win", "headless_shell.exe"],
}
_instance = None
_browsers: ClassVar[list] = []
@ -62,8 +57,6 @@ class ChromePDFGenerator:
self._devtools_url = self.CHROMIUM_WEBSOCKET_URL
return
# only when we want to use chromium from a specific path ( incase we don't have chromium in bench folder )
self.CHROMIUM_BINARY_PATH = site_config.get("chromium_binary_path", "")
"""
Number of allowed open websocket connections to chromium.
This number will basically define how many concurrent requests can be handled by one chromium instance.
@ -77,38 +70,11 @@ class ChromePDFGenerator:
# time to wait for chromium to start and provide dev tools url used in _set_devtools_url.
self.START_TIMEOUT = site_config.get("chromium_start_timeout", 3)
self._chromium_path = (
self._find_chromium_executable() if not self.CHROMIUM_BINARY_PATH else self.CHROMIUM_BINARY_PATH
)
self._chromium_path = find_or_download_chromium_executable()
if self._verify_chromium_installation():
if not self._devtools_url:
self.start_chromium_process()
def _find_chromium_executable(self):
"""Finds the Chromium executable or raises an error if not found."""
bench_path = frappe.utils.get_bench_path()
"""Determine the path to the Chromium executable. chromium is downloaded by download_chromium in print_designer/install.py"""
chromium_dir = os.path.join(bench_path, "chromium")
if not os.path.exists(chromium_dir):
frappe.throw(_("Chromium is not downloaded. Please run the setup first."))
platform_name = platform.system().lower()
if platform_name not in ["linux", "darwin", "windows"]:
frappe.throw(f"Unsupported platform: {platform_name}")
executable_name = self.EXECUTABLE_PATHS.get(platform_name)
# Construct the full path to the executable
exec_path = Path(chromium_dir).joinpath(*executable_name)
if not exec_path.exists():
frappe.throw(
f"Chromium executable not found: {exec_path}. please run bench setup-new-pdf-backend"
)
return str(exec_path)
def _verify_chromium_installation(self):
"""Ensures Chromium is available and executable, raising clearer errors if not."""
if not os.path.exists(self._chromium_path):

View file

@ -172,8 +172,12 @@ def setup_chromium():
def find_or_download_chromium_executable():
"""Finds the Chromium executable or downloads if not found."""
import platform
import shutil
from pathlib import Path
if chromium_path := shutil.which(frappe.get_common_site_config().chromium_path):
return chromium_path
bench_path = frappe.utils.get_bench_path()
"""Determine the path to the Chromium executable."""
chromium_dir = os.path.join(bench_path, "chromium")