(args: argparse.Namespace)
| 105 | |
| 106 | |
| 107 | def _cmd_create(args: argparse.Namespace) -> int: |
| 108 | workspace_dir = args.workspace_dir or str(Path.cwd()) |
| 109 | out_path = _resolve_path(args.out, workspace_dir) |
| 110 | user_data_dir = _resolve_path( |
| 111 | args.user_data_dir or DEFAULT_USER_DATA_SUBDIR, workspace_dir |
| 112 | ) |
| 113 | out_path.parent.mkdir(parents=True, exist_ok=True) |
| 114 | user_data_dir.mkdir(parents=True, exist_ok=True) |
| 115 | |
| 116 | chromium = _chromium_executable() |
| 117 | chromium_args = [ |
| 118 | chromium, |
| 119 | "--remote-debugging-port=0", |
| 120 | f"--user-data-dir={user_data_dir}", |
| 121 | "--no-first-run", |
| 122 | "--no-default-browser-check", |
| 123 | "--disable-features=TranslateUI,MediaRouter", |
| 124 | f"--window-size={args.window_width},{args.window_height}", |
| 125 | ] |
| 126 | if args.headless: |
| 127 | chromium_args.append("--headless=new") |
| 128 | if args.no_sandbox: |
| 129 | chromium_args.append("--no-sandbox") |
| 130 | chromium_args.extend(args.chromium_arg or []) |
| 131 | |
| 132 | # Detach into its own process group so it survives the parent shell exit. |
| 133 | popen_kwargs: dict = { |
| 134 | "stdout": subprocess.DEVNULL, |
| 135 | "stderr": subprocess.PIPE, |
| 136 | "stdin": subprocess.DEVNULL, |
| 137 | "text": True, |
| 138 | "bufsize": 1, |
| 139 | "close_fds": True, |
| 140 | } |
| 141 | if os.name == "posix": |
| 142 | popen_kwargs["start_new_session"] = True |
| 143 | |
| 144 | proc = subprocess.Popen(chromium_args, **popen_kwargs) # noqa: S603 |
| 145 | try: |
| 146 | connect_url = _wait_for_devtools_url(proc, args.startup_timeout) |
| 147 | except SystemExit: |
| 148 | try: |
| 149 | proc.terminate() |
| 150 | except Exception: # noqa: BLE001 |
| 151 | pass |
| 152 | raise |
| 153 | |
| 154 | session = { |
| 155 | "id": uuid.uuid4().hex, |
| 156 | "pid": proc.pid, |
| 157 | "connectUrl": connect_url, |
| 158 | "userDataDir": str(user_data_dir), |
| 159 | "executablePath": chromium, |
| 160 | "headless": bool(args.headless), |
| 161 | "createdAt": int(time.time()), |
| 162 | } |
| 163 | out_path.write_text(json.dumps(session, indent=2) + "\n", encoding="utf-8") |
| 164 |
nothing calls this directly
no test coverage detected