Skip to content

Commit

Permalink
chore: convert setup.py to build
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Nov 12, 2024
1 parent 1e1122c commit 12de833
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 145 deletions.
4 changes: 3 additions & 1 deletion .azure-pipelines/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ extends:
python -m pip install --upgrade pip
pip install -r local-requirements.txt
pip install -e .
python setup.py bdist_wheel --all
for wheel in $(python setup.py --list-wheels); do
PLAYWRIGHT_TARGET_WHEEL=$wheel python -m build --wheel
done
displayName: 'Install & Build'
- task: EsrpRelease@7
inputs:
Expand Down
4 changes: 1 addition & 3 deletions local-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
auditwheel==6.1.0
autobahn==23.1.2
black==24.8.0
build==1.2.2.post1
flake8==7.1.1
flaky==3.8.1
mypy==1.13.0
Expand All @@ -17,8 +17,6 @@ pytest-timeout==2.3.1
pytest-xdist==3.6.1
requests==2.32.3
service_identity==24.2.0
setuptools==75.4.0
twisted==24.10.0
types-pyOpenSSL==24.1.0.20240722
types-requests==2.32.0.20241016
wheel==0.45.0
48 changes: 48 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,54 @@
requires = ["setuptools==75.4.0", "setuptools-scm==8.1.0", "wheel==0.45.0", "auditwheel==6.1.0"]
build-backend = "setuptools.build_meta"

[project]
name = "playwright"
description = "A high-level API to automate web browsers"
authors = [
{name = "Microsoft Corporation"}
]
readme = "README.md"
license = {text = "Apache-2.0"}
dynamic = ["version"]
requires-python = ">=3.9"
dependencies = [
"greenlet==3.1.1",
"pyee==12.0.0",
]
classifiers = [
"Topic :: Software Development :: Testing",
"Topic :: Internet :: WWW/HTTP :: Browsers",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
]

[project.urls]
homepage = "https://github.com/Microsoft/playwright-python"
"Release notes" = "https://github.com/microsoft/playwright-python/releases"

[project.scripts]
playwright = "playwright.__main__:main"

[project.entry-points.pyinstaller40]
hook-dirs = "playwright._impl.__pyinstaller:get_hook_dirs"

[tool.setuptools]
packages = [
"playwright",
"playwright.async_api",
"playwright.sync_api",
"playwright._impl",
"playwright._impl.__pyinstaller",
]
include-package-data = true

[tool.setuptools_scm]
version_file = "playwright/_repo_version.py"

Expand Down
231 changes: 90 additions & 141 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import subprocess
import sys
import zipfile
from pathlib import Path
from typing import Dict, List

from setuptools import setup

try:
from auditwheel.wheeltools import InWheel
except ImportError:
Expand Down Expand Up @@ -60,132 +58,123 @@ def download_driver(zip_name: str) -> None:


