forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
82 lines (68 loc) · 2.43 KB
/
Copy pathsetup.py
File metadata and controls
82 lines (68 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import setup
try:
from wheel.bdist_wheel import bdist_wheel as BDistWheelCommand
except ImportError:
BDistWheelCommand = None
PLAYWRIGHT_NODE_REPO = "https://github.com/jacobsparts/playwright.git"
PLAYWRIGHT_NODE_BRANCH = "main"
def _ensure_driver(playwright_pkg_dir: Path) -> None:
"""Clone, build, and install the custom Node.js playwright driver."""
driver_dir = playwright_pkg_dir / "driver"
driver_dir.mkdir(parents=True, exist_ok=True)
build_dir = Path(
os.environ.get(
"PLAYWRIGHT_NODE_BUILD_DIR",
str(playwright_pkg_dir.parent / "_playwright_node_build"),
)
)
if not (build_dir / "package.json").exists():
print(f"Cloning {PLAYWRIGHT_NODE_REPO} into {build_dir}...")
subprocess.check_call(
[
"git",
"clone",
"--depth",
"1",
"--branch",
PLAYWRIGHT_NODE_BRANCH,
PLAYWRIGHT_NODE_REPO,
str(build_dir),
]
)
core_pkg = build_dir / "packages" / "playwright-core"
if not (core_pkg / "lib").exists():
print("Running npm install...")
subprocess.check_call(["npm", "install"], cwd=str(build_dir))
print("Running npm run build...")
subprocess.check_call(["npm", "run", "build"], cwd=str(build_dir))
# Copy playwright-core into the wheel
package_dest = driver_dir / "package"
if package_dest.exists() or package_dest.is_symlink():
if package_dest.is_symlink():
package_dest.unlink()
else:
shutil.rmtree(str(package_dest))
shutil.copytree(str(core_pkg), str(package_dest))
# Symlink node binary
node_dest = driver_dir / "node"
if node_dest.exists() or node_dest.is_symlink():
node_dest.unlink()
node_path = shutil.which("node")
if not node_path:
raise RuntimeError("node not found on PATH. Install Node.js >= 18 first.")
node_dest.symlink_to(node_path)
print(f"Driver ready at {driver_dir}")
if BDistWheelCommand:
class CustomBDistWheel(BDistWheelCommand):
def run(self):
playwright_pkg_dir = Path(__file__).parent / "playwright"
_ensure_driver(playwright_pkg_dir)
super().run()
setup(cmdclass={"bdist_wheel": CustomBDistWheel})
else:
setup()