Skip to content

Commit d1b6a1b

Browse files
authored
fix(PermissionError): Make file executable before running the app. (microsoft#222)
1 parent b99dbf2 commit d1b6a1b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

playwright/main.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import asyncio
1616
import io
17-
import os
18-
import stat
1917
import subprocess
2018
import sys
2119
from pathlib import Path
@@ -27,7 +25,7 @@
2725
from playwright.connection import Connection
2826
from playwright.helper import Error
2927
from playwright.object_factory import create_remote_object
30-
from playwright.path_utils import get_file_dirname
28+
from playwright.path_utils import get_file_dirname, make_file_executable
3129
from playwright.playwright import Playwright
3230
from playwright.sync_api import Playwright as SyncPlaywright
3331
from playwright.sync_base import dispatcher_fiber, set_dispatcher_fiber
@@ -37,15 +35,19 @@ def compute_driver_executable() -> Path:
3735
package_path = get_file_dirname()
3836
platform = sys.platform
3937
if platform == "darwin":
40-
return package_path / "drivers" / "driver-darwin"
38+
path = package_path / "drivers" / "driver-darwin"
39+
return make_file_executable(path)
4140
elif platform == "linux":
42-
return package_path / "drivers" / "driver-linux"
41+
path = package_path / "drivers" / "driver-linux"
42+
return make_file_executable(path)
4343
elif platform == "win32":
4444
result = package_path / "drivers" / "driver-win32-amd64.exe"
4545
if result.exists():
4646
return result
4747
return package_path / "drivers" / "driver-win32.exe"
48-
return package_path / "drivers" / "driver-linux"
48+
49+
path = package_path / "drivers" / "driver-linux"
50+
return make_file_executable(path)
4951

5052

5153
async def run_driver_async() -> Connection:
@@ -140,11 +142,7 @@ def main() -> None:
140142
print('Run "python -m playwright install" to complete installation')
141143
return
142144
driver_executable = compute_driver_executable()
143-
# Fix the executable bit during the installation.
144-
if not sys.platform == "win32":
145-
st = os.stat(driver_executable)
146-
if st.st_mode & stat.S_IEXEC == 0:
147-
os.chmod(driver_executable, st.st_mode | stat.S_IEXEC)
145+
148146
print("Installing the browsers...")
149147
subprocess.check_call([str(driver_executable), "install"])
150148

playwright/path_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import stat
23
from pathlib import Path
34

45

@@ -8,3 +9,9 @@ def get_file_dirname() -> Path:
89
module = inspect.getmodule(frame[0])
910
assert module
1011
return Path(module.__file__).parent.absolute()
12+
13+
14+
def make_file_executable(file_path: Path) -> Path:
15+
"""Makes a file executable."""
16+
file_path.chmod(file_path.stat().st_mode | stat.S_IEXEC)
17+
return file_path

0 commit comments

Comments
 (0)