class PlaywrightBDistWheelCommand(BDistWheelCommand):
user_options = BDistWheelCommand.user_options + [
("all", "a", "create wheels for all platforms")
base_wheel_bundles: List[Dict[str, str]] = [
{
"wheel": "macosx_10_13_x86_64.whl",
"machine": "x86_64",
"platform": "darwin",
"zip_name": "mac",
},
{
"wheel": "macosx_11_0_universal2.whl",
"machine": "x86_64",
"platform": "darwin",
"zip_name": "mac",
},
{
"wheel": "macosx_11_0_arm64.whl",
"machine": "arm64",
"platform": "darwin",
"zip_name": "mac-arm64",
},
{
"wheel": "manylinux1_x86_64.whl",
"machine": "x86_64",
"platform": "linux",
"zip_name": "linux",
},
{
"wheel": "manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
"machine": "aarch64",
"platform": "linux",
"zip_name": "linux-arm64",
},
{
"wheel": "win32.whl",
"machine": "i386",
"platform": "win32",
"zip_name": "win32_x64",
},
{
"wheel": "win_amd64.whl",
"machine": "amd64",
"platform": "win32",
"zip_name": "win32_x64",
},
]
boolean_options = BDistWheelCommand.boolean_options + ["all"]

def initialize_options(self) -> None:
super().initialize_options()
self.all = False

def run(self) -> None:
shutil.rmtree("build", ignore_errors=True)
shutil.rmtree("dist", ignore_errors=True)
shutil.rmtree("playwright.egg-info", ignore_errors=True)
super().run()
os.makedirs("driver", exist_ok=True)
os.makedirs("playwright/driver", exist_ok=True)
base_wheel_bundles: List[Dict[str, str]] = [
{
"wheel": "macosx_10_13_x86_64.whl",
"machine": "x86_64",
"platform": "darwin",
"zip_name": "mac",
},
{
"wheel": "macosx_11_0_universal2.whl",
"machine": "x86_64",
"platform": "darwin",
"zip_name": "mac",
},
{
"wheel": "macosx_11_0_arm64.whl",
"machine": "arm64",
"platform": "darwin",
"zip_name": "mac-arm64",
},
{
"wheel": "manylinux1_x86_64.whl",
"machine": "x86_64",
"platform": "linux",
"zip_name": "linux",
},
{
"wheel": "manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
"machine": "aarch64",
"platform": "linux",
"zip_name": "linux-arm64",
},
{
"wheel": "win32.whl",
"machine": "i386",
"platform": "win32",
"zip_name": "win32_x64",
},
{
"wheel": "win_amd64.whl",
"machine": "amd64",
"platform": "win32",
"zip_name": "win32_x64",
},
]
self._download_and_extract_local_driver(base_wheel_bundles)

wheels = base_wheel_bundles
if not self.all:
# Limit to 1, since for MacOS e.g. we have multiple wheels for the same platform and architecture and Conda expects 1.
wheels = list(
self._download_and_extract_local_driver()

wheel = None
if os.getenv("PLAYWRIGHT_TARGET_WHEEL", None):
wheel = list(
filter(
lambda wheel: wheel["wheel"]
== os.getenv("PLAYWRIGHT_TARGET_WHEEL"),
self.base_wheel_bundles,
)
)[0]
else:
wheel = list(
filter(
lambda wheel: wheel["platform"] == sys.platform
and wheel["machine"] == platform.machine().lower(),
base_wheel_bundles,
self.base_wheel_bundles,
)
)[:1]
self._build_wheels(wheels)
)[0]
assert wheel
self._build_wheel(wheel)

def _build_wheels(
def _build_wheel(
self,
wheels: List[Dict[str, str]],
wheel_bundle: Dict[str, str],
) -> None:
assert self.dist_dir
base_wheel_location: str = glob.glob(os.path.join(self.dist_dir, "*.whl"))[0]
without_platform = base_wheel_location[:-7]
for wheel_bundle in wheels:
download_driver(wheel_bundle["zip_name"])
zip_file = (
f"driver/playwright-{driver_version}-{wheel_bundle['zip_name']}.zip"
download_driver(wheel_bundle["zip_name"])
zip_file = f"driver/playwright-{driver_version}-{wheel_bundle['zip_name']}.zip"
with zipfile.ZipFile(zip_file, "r") as zip:
extractall(zip, f"driver/{wheel_bundle['zip_name']}")
wheel_location = without_platform + wheel_bundle["wheel"]
shutil.copy(base_wheel_location, wheel_location)
with zipfile.ZipFile(wheel_location, "a") as zip:
driver_root = os.path.abspath(f"driver/{wheel_bundle['zip_name']}")
for dir_path, _, files in os.walk(driver_root):
for file in files:
from_path = os.path.join(dir_path, file)
to_path = os.path.relpath(from_path, driver_root)
zip.write(from_path, f"playwright/driver/{to_path}")
zip.writestr(
"playwright/driver/README.md",
f"{wheel_bundle['wheel']} driver package",
)
with zipfile.ZipFile(zip_file, "r") as zip:
extractall(zip, f"driver/{wheel_bundle['zip_name']}")
wheel_location = without_platform + wheel_bundle["wheel"]
shutil.copy(base_wheel_location, wheel_location)
with zipfile.ZipFile(wheel_location, "a") as zip:
driver_root = os.path.abspath(f"driver/{wheel_bundle['zip_name']}")
for dir_path, _, files in os.walk(driver_root):
for file in files:
from_path = os.path.join(dir_path, file)
to_path = os.path.relpath(from_path, driver_root)
zip.write(from_path, f"playwright/driver/{to_path}")
zip.writestr(
"playwright/driver/README.md",
f"{wheel_bundle['wheel']} driver package",
)
os.remove(base_wheel_location)
if InWheel:
for whlfile in glob.glob(os.path.join(self.dist_dir, "*.whl")):
os.makedirs("wheelhouse", exist_ok=True)
for whlfile in glob.glob(os.path.join(self.dist_dir, "*.whl")):
os.makedirs("wheelhouse", exist_ok=True)
if InWheel:
with InWheel(
in_wheel=whlfile,
out_wheel=os.path.join("wheelhouse", os.path.basename(whlfile)),
):
print(f"Updating RECORD file of {whlfile}")
shutil.rmtree(self.dist_dir)
print("Copying new wheels")
shutil.move("wheelhouse", self.dist_dir)
else:
print("auditwheel not installed, not updating RECORD file")
print("Copying new wheels")
shutil.move("wheelhouse", self.dist_dir)

def _download_and_extract_local_driver(
self,
wheels: List[Dict[str, str]],
) -> None:
zip_names_for_current_system = set(
map(
lambda wheel: wheel["zip_name"],
filter(
lambda wheel: wheel["machine"] == platform.machine().lower()
and wheel["platform"] == sys.platform,
wheels,
self.base_wheel_bundles,
),
)
)
Expand All @@ -197,51 +186,11 @@ def _download_and_extract_local_driver(
extractall(zip, "playwright/driver")


if len(sys.argv) == 2 and sys.argv[1] == "--list-wheels":
for bundle in PlaywrightBDistWheelCommand.base_wheel_bundles:
print(bundle["wheel"])
exit(0)

setup(
name="playwright",
author="Microsoft Corporation",
author_email="",
description="A high-level API to automate web browsers",
long_description=Path("README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
license="Apache-2.0",
url="https://github.com/Microsoft/playwright-python",
project_urls={
"Release notes": "https://github.com/microsoft/playwright-python/releases",
},
packages=[
"playwright",
"playwright.async_api",
"playwright.sync_api",
"playwright._impl",
"playwright._impl.__pyinstaller",
],
include_package_data=True,
install_requires=[
"greenlet==3.1.1",
"pyee==12.0.0",
],
# TODO: Can be removed once we migrate to pypa/build or pypa/installer.
setup_requires=["setuptools-scm==8.1.0", "wheel==0.45.0"],
classifiers=[
"Topic :: Software Development :: Testing",
"Topic :: Internet :: WWW/HTTP :: Browsers",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
python_requires=">=3.9",
cmdclass={"bdist_wheel": PlaywrightBDistWheelCommand},
entry_points={
"console_scripts": [
"playwright=playwright.__main__:main",
],
"pyinstaller40": ["hook-dirs=playwright._impl.__pyinstaller:get_hook_dirs"],
},
)

0 comments on commit 12de833

Please sign in to comment.