1919import subprocess
2020import sys
2121import zipfile
22- from pathlib import Path
2322from typing import Dict , List
2423
24+ from auditwheel .wheeltools import InWheel
2525from setuptools import setup
26-
27- try :
28- from auditwheel .wheeltools import InWheel
29- except ImportError :
30- InWheel = None
3126from wheel .bdist_wheel import bdist_wheel as BDistWheelCommand
3227
3328driver_version = "1.48.1"
@@ -60,132 +55,122 @@ def download_driver(zip_name: str) -> None:
6055
6156
6257class PlaywrightBDistWheelCommand (BDistWheelCommand ):
63- user_options = BDistWheelCommand .user_options + [
64- ("all" , "a" , "create wheels for all platforms" )
58+ base_wheel_bundles : List [Dict [str , str ]] = [
59+ {
60+ "wheel" : "macosx_10_13_x86_64.whl" ,
61+ "machine" : "x86_64" ,
62+ "platform" : "darwin" ,
63+ "zip_name" : "mac" ,
64+ },
65+ {
66+ "wheel" : "macosx_11_0_universal2.whl" ,
67+ "machine" : "x86_64" ,
68+ "platform" : "darwin" ,
69+ "zip_name" : "mac" ,
70+ },
71+ {
72+ "wheel" : "macosx_11_0_arm64.whl" ,
73+ "machine" : "arm64" ,
74+ "platform" : "darwin" ,
75+ "zip_name" : "mac-arm64" ,
76+ },
77+ {
78+ "wheel" : "manylinux1_x86_64.whl" ,
79+ "machine" : "x86_64" ,
80+ "platform" : "linux" ,
81+ "zip_name" : "linux" ,
82+ },
83+ {
84+ "wheel" : "manylinux_2_17_aarch64.manylinux2014_aarch64.whl" ,
85+ "machine" : "aarch64" ,
86+ "platform" : "linux" ,
87+ "zip_name" : "linux-arm64" ,
88+ },
89+ {
90+ "wheel" : "win32.whl" ,
91+ "machine" : "i386" ,
92+ "platform" : "win32" ,
93+ "zip_name" : "win32_x64" ,
94+ },
95+ {
96+ "wheel" : "win_amd64.whl" ,
97+ "machine" : "amd64" ,
98+ "platform" : "win32" ,
99+ "zip_name" : "win32_x64" ,
100+ },
65101 ]
66- boolean_options = BDistWheelCommand .boolean_options + ["all" ]
67-
68- def initialize_options (self ) -> None :
69- super ().initialize_options ()
70- self .all = False
71102
72103 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 )
76104 super ().run ()
77105 os .makedirs ("driver" , exist_ok = True )
78106 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 (
107+ self ._download_and_extract_local_driver ()
108+
109+ wheel = None
110+ if os .getenv ("PLAYWRIGHT_TARGET_WHEEL" , None ):
111+ wheel = list (
112+ filter (
113+ lambda wheel : wheel ["wheel" ]
114+ == os .getenv ("PLAYWRIGHT_TARGET_WHEEL" ),
115+ self .base_wheel_bundles ,
116+ )
117+ )[0 ]
118+ else :
119+ wheel = list (
129120 filter (
130121 lambda wheel : wheel ["platform" ] == sys .platform
131122 and wheel ["machine" ] == platform .machine ().lower (),
132- base_wheel_bundles ,
123+ self . base_wheel_bundles ,
133124 )
134- )[:1 ]
135- self ._build_wheels (wheels )
125+ )[0 ]
126+ assert wheel
127+ self ._build_wheel (wheel )
136128
137- def _build_wheels (
129+ def _build_wheel (
138130 self ,
139- wheels : List [ Dict [str , str ] ],
131+ wheel_bundle : Dict [str , str ],
140132 ) -> None :
133+ assert self .dist_dir
141134 base_wheel_location : str = glob .glob (os .path .join (self .dist_dir , "*.whl" ))[0 ]
142135 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"
136+ download_driver (wheel_bundle ["zip_name" ])
137+ zip_file = f"driver/playwright-{ driver_version } -{ wheel_bundle ['zip_name' ]} .zip"
138+ with zipfile .ZipFile (zip_file , "r" ) as zip :
139+ extractall (zip , f"driver/{ wheel_bundle ['zip_name' ]} " )
140+ wheel_location = without_platform + wheel_bundle ["wheel" ]
141+ shutil .copy (base_wheel_location , wheel_location )
142+ with zipfile .ZipFile (wheel_location , "a" ) as zip :
143+ driver_root = os .path .abspath (f"driver/{ wheel_bundle ['zip_name' ]} " )
144+ for dir_path , _ , files in os .walk (driver_root ):
145+ for file in files :
146+ from_path = os .path .join (dir_path , file )
147+ to_path = os .path .relpath (from_path , driver_root )
148+ zip .write (from_path , f"playwright/driver/{ to_path } " )
149+ zip .writestr (
150+ "playwright/driver/README.md" ,
151+ f"{ wheel_bundle ['wheel' ]} driver package" ,
147152 )
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- )
163153 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 )
167- with InWheel (
168- in_wheel = whlfile ,
169- out_wheel = os .path .join ("wheelhouse" , os .path .basename (whlfile )),
170- ):
171- 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" )
154+ for whlfile in glob .glob (os .path .join (self .dist_dir , "*.whl" )):
155+ os .makedirs ("wheelhouse" , exist_ok = True )
156+ with InWheel (
157+ in_wheel = whlfile ,
158+ out_wheel = os .path .join ("wheelhouse" , os .path .basename (whlfile )),
159+ ):
160+ print (f"Updating RECORD file of { whlfile } " )
161+ print ("Copying new wheels" )
162+ shutil .move ("wheelhouse" , self .dist_dir )
177163
178164 def _download_and_extract_local_driver (
179165 self ,
180- wheels : List [Dict [str , str ]],
181166 ) -> None :
182167 zip_names_for_current_system = set (
183168 map (
184169 lambda wheel : wheel ["zip_name" ],
185170 filter (
186171 lambda wheel : wheel ["machine" ] == platform .machine ().lower ()
187172 and wheel ["platform" ] == sys .platform ,
188- wheels ,
173+ self . base_wheel_bundles ,
189174 ),
190175 )
191176 )
@@ -197,51 +182,11 @@ def _download_and_extract_local_driver(
197182 extractall (zip , "playwright/driver" )
198183
199184
185+ if len (sys .argv ) == 2 and sys .argv [1 ] == "--list-wheels" :
186+ for bundle in PlaywrightBDistWheelCommand .base_wheel_bundles :
187+ print (bundle ["wheel" ])
188+ exit (0 )
189+
200190setup (
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" ,
240191 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- },
247192)
0 commit comments