Skip to content

Commit c4e79dc

Browse files
committed
Redo chromium.py changes?
1 parent 7a3ae8a commit c4e79dc

File tree

2 files changed

+71
-26
lines changed

2 files changed

+71
-26
lines changed

choreographer/_browsers/chromium.py

+69-24
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,121 @@
22

33
import os
44
import platform
5+
import subprocess
56
import sys
67
from pathlib import Path
78

89
if platform.system() == "Windows":
910
import msvcrt
1011

1112
from choreographer._channels import Pipe
12-
from choreographer._sys_utils import get_browser_path
13+
from choreographer._sys_utils import TmpDirectory, get_browser_path
1314

15+
from ._chrome_constants import chrome_names, typical_chrome_paths
1416

15-
class Chromium:
16-
def __init__(self, channel):
17-
self._channel = channel
18-
if not isinstance(channel, Pipe):
19-
raise NotImplementedError("Websocket style channels not implemented yet")
17+
chromium_wrapper_path = Path(__file__).resolve().parent / "chromium_wrapper.py"
18+
19+
20+
def _is_exe(path):
21+
try:
22+
return os.access(path, os.X_OK)
23+
except: # noqa: E722 bare except ok, weird errors, best effort.
24+
return False
2025

21-
# where do we get user data dir
22-
def get_cli(self, temp_dir, **kwargs):
23-
gpu_enabled = kwargs.pop("with_gpu", False)
24-
headless = kwargs.pop("headless", True)
25-
sandbox = kwargs.pop("with_sandbox", False)
26+
27+
class Chromium:
28+
def __init__(self, channel, **kwargs):
29+
self.gpu_enabled = kwargs.pop("with_gpu", False)
30+
self.headless = kwargs.pop("headless", True)
31+
self.sandbox_enabled = kwargs.pop("with_sandbox", False)
32+
self.path = kwargs.pop("path", None)
33+
self._tmp_dir_path = kwargs.pop("tmp_dir", None)
34+
self.skip_local = bool(
35+
"ubuntu" in platform.version().lower() and self.enable_sandbox,
36+
)
2637
if kwargs:
2738
raise RuntimeError(
2839
"Chromium.get_cli() received " f"invalid args: {kwargs.keys()}",
2940
)
30-
path = get_browser_path()
31-
if not path:
32-
raise RuntimeError("Browser not found.")
3341

34-
chromium_wrapper_path = Path(__file__).resolve().parent / "chromium_wrapper.py"
42+
if not self.path:
43+
self.path = get_browser_path(chrome_names, skip_local=self.skip_local)
44+
if not self.path:
45+
# do typical chrome paths
46+
for candidate in typical_chrome_paths:
47+
if _is_exe(candidate):
48+
self.path = candidate
49+
break
50+
if not self.path:
51+
raise RuntimeError(
52+
"Browser not found. You can use get_chrome(), "
53+
"please see documentation.",
54+
)
55+
self._channel = channel
56+
if not isinstance(channel, Pipe):
57+
raise NotImplementedError("Websocket style channels not implemented yet")
58+
59+
self.tmp_dir = TmpDirectory(
60+
path=self._tmp_dir_path,
61+
sneak="snap" in self.path,
62+
)
63+
64+
def get_popen_args(self):
65+
args = {}
66+
# need to check pipe
67+
if platform.system() == "Windows":
68+
args["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
69+
args["close_fds"] = False
70+
else:
71+
args["close_fds"] = True
72+
if isinstance(self.channel, Pipe):
73+
args["stdin"] = self.channel.from_choreo_to_external
74+
args["stdout"] = self.channel.from_external_to_choreo
75+
76+
def get_cli(self):
3577
if platform.system() != "Windows":
3678
cli = [
3779
sys.executable,
3880
chromium_wrapper_path,
39-
path,
81+
self.path,
4082
]
4183
else:
4284
cli = [
43-
path,
85+
self.path,
4486
]
4587

4688
cli.extend(
4789
[
4890
"--disable-breakpad",
4991
"--allow-file-access-from-files",
5092
"--enable-logging=stderr",
51-
f"--user-data-dir={temp_dir}",
93+
f"--user-data-dir={self.tmp_dir.name}",
5294
"--no-first-run",
5395
"--enable-unsafe-swiftshader",
5496
],
5597
)
56-
if not gpu_enabled:
98+
if not self.gpu_enabled:
5799
cli.append("--disable-gpu")
58-
if headless:
100+
if self.headless:
59101
cli.append("--headless")
60-
if not sandbox:
102+
if not self.sandbox_enabled:
61103
cli.append("--no-sandbox")
62104

63105
if isinstance(self._channel, Pipe):
64106
cli.append("--remote-debugging-pipe")
65107
if platform.system() == "Windows":
66-
_inheritable = True
67108
w_handle = msvcrt.get_osfhandle(self._channel.from_choreo_to_external)
68109
r_handle = msvcrt.get_osfhandle(self._channel.from_external_to_choreo)
110+
_inheritable = True
69111
os.set_handle_inheritable(w_handle, _inheritable)
70112
os.set_handle_inheritable(r_handle, _inheritable)
71113
cli += [
72114
f"--remote-debugging-io-pipes={r_handle!s},{w_handle!s}",
73115
]
116+
return cli
74117

118+
def get_env():
119+
return os.environ().copy()
75120

76-
def get_env():
77-
return os.environ().copy()
121+
def clean():
122+
raise ValueError("Look at tempdir")

choreographer/_sys_utils/_which.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ def browser_which(executable_names, *, skip_local=False):
6464
return None
6565

6666

67-
def get_browser_path(**kwargs):
68-
return os.environ.get("BROWSER_PATH", browser_which(**kwargs))
67+
def get_browser_path(*args, **kwargs):
68+
return os.environ.get("BROWSER_PATH", browser_which(*args, **kwargs))

0 commit comments

Comments
 (0)