hoodini.utils.browser_setup

Browser setup utilities for installing and verifying Playwright dependencies.

This module ensures Playwright Firefox is available before use.

 1"""
 2Browser setup utilities for installing and verifying Playwright dependencies.
 3
 4This module ensures Playwright Firefox is available before use.
 5"""
 6
 7import asyncio
 8import subprocess
 9import sys
10
11
12def ensure_playwright_firefox() -> bool:
13    """
14    Ensure Playwright Firefox is installed.
15
16    Automatically installs Firefox if not already present.
17    This should be called once per environment during initial setup.
18
19    Returns:
20        True if Firefox is available or was successfully installed, False otherwise.
21    """
22    try:
23        # Try to import playwright first
24        from playwright.async_api import async_playwright
25
26        # Check if Firefox binary exists by attempting to launch it
27        async def check_firefox():
28            try:
29                async with async_playwright() as p:
30                    browser = await p.firefox.launch(headless=True)
31                    await browser.close()
32                    return True
33            except Exception:
34                return False
35
36        if asyncio.run(check_firefox()):
37            return True
38    except Exception:
39        pass
40
41    # Firefox not available, attempt to install
42    print("Installing Playwright Firefox browser...")
43    try:
44        subprocess.run(
45            [sys.executable, "-m", "playwright", "install", "firefox"],
46            check=True,
47            capture_output=True,
48            text=True,
49        )
50        print("✓ Playwright Firefox installed successfully")
51        return True
52    except subprocess.CalledProcessError as e:
53        print(f"✗ Failed to install Playwright Firefox:\n{e.stderr}")
54        return False
55
56
57__all__ = ["ensure_playwright_firefox"]
def ensure_playwright_firefox() -> bool:
13def ensure_playwright_firefox() -> bool:
14    """
15    Ensure Playwright Firefox is installed.
16
17    Automatically installs Firefox if not already present.
18    This should be called once per environment during initial setup.
19
20    Returns:
21        True if Firefox is available or was successfully installed, False otherwise.
22    """
23    try:
24        # Try to import playwright first
25        from playwright.async_api import async_playwright
26
27        # Check if Firefox binary exists by attempting to launch it
28        async def check_firefox():
29            try:
30                async with async_playwright() as p:
31                    browser = await p.firefox.launch(headless=True)
32                    await browser.close()
33                    return True
34            except Exception:
35                return False
36
37        if asyncio.run(check_firefox()):
38            return True
39    except Exception:
40        pass
41
42    # Firefox not available, attempt to install
43    print("Installing Playwright Firefox browser...")
44    try:
45        subprocess.run(
46            [sys.executable, "-m", "playwright", "install", "firefox"],
47            check=True,
48            capture_output=True,
49            text=True,
50        )
51        print("✓ Playwright Firefox installed successfully")
52        return True
53    except subprocess.CalledProcessError as e:
54        print(f"✗ Failed to install Playwright Firefox:\n{e.stderr}")
55        return False

Ensure Playwright Firefox is installed.

Automatically installs Firefox if not already present. This should be called once per environment during initial setup.

Returns: True if Firefox is available or was successfully installed, False otherwise.