@@ -19,6 +19,9 @@ const CA_CERT_PATH = join(import.meta.dirname, 'runner/certs/cacert.pem')
1919
2020const log = debuglog ( 'UNDICI_WPT' )
2121const WPT_SERVER_URL = 'http://web-platform.test:8000'
22+ const PYTHON_CANDIDATES = [ 'python3' , 'python' ]
23+
24+ let pythonInfoPromise
2225
2326const SERVER_READY_CHECKS = [
2427 [ 'http-default' , ( line ) => line . includes ( 'http on port 8000' ) && line . includes ( 'Starting http server' ) ] ,
@@ -92,6 +95,88 @@ async function terminateProcess (proc, exitPromise) {
9295 await exitPromise
9396}
9497
98+ function parsePythonVersion ( output ) {
99+ const versionRegex = / ^ P y t h o n (?< major > \d + ) \. (?< minor > \d + ) \. (?< patch > \d + ) / m. exec ( output . trim ( ) )
100+
101+ if ( versionRegex === null ) {
102+ return null
103+ }
104+
105+ const { major, minor, patch } = versionRegex . groups
106+ return { major : Number ( major ) , minor : Number ( minor ) , patch : Number ( patch ) }
107+ }
108+
109+ async function getPythonInfo ( ) {
110+ pythonInfoPromise ??= ( async ( ) => {
111+ const failures = [ ]
112+
113+ for ( const command of PYTHON_CANDIDATES ) {
114+ const result = await new Promise ( ( resolve ) => {
115+ const proc = spawn ( command , [ '--version' ] , {
116+ stdio : [ 'ignore' , 'pipe' , 'pipe' ]
117+ } )
118+
119+ let output = ''
120+ let settled = false
121+ const onData = ( chunk ) => {
122+ output += chunk . toString ( )
123+ }
124+
125+ proc . stdout . on ( 'data' , onData )
126+ proc . stderr . on ( 'data' , onData )
127+
128+ const timeout = setTimeout ( ( ) => {
129+ if ( ! settled ) {
130+ settled = true
131+ proc . kill ( )
132+ resolve ( { command, reason : 'timed out while checking version' } )
133+ }
134+ } , 10_000 )
135+
136+ proc . once ( 'error' , ( err ) => {
137+ if ( settled ) {
138+ return
139+ }
140+
141+ settled = true
142+ clearTimeout ( timeout )
143+ resolve ( { command, reason : err . message } )
144+ } )
145+
146+ proc . once ( 'exit' , ( code ) => {
147+ if ( settled ) {
148+ return
149+ }
150+
151+ settled = true
152+ clearTimeout ( timeout )
153+
154+ const version = parsePythonVersion ( output )
155+ if ( code === 0 && version !== null && version . major === 3 ) {
156+ resolve ( { command, version } )
157+ return
158+ }
159+
160+ const reason = version !== null
161+ ? `found unsupported Python ${ version . major } .${ version . minor } .${ version . patch } `
162+ : output . trim ( ) || `exited with code ${ code } `
163+ resolve ( { command, reason } )
164+ } )
165+ } )
166+
167+ if ( 'version' in result ) {
168+ return result
169+ }
170+
171+ failures . push ( `${ command } : ${ result . reason } ` )
172+ }
173+
174+ throw new Error ( `Python 3 is required. Checked: ${ failures . join ( '; ' ) } ` )
175+ } ) ( )
176+
177+ return pythonInfoPromise
178+ }
179+
95180async function ensureWPTCheckout ( ) {
96181 if ( existsSync ( WPT_SCRIPT_PATH ) ) {
97182 return
@@ -122,6 +207,7 @@ async function ensureWPTCheckout () {
122207}
123208
124209async function startWPTServer ( ) {
210+ const { command : pythonCommand } = await getPythonInfo ( )
125211 const { promise, resolve, reject } = Promise . withResolvers ( )
126212 const { promise : readyPromise , resolve : resolveReady , reject : rejectReady } = Promise . withResolvers ( )
127213 const readyChecks = new Set ( )
@@ -144,7 +230,7 @@ async function startWPTServer () {
144230 }
145231 }
146232
147- const proc = spawn ( 'python3' , [ WPT_SCRIPT_PATH , 'serve' , '--config' , '../runner/config.json' ] , {
233+ const proc = spawn ( pythonCommand , [ WPT_SCRIPT_PATH , 'serve' , '--config' , '../runner/config.json' ] , {
148234 cwd : WPT_DIR ,
149235 stdio : [ 'ignore' , 'pipe' , 'pipe' ]
150236 } )
@@ -503,32 +589,14 @@ async function setup () {
503589
504590 await ensureWPTCheckout ( )
505591
506- // Check Python
507- const pythonCheck = spawn ( 'python3' , [ '--version' ] , { stdio : 'pipe' } )
508- pythonCheck . stdout . setEncoding ( 'ascii' )
509- const pythonVersion = await new Promise ( ( resolve , reject ) => {
510- pythonCheck . stdout . on ( 'data' , ( c ) => {
511- const versionRegex = / ^ P y t h o n (?< major > \d + ) \. (?< minor > \d + ) \. (?< patch > \d + ) / . exec ( c . trim ( ) )
512-
513- if ( versionRegex !== null ) {
514- const { major, minor, patch } = versionRegex . groups
515- resolve ( { major : Number ( major ) , minor : Number ( minor ) , patch : Number ( patch ) } )
516- clearTimeout ( timeout )
517- }
518- } )
519-
520- const timeout = setTimeout ( reject , 30_000 , 'Took too long to determine Python version' )
521- } )
522-
523- if ( pythonVersion . major !== 3 ) {
524- throw new Error ( 'Python 3 is required' )
525- }
592+ const { command : pythonCommand , version : pythonVersion } = await getPythonInfo ( )
593+ console . log ( `Using Python command: ${ pythonCommand } (${ pythonVersion . major } .${ pythonVersion . minor } .${ pythonVersion . patch } )` )
526594
527595 // Check if manifest exists
528596 const manifestPath = join ( WPT_DIR , 'MANIFEST.json' )
529597 if ( ! existsSync ( manifestPath ) ) {
530598 console . log ( 'Updating WPT manifest...' )
531- const manifestProc = spawn ( 'python3' , [ WPT_SCRIPT_PATH , 'manifest' ] , {
599+ const manifestProc = spawn ( pythonCommand , [ WPT_SCRIPT_PATH , 'manifest' ] , {
532600 cwd : WPT_DIR ,
533601 stdio : 'inherit'
534602 } )
@@ -553,7 +621,7 @@ async function setup () {
553621 const etcHostsConfigured = hostsContent . includes ( 'web-platform.test' )
554622
555623 async function setupHostsFile ( ) {
556- const makeHostsProc = spawn ( 'python3' , [ WPT_SCRIPT_PATH , 'make-hosts-file' ] , {
624+ const makeHostsProc = spawn ( pythonCommand , [ WPT_SCRIPT_PATH , 'make-hosts-file' ] , {
557625 cwd : WPT_DIR ,
558626 stdio : [ 'ignore' , 'pipe' , 'pipe' ]
559627 } )
@@ -609,9 +677,9 @@ async function setup () {
609677 console . log ( 'Please configure hosts file manually:' )
610678 console . log ( `cd ${ WPT_DIR } ` )
611679 if ( process . platform === 'win32' ) {
612- console . log ( 'python wpt make-hosts-file | Out-File $env:SystemRoot\\System32\\drivers\\etc\\hosts -Encoding ascii -Append' )
680+ console . log ( ` ${ pythonCommand } wpt make-hosts-file | Out-File $env:SystemRoot\\System32\\drivers\\etc\\hosts -Encoding ascii -Append` )
613681 } else {
614- console . log ( 'python3 wpt make-hosts-file | sudo tee -a /etc/hosts' )
682+ console . log ( ` ${ pythonCommand } wpt make-hosts-file | sudo tee -a /etc/hosts` )
615683 }
616684
617685 console . log ( '❌ \x1B[31mSetup incomplete.\x1B[0m' )
0 commit comments