|
25 | 25 |
|
26 | 26 | from playwright.async_api import Playwright as AsyncPlaywright |
27 | 27 | from playwright.connection import Connection |
| 28 | +from playwright.helper import not_installed_error |
28 | 29 | from playwright.object_factory import create_remote_object |
29 | 30 | from playwright.playwright import Playwright |
30 | 31 | from playwright.sync_api import Playwright as SyncPlaywright |
31 | 32 | from playwright.sync_base import dispatcher_fiber, set_dispatcher_fiber |
32 | 33 |
|
33 | 34 |
|
34 | | -async def run_driver_async() -> Connection: |
35 | | - package_path = os.path.dirname(os.path.abspath(__file__)) |
| 35 | +def compute_driver_name() -> str: |
36 | 36 | platform = sys.platform |
37 | 37 | if platform == "darwin": |
38 | | - driver_name = "driver-macos" |
| 38 | + result = "driver-macos" |
39 | 39 | elif platform == "linux": |
40 | | - driver_name = "driver-linux" |
| 40 | + result = "driver-linux" |
41 | 41 | elif platform == "win32": |
42 | | - driver_name = "driver-win.exe" |
| 42 | + result = "driver-win.exe" |
| 43 | + return result |
| 44 | + |
| 45 | + |
| 46 | +async def run_driver_async() -> Connection: |
| 47 | + package_path = os.path.dirname(os.path.abspath(__file__)) |
| 48 | + driver_name = compute_driver_name() |
43 | 49 | driver_executable = os.path.join(package_path, driver_name) |
44 | 50 | archive_name = os.path.join(package_path, "drivers", driver_name + ".gz") |
45 | 51 |
|
46 | 52 | if not os.path.exists(driver_executable) or os.path.getmtime( |
47 | 53 | driver_executable |
48 | 54 | ) < os.path.getmtime(archive_name): |
49 | | - with gzip.open(archive_name, "rb") as f_in, open( |
50 | | - driver_executable, "wb" |
51 | | - ) as f_out: |
52 | | - shutil.copyfileobj(f_in, f_out) |
53 | | - |
54 | | - st = os.stat(driver_executable) |
55 | | - if st.st_mode & stat.S_IEXEC == 0: |
56 | | - os.chmod(driver_executable, st.st_mode | stat.S_IEXEC) |
57 | | - |
58 | | - subprocess.run(f"{driver_executable} install", shell=True) |
| 55 | + raise not_installed_error( |
| 56 | + "Playwright requires additional post-installation step to be made." |
| 57 | + ) |
59 | 58 |
|
60 | 59 | proc = await asyncio.create_subprocess_exec( |
61 | 60 | driver_executable, |
@@ -116,3 +115,32 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: |
116 | 115 | # Use ProactorEventLoop in 3.7, which is default in 3.8 |
117 | 116 | loop = asyncio.ProactorEventLoop() |
118 | 117 | asyncio.set_event_loop(loop) |
| 118 | + |
| 119 | + |
| 120 | +def main() -> None: |
| 121 | + if "install" not in sys.argv: |
| 122 | + print('Run "python -m playwright install" to complete installation') |
| 123 | + return |
| 124 | + package_path = os.path.dirname(os.path.abspath(__file__)) |
| 125 | + driver_name = compute_driver_name() |
| 126 | + driver_executable = os.path.join(package_path, driver_name) |
| 127 | + archive_name = os.path.join(package_path, "drivers", driver_name + ".gz") |
| 128 | + |
| 129 | + if not os.path.exists(driver_executable) or os.path.getmtime( |
| 130 | + driver_executable |
| 131 | + ) < os.path.getmtime(archive_name): |
| 132 | + print(f"Extracting {archive_name} into {driver_executable}...") |
| 133 | + with gzip.open(archive_name, "rb") as f_in, open( |
| 134 | + driver_executable, "wb" |
| 135 | + ) as f_out: |
| 136 | + shutil.copyfileobj(f_in, f_out) |
| 137 | + |
| 138 | + st = os.stat(driver_executable) |
| 139 | + if st.st_mode & stat.S_IEXEC == 0: |
| 140 | + print(f"Making {driver_executable} executable...") |
| 141 | + os.chmod(driver_executable, st.st_mode | stat.S_IEXEC) |
| 142 | + |
| 143 | + print("Installing the browsers...") |
| 144 | + subprocess.run(f"{driver_executable} install", shell=True) |
| 145 | + |
| 146 | + print("Playwright is now ready for use") |
0 commit comments