|
12 | 12 | from functools import partial
|
13 | 13 | from pathlib import Path
|
14 | 14 |
|
| 15 | +import logistro |
| 16 | + |
| 17 | +_logger = logistro.getLogger(__name__) |
| 18 | + |
15 | 19 | # SOON TODO this isn't the right download path, look at uv, use sysconfig
|
16 | 20 | _default_download_path = Path(__file__).resolve().parent / "browser_exe"
|
17 | 21 |
|
18 | 22 | _chrome_for_testing_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
|
19 | 23 |
|
| 24 | + |
20 | 25 | _platforms = ["linux64", "win32", "win64", "mac-x64", "mac-arm64"]
|
21 | 26 |
|
22 | 27 | _arch_size_detected = "64" if sys.maxsize > 2**32 else "32"
|
@@ -69,20 +74,30 @@ def _extract_member(self, member, targetpath, pwd): # type: ignore [no-untyped-
|
69 | 74 |
|
70 | 75 | def get_chrome_sync(
|
71 | 76 | arch: str = _chrome_platform_detected,
|
72 |
| - i: int = -1, |
| 77 | + i: int | None = None, |
73 | 78 | path: str | Path = _default_download_path,
|
74 | 79 | *,
|
75 | 80 | verbose: bool = False,
|
76 | 81 | ) -> Path | str:
|
77 | 82 | """Download chrome synchronously: see `get_chrome()`."""
|
78 | 83 | if isinstance(path, str):
|
79 | 84 | path = Path(path)
|
80 |
| - browser_list = json.loads( |
81 |
| - urllib.request.urlopen( # noqa: S310 audit url for schemes |
82 |
| - _chrome_for_testing_url, |
83 |
| - ).read(), |
84 |
| - ) |
85 |
| - version_obj = browser_list["versions"][i] |
| 85 | + if i: |
| 86 | + _logger.info("Loading chrome from list") |
| 87 | + browser_list = json.loads( |
| 88 | + urllib.request.urlopen( # noqa: S310 audit url for schemes |
| 89 | + _chrome_for_testing_url, |
| 90 | + ).read(), |
| 91 | + ) |
| 92 | + version_obj = browser_list["versions"][i] |
| 93 | + else: |
| 94 | + _logger.info("Using last known good version of chrome") |
| 95 | + with ( |
| 96 | + Path(__file__).resolve().parent.parent |
| 97 | + / "resources" |
| 98 | + / "last_known_good_chrome.json" |
| 99 | + ).open() as f: |
| 100 | + version_obj = json.load(f) |
86 | 101 | if verbose:
|
87 | 102 | print(version_obj["version"]) # noqa: T201 allow print in cli
|
88 | 103 | print(version_obj["revision"]) # noqa: T201 allow print in cli
|
@@ -119,7 +134,7 @@ def get_chrome_sync(
|
119 | 134 |
|
120 | 135 | async def get_chrome(
|
121 | 136 | arch: str = _chrome_platform_detected,
|
122 |
| - i: int = -1, |
| 137 | + i: int | None = None, |
123 | 138 | path: str | Path = _default_download_path,
|
124 | 139 | *,
|
125 | 140 | verbose: bool = False,
|
@@ -153,17 +168,37 @@ def get_chrome_cli() -> None:
|
153 | 168 | " package manager.",
|
154 | 169 | UserWarning,
|
155 | 170 | )
|
156 |
| - parser = argparse.ArgumentParser(description="tool to help debug problems") |
157 |
| - parser.add_argument("--i", "-i", type=int, dest="i") |
158 |
| - parser.add_argument("--arch", dest="arch") |
159 |
| - parser.add_argument("--path", dest="path") |
| 171 | + parser = argparse.ArgumentParser( |
| 172 | + description="Will download Chrome for testing. All arguments optional.", |
| 173 | + parents=[logistro.parser], |
| 174 | + ) |
| 175 | + parser.add_argument( |
| 176 | + "--i", |
| 177 | + "-i", |
| 178 | + type=int, |
| 179 | + dest="i", |
| 180 | + help=( |
| 181 | + "Google offers thousands of chrome versions for download. " |
| 182 | + "'-i 0' is the oldest, '-i -1' is the newest: array syntax" |
| 183 | + ), |
| 184 | + ) |
| 185 | + parser.add_argument( |
| 186 | + "--arch", |
| 187 | + dest="arch", |
| 188 | + help="linux64|win32|win64|mac-x64|mac-arm64", |
| 189 | + ) |
| 190 | + parser.add_argument( |
| 191 | + "--path", |
| 192 | + dest="path", |
| 193 | + help="Where to store the download.", |
| 194 | + ) |
160 | 195 | parser.add_argument(
|
161 | 196 | "-v",
|
162 | 197 | "--verbose",
|
163 | 198 | dest="verbose",
|
164 | 199 | action="store_true",
|
| 200 | + help="Display found version number if using -i (to stdout)", |
165 | 201 | )
|
166 |
| - parser.set_defaults(i=-1) |
167 | 202 | parser.set_defaults(path=_default_download_path)
|
168 | 203 | parser.set_defaults(arch=_chrome_platform_detected)
|
169 | 204 | parser.set_defaults(verbose=False)
|
|
0 commit comments