Skip to content

Commit 12de833

Browse files
committed
chore: convert setup.py to build
1 parent 1e1122c commit 12de833

File tree

4 files changed

+142
-145
lines changed

4 files changed

+142
-145
lines changed

.azure-pipelines/publish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ extends:
3939
python -m pip install --upgrade pip
4040
pip install -r local-requirements.txt
4141
pip install -e .
42-
python setup.py bdist_wheel --all
42+
for wheel in $(python setup.py --list-wheels); do
43+
PLAYWRIGHT_TARGET_WHEEL=$wheel python -m build --wheel
44+
done
4345
displayName: 'Install & Build'
4446
- task: EsrpRelease@7
4547
inputs:

local-requirements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
auditwheel==6.1.0
21
autobahn==23.1.2
32
black==24.8.0
3+
build==1.2.2.post1
44
flake8==7.1.1
55
flaky==3.8.1
66
mypy==1.13.0
@@ -17,8 +17,6 @@ pytest-timeout==2.3.1
1717
pytest-xdist==3.6.1
1818
requests==2.32.3
1919
service_identity==24.2.0
20-
setuptools==75.4.0
2120
twisted==24.10.0
2221
types-pyOpenSSL==24.1.0.20240722
2322
types-requests==2.32.0.20241016
24-
wheel==0.45.0

pyproject.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,54 @@
22
requires = ["setuptools==75.4.0", "setuptools-scm==8.1.0", "wheel==0.45.0", "auditwheel==6.1.0"]
33
build-backend = "setuptools.build_meta"
44

5+
[project]
6+
name = "playwright"
7+
description = "A high-level API to automate web browsers"
8+
authors = [
9+
{name = "Microsoft Corporation"}
10+
]
11+
readme = "README.md"
12+
license = {text = "Apache-2.0"}
13+
dynamic = ["version"]
14+
requires-python = ">=3.9"
15+
dependencies = [
16+
"greenlet==3.1.1",
17+
"pyee==12.0.0",
18+
]
19+
classifiers = [
20+
"Topic :: Software Development :: Testing",
21+
"Topic :: Internet :: WWW/HTTP :: Browsers",
22+
"Intended Audience :: Developers",
23+
"Programming Language :: Python :: 3",
24+
"Programming Language :: Python :: 3.9",
25+
"Programming Language :: Python :: 3.10",
26+
"Programming Language :: Python :: 3.11",
27+
"Programming Language :: Python :: 3.12",
28+
"Programming Language :: Python :: 3.13",
29+
"License :: OSI Approved :: Apache Software License",
30+
"Operating System :: OS Independent",
31+
]
32+
33+
[project.urls]
34+
homepage = "https://github.com/Microsoft/playwright-python"
35+
"Release notes" = "https://github.com/microsoft/playwright-python/releases"
36+
37+
[project.scripts]
38+
playwright = "playwright.__main__:main"
39+
40+
[project.entry-points.pyinstaller40]
41+
hook-dirs = "playwright._impl.__pyinstaller:get_hook_dirs"
42+
43+
[tool.setuptools]
44+
packages = [
45+
"playwright",
46+
"playwright.async_api",
47+
"playwright.sync_api",
48+
"playwright._impl",
49+
"playwright._impl.__pyinstaller",
50+
]
51+
include-package-data = true
52+
553
[tool.setuptools_scm]
654
version_file = "playwright/_repo_version.py"
755

setup.py

Lines changed: 90 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import subprocess
2020
import sys
2121
import zipfile
22-
from pathlib import Path
2322
from typing import Dict, List
2423

2524
from setuptools import setup
26-
2725
try:
2826
from auditwheel.wheeltools import InWheel
2927
except ImportError:
@@ -60,132 +58,123 @@ def download_driver(zip_name: str) -> None:
6058

6159

