@@ -15,34 +15,62 @@ import { setupDnfPack } from "../utils/setup/setupDnfPack"
15
15
import { isUbuntu } from "../utils/env/isUbuntu"
16
16
import { getExecOutput } from "@actions/exec"
17
17
import { isBinUptoDate } from "../utils/setup/version"
18
- import { getVersion } from "../versions/versions"
19
- import assert from "assert"
20
18
import { execaSync } from "execa"
21
19
import { unique } from "../utils/std"
22
20
import { DefaultVersions } from "../versions/default_versions"
23
21
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
+ }
28
49
}
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
29
63
try {
30
- info ( "Installing python in GitHub Actions" )
31
- const { setupActionsPython } = await import ( "./actions_python" )
32
- return setupActionsPython ( version , setupDir , arch )
64
+ setupWheel ( foundPython )
33
65
} 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...` )
36
67
}
68
+
69
+ return installInfo
37
70
}
38
71
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
46
74
switch ( process . platform ) {
47
75
case "win32" : {
48
76
if ( setupDir ) {
@@ -81,23 +109,9 @@ export async function setupPythonViaSystem(
81
109
throw new Error ( "Unsupported platform" )
82
110
}
83
111
}
84
- await findOrSetupPip ( ( await findPython ( ) ) ! )
85
112
return installInfo
86
113
}
87
114
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
-
101
115
async function findPython ( ) {
102
116
if ( which . sync ( "python3" , { nothrow : true } ) !== null ) {
103
117
return "python3"
@@ -107,23 +121,6 @@ async function findPython() {
107
121
return undefined
108
122
}
109
123
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
-
127
124
async function findOrSetupPip ( foundPython : string ) {
128
125
const maybePip = await findPip ( )
129
126
@@ -161,12 +158,14 @@ function ensurePipUpgrade(foundPython: string) {
161
158
try {
162
159
execaSync ( foundPython , [ "-m" , "ensurepip" , "-U" , "--upgrade" ] , { stdio : "inherit" } )
163
160
return true
164
- } catch {
161
+ } catch ( err1 ) {
162
+ info ( ( err1 as Error ) ?. toString ?.( ) )
165
163
try {
166
164
// ensure pip is disabled on Ubuntu
167
165
execaSync ( foundPython , [ "-m" , "pip" , "install" , "--upgrade" , "pip" ] , { stdio : "inherit" } )
168
166
return true
169
- } catch {
167
+ } catch ( err2 ) {
168
+ info ( ( err2 as Error ) ?. toString ?.( ) )
170
169
// pip module not found
171
170
}
172
171
}
0 commit comments