File size: 2,158 Bytes
d10e42a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import traceback
from pathlib import Path

from playwright.async_api import async_playwright
from rich.console import Console

from utils.config import DEBUG, Environment, get_environment


PLAYWRIGHT_BROWSERS_PATH = "../chrome"
console = Console()


def _looks_like_bundled_windows_playwright(path: Path):
    if not path.exists():
        return False
    return any(p.name.startswith("chromium-") for p in path.iterdir())


def _configure_playwright_browser_path(env: Environment):
    # Respect explicit runtime override first.
    if os.getenv("PLAYWRIGHT_BROWSERS_PATH"):
        return

    # Hugging Face Docker should use browsers installed in the image.
    if env == Environment.HUGGINGFACE:
        return

    # Local mode may have a bundled playwright browser directory.
    if env == Environment.LOCAL:
        bundled = Path(__file__).resolve().parent / PLAYWRIGHT_BROWSERS_PATH
        # The repo bundles Windows binaries only. Avoid forcing this path on Linux/macOS.
        if os.name == "nt" and _looks_like_bundled_windows_playwright(bundled):
            os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(bundled.resolve())
        return

    # Packed mode keeps runtime assets near executable.
    if env == Environment.PACKED:
        packed = Path(sys.executable).resolve().parent / PLAYWRIGHT_BROWSERS_PATH
        if packed.exists():
            os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(packed.resolve())


async def get_browser(GUI=False):
    headless = True
    env = get_environment()
    _configure_playwright_browser_path(env)

    if env == Environment.LOCAL and DEBUG:
        headless = False
    if GUI:
        headless = False

    try:
        playwright = await async_playwright().start()
        browser = await playwright.chromium.launch(
            headless=headless,
            args=[
                "--no-sandbox",
                "--disable-dev-shm-usage",
                "--disable-gpu",
            ],
        )
        return playwright, browser
    except Exception:
        traceback.print_exc()
        raise