6260
class PlaywrightBDistWheelCommand(BDistWheelCommand):
63-
user_options = BDistWheelCommand.user_options + [
64-
("all", "a", "create wheels for all platforms")
61+
base_wheel_bundles: List[Dict[str, str]] = [
62+
{
63+
"wheel": "macosx_10_13_x86_64.whl",
64+
"machine": "x86_64",
65+
"platform": "darwin",
66+
"zip_name": "mac",
67+
},
68+
{
69+
"wheel": "macosx_11_0_universal2.whl",
70+
"machine": "x86_64",
71+
"platform": "darwin",
72+
"zip_name": "mac",
73+
},
74+
{
75+
"wheel": "macosx_11_0_arm64.whl",
76+
"machine": "arm64",
77+
"platform": "darwin",
78+
"zip_name": "mac-arm64",
79+
},
80+
{
81+
"wheel": "manylinux1_x86_64.whl",
82+
"machine": "x86_64",
83+
"platform": "linux",
84+
"zip_name": "linux",
85+
},
86+
{
87+
"wheel": "manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
88+
"machine": "aarch64",
89+
"platform": "linux",
90+
"zip_name": "linux-arm64",
91+
},
92+
{
93+
"wheel": "win32.whl",
94+
"machine": "i386",
95+
"platform": "win32",
96+
"zip_name": "win32_x64",
97+
},
98+
{
99+
"wheel": "win_amd64.whl",
100+
"machine": "amd64",
101+
"platform": "win32",
102+
"zip_name": "win32_x64",
103+
},
65104
]
66-
boolean_options = BDistWheelCommand.boolean_options + ["all"]
67-
68-
def initialize_options(self) -> None:
69-
super().initialize_options()
70-
self.all = False
71105

72106
def run(self) -> None:
73-
shutil.rmtree("build", ignore_errors=True)
74-
shutil.rmtree("dist", ignore_errors=True)
75-
shutil.rmtree("playwright.egg-info", ignore_errors=True)
76107
super().run()
77108
os.makedirs("driver", exist_ok=True)
78109
os.makedirs("playwright/driver", exist_ok=True)
79-
base_wheel_bundles: List[Dict[str, str]] = [
80-
{
81-
"wheel": "macosx_10_13_x86_64.whl",
82-
"machine": "x86_64",
83-
"platform": "darwin",
84-
"zip_name": "mac",
85-
},
86-
{
87-
"wheel": "macosx_11_0_universal2.whl",
88-
"machine": "x86_64",
89-
"platform": "darwin",
90-
"zip_name": "mac",
91-
},
92-
{
93-
"wheel": "macosx_11_0_arm64.whl",
94-
"machine": "arm64",
95-
"platform": "darwin",
96-
"zip_name": "mac-arm64",
97-
},
98-
{
99-
"wheel": "manylinux1_x86_64.whl",
100-
"machine": "x86_64",
101-
"platform": "linux",
102-
"zip_name": "linux",
103-
},
104-
{
105-
"wheel": "manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
106-
"machine": "aarch64",
107-
"platform": "linux",
108-
"zip_name": "linux-arm64",
109-
},
110-
{
111-
"wheel": "win32.whl",
112-
"machine": "i386",
113-
"platform": "win32",
114-
"zip_name": "win32_x64",
115-
},
116-
{
117-
"wheel": "win_amd64.whl",
118-
"machine": "amd64",
119-
"platform": "win32",
120-
"zip_name": "win32_x64",
121-
},
122-
]
123-
self._download_and_extract_local_driver(base_wheel_bundles)
124-
125-
wheels = base_wheel_bundles
126-
if not self.all:
127-
# Limit to 1, since for MacOS e.g. we have multiple wheels for the same platform and architecture and Conda expects 1.
128-
wheels = list(
110+
self._download_and_extract_local_driver()
111+
112+
wheel = None
113+
if os.getenv("PLAYWRIGHT_TARGET_WHEEL", None):
114+
wheel = list(
115+
filter(
116+
lambda wheel: wheel["wheel"]
117+
== os.getenv("PLAYWRIGHT_TARGET_WHEEL"),
118+
self.base_wheel_bundles,
119+
)
120+
)[0]
121+
else:
122+
wheel = list(
129123
filter(
130124
lambda wheel: wheel["platform"] == sys.platform
131125
and wheel["machine"] == platform.machine().lower(),
132-
base_wheel_bundles,
126+
self.base_wheel_bundles,
133127
)
134-
)[:1]
135-
self._build_wheels(wheels)
128+
)[0]
129+
assert wheel
130+
self._build_wheel(wheel)
136131

137-
def _build_wheels(
132+
def _build_wheel(
138133
self,
139-
wheels: List[Dict[str, str]],
134+
wheel_bundle: Dict[str, str],
140135
) -> None:
136+
assert self.dist_dir
141137
base_wheel_location: str = glob.glob(os.path.join(self.dist_dir, "*.whl"))[0]
142138
without_platform = base_wheel_location[:-7]
143-
for wheel_bundle in wheels:
144-
download_driver(wheel_bundle["zip_name"])
145-
zip_file = (
146-
f"driver/playwright-{driver_version}-{wheel_bundle['zip_name']}.zip"
139+
download_driver(wheel_bundle["zip_name"])
140+
zip_file = f"driver/playwright-{driver_version}-{wheel_bundle['zip_name']}.zip"
141+
with zipfile.ZipFile(zip_file, "r") as zip:
142+
extractall(zip, f"driver/{wheel_bundle['zip_name']}")
143+
wheel_location = without_platform + wheel_bundle["wheel"]
144+
shutil.copy(base_wheel_location, wheel_location)
145+
with zipfile.ZipFile(wheel_location, "a") as zip:
146+
driver_root = os.path.abspath(f"driver/{wheel_bundle['zip_name']}")
147+
for dir_path, _, files in os.walk(driver_root):
148+
for file in files:
149+
from_path = os.path.join(dir_path, file)
150+
to_path = os.path.relpath(from_path, driver_root)
151+
zip.write(from_path, f"playwright/driver/{to_path}")
152+
zip.writestr(
153+
"playwright/driver/README.md",
154+
f"{wheel_bundle['wheel']} driver package",
147155
)
148-
with zipfile.ZipFile(zip_file, "r") as zip:
149-
extractall(zip, f"driver/{wheel_bundle['zip_name']}")
150-
wheel_location = without_platform + wheel_bundle["wheel"]
151-
shutil.copy(base_wheel_location, wheel_location)
152-
with zipfile.ZipFile(wheel_location, "a") as zip:
153-
driver_root = os.path.abspath(f"driver/{wheel_bundle['zip_name']}")
154-
for dir_path, _, files in os.walk(driver_root):
155-
for file in files:
156-
from_path = os.path.join(dir_path, file)
157-
to_path = os.path.relpath(from_path, driver_root)
158-
zip.write(from_path, f"playwright/driver/{to_path}")
159-
zip.writestr(
160-
"playwright/driver/README.md",
161-
f"{wheel_bundle['wheel']} driver package",
162-
)
163156
os.remove(base_wheel_location)
164-
if InWheel:
165-
for whlfile in glob.glob(os.path.join(self.dist_dir, "*.whl")):
166-
os.makedirs("wheelhouse", exist_ok=True)
157+
for whlfile in glob.glob(os.path.join(self.dist_dir, "*.whl")):
158+
os.makedirs("wheelhouse", exist_ok=True)
159+
if InWheel:
167160
with InWheel(
168161
in_wheel=whlfile,
169162
out_wheel=os.path.join("wheelhouse", os.path.basename(whlfile)),
170163
):
171164
print(f"Updating RECORD file of {whlfile}")
172-
shutil.rmtree(self.dist_dir)
173-
print("Copying new wheels")
174-
shutil.move("wheelhouse", self.dist_dir)
175-
else:
176-
print("auditwheel not installed, not updating RECORD file")
165+
print("Copying new wheels")
166+
shutil.move("wheelhouse", self.dist_dir)
177167

178168
def _download_and_extract_local_driver(
179169
self,
180-
wheels: List[Dict[str, str]],
181170
) -> None:
182171
zip_names_for_current_system = set(
183172
map(
184173
lambda wheel: wheel["zip_name"],
185174
filter(
186175
lambda wheel: wheel["machine"] == platform.machine().lower()
187176
and wheel["platform"] == sys.platform,
188-
wheels,
177+
self.base_wheel_bundles,
189178
),
190179
)
191180
)
@@ -197,51 +186,11 @@ def _download_and_extract_local_driver(
197186
extractall(zip, "playwright/driver")
198187

199188

189+
if len(sys.argv) == 2 and sys.argv[1] == "--list-wheels":
190+
for bundle in PlaywrightBDistWheelCommand.base_wheel_bundles:
191+
print(bundle["wheel"])
192+
exit(0)
193+
200194
setup(
201-
name="playwright",
202-
author="Microsoft Corporation",
203-
author_email="",
204-
description="A high-level API to automate web browsers",
205-
long_description=Path("README.md").read_text(encoding="utf-8"),
206-
long_description_content_type="text/markdown",
207-
license="Apache-2.0",
208-
url="https://github.com/Microsoft/playwright-python",
209-
project_urls={
210-
"Release notes": "https://github.com/microsoft/playwright-python/releases",
211-
},
212-
packages=[
213-
"playwright",
214-
"playwright.async_api",
215-
"playwright.sync_api",
216-
"playwright._impl",
217-
"playwright._impl.__pyinstaller",
218-
],
219-
include_package_data=True,
220-
install_requires=[
221-
"greenlet==3.1.1",
222-
"pyee==12.0.0",
223-
],
224-
# TODO: Can be removed once we migrate to pypa/build or pypa/installer.
225-
setup_requires=["setuptools-scm==8.1.0", "wheel==0.45.0"],
226-
classifiers=[
227-
"Topic :: Software Development :: Testing",
228-
"Topic :: Internet :: WWW/HTTP :: Browsers",
229-
"Intended Audience :: Developers",
230-
"Programming Language :: Python :: 3",
231-
"Programming Language :: Python :: 3.9",
232-
"Programming Language :: Python :: 3.10",
233-
"Programming Language :: Python :: 3.11",
234-
"Programming Language :: Python :: 3.12",
235-
"Programming Language :: Python :: 3.13",
236-
"License :: OSI Approved :: Apache Software License",
237-
"Operating System :: OS Independent",
238-
],
239-
python_requires=">=3.9",
240195
cmdclass={"bdist_wheel": PlaywrightBDistWheelCommand},
241-
entry_points={
242-
"console_scripts": [
243-
"playwright=playwright.__main__:main",
244-
],
245-
"pyinstaller40": ["hook-dirs=playwright._impl.__pyinstaller:get_hook_dirs"],
246-
},
247196
)

0 commit comments

Comments
 (0)