@@ -15,34 +15,62 @@ import { setupDnfPack } from "../utils/setup/setupDnfPack"
1515import { isUbuntu } from "../utils/env/isUbuntu"
1616import { getExecOutput } from "@actions/exec"
1717import { isBinUptoDate } from "../utils/setup/version"
18- import { getVersion } from "../versions/versions"
19- import assert from "assert"
2018import { execaSync } from "execa"
2119import { unique } from "../utils/std"
2220import { DefaultVersions } from "../versions/default_versions"
2321
24- export async function setupPython ( version : string , setupDir : string , arch : string ) {
25- if ( ! GITHUB_ACTIONS ) {
26- // TODO parse version
27- return setupPythonViaSystem ( version , setupDir , arch )
22+ export async function setupPython ( version : string , setupDir : string , arch : string ) : Promise < InstallationInfo > {
23+ let installInfo : InstallationInfo | undefined
24+ let foundPython = await findPython ( )
25+
26+ if ( foundPython !== undefined ) {
27+ const binDir = dirname ( foundPython )
28+ installInfo = { bin : foundPython , installDir : binDir , binDir }
29+ } else {
30+ // if python is not found, try to install it
31+ if ( GITHUB_ACTIONS ) {
32+ // install python in GitHub Actions
33+ try {
34+ info ( "Installing python in GitHub Actions" )
35+ const { setupActionsPython } = await import ( "./actions_python" )
36+ await setupActionsPython ( version , setupDir , arch )
37+
38+ foundPython = ( await findPython ( ) ) !
39+ const binDir = dirname ( foundPython )
40+ installInfo = { bin : foundPython , installDir : binDir , binDir }
41+ } catch ( err ) {
42+ warning ( ( err as Error ) . toString ( ) )
43+ }
44+ }
45+ if ( installInfo === undefined ) {
46+ // install python via system package manager
47+ installInfo = await setupPythonSystem ( setupDir , version )
48+ }
2849 }
50+
51+ if ( foundPython === undefined ) {
52+ foundPython = ( await findPython ( ) ) !
53+ installInfo . bin = foundPython
54+ }
55+
56+ // setup pip
57+ const foundPip = await findOrSetupPip ( foundPython )
58+ if ( foundPip === undefined ) {
59+ throw new Error ( "pip was not installed correctly" )
60+ }
61+
62+ // setup wheel
2963 try {
30- info ( "Installing python in GitHub Actions" )
31- const { setupActionsPython } = await import ( "./actions_python" )
32- return setupActionsPython ( version , setupDir , arch )
64+ setupWheel ( foundPython )
3365 } catch ( err ) {
34- warning ( ( err as Error ) . toString ( ) )
35- return setupPythonViaSystem ( version , setupDir , arch )
66+ warning ( `Failed to install wheels: ${ ( err as Error ) . toString ( ) } . Ignoring...` )
3667 }
68+
69+ return installInfo
3770}
3871
39- export async function setupPythonViaSystem (
40- version : string ,
41- setupDir : string ,
42- // eslint-disable-next-line @typescript-eslint/no-unused-vars
43- _arch : string
44- ) : Promise < InstallationInfo > {
45- let installInfo : InstallationInfo
72+ async function setupPythonSystem ( setupDir : string , version : string ) {
73+ let installInfo : InstallationInfo | undefined
4674 switch ( process . platform ) {
4775 case "win32" : {
4876 if ( setupDir ) {
@@ -81,23 +109,9 @@ export async function setupPythonViaSystem(
81109 throw new Error ( "Unsupported platform" )
82110 }
83111 }
84- await findOrSetupPip ( ( await findPython ( ) ) ! )
85112 return installInfo
86113}
87114
88- /// setup python and pip if needed
89- export async function findOrSetupPythonAndPip ( ) : Promise < string > {
90- const foundPython = await findOrSetupPython ( )
91- const foundPip = await findOrSetupPip ( foundPython )
92- if ( foundPip === undefined ) {
93- throw new Error ( "pip was not installed correctly" )
94- }
95- setupWheel ( foundPython )
96- return foundPython
97- }
98-
99- let setupPythonTried = false
100-
101115async function findPython ( ) {
102116 if ( which . sync ( "python3" , { nothrow : true } ) !== null ) {
103117 return "python3"
@@ -107,23 +121,6 @@ async function findPython() {
107121 return undefined
108122}
109123
110- async function findOrSetupPython ( ) {
111- const maybeFoundPython = await findPython ( )
112- if ( maybeFoundPython !== undefined ) {
113- return maybeFoundPython
114- }
115-
116- if ( setupPythonTried ) {
117- throw new Error ( "Failed to install python" )
118- }
119- setupPythonTried = true
120-
121- // install python
122- info ( "python3 was not found. Installing python" )
123- await setupPython ( getVersion ( "python" , undefined ) , "" , process . arch )
124- return findOrSetupPython ( ) // recurse
125- }
126-
127124async function findOrSetupPip ( foundPython : string ) {
128125 const maybePip = await findPip ( )
129126
@@ -161,12 +158,14 @@ function ensurePipUpgrade(foundPython: string) {
161158 try {
162159 execaSync ( foundPython , [ "-m" , "ensurepip" , "-U" , "--upgrade" ] , { stdio : "inherit" } )
163160 return true
164- } catch {
161+ } catch ( err1 ) {
162+ info ( ( err1 as Error ) ?. toString ?.( ) )
165163 try {
166164 // ensure pip is disabled on Ubuntu
167165 execaSync ( foundPython , [ "-m" , "pip" , "install" , "--upgrade" , "pip" ] , { stdio : "inherit" } )
168166 return true
169- } catch {
167+ } catch ( err2 ) {
168+ info ( ( err2 as Error ) ?. toString ?.( ) )
170169 // pip module not found
171170 }
172171 }
0 commit comments