FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
setup-python/src/install-python.ts at main · actions/setup-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
actions
/
setup-python
Public
Notifications
You must be signed in to change notification settings
Fork
734
Star
2.2k
Code
Issues
37
Pull requests
21
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
setup-python
/
src
/
install-python.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
325 lines (294 loc) · 9.64 KB
Breadcrumbs
setup-python
/
src
/
install-python.ts
Copy path
File metadata and controls
325 lines (294 loc) · 9.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import
*
as
path
from
'path'
;
import
*
as
core
from
'@actions/core'
;
import
*
as
tc
from
'@actions/tool-cache'
;
import
*
as
exec
from
'@actions/exec'
;
import
{
ExecOptions
}
from
'@actions/exec'
;
import
*
as
httpm
from
'@actions/http-client'
;
import
*
as
fs
from
'fs'
;
import
*
as
semver
from
'semver'
;
import
{
IS_WINDOWS
,
IS_LINUX
,
getDownloadFileName
}
from
'./utils.js'
;
import
{
IToolRelease
}
from
'@actions/tool-cache'
;
const
TOKEN
=
core
.
getInput
(
'token'
)
;
const
AUTH
=
!
TOKEN
?
undefined
:
`token
${
TOKEN
}
`
;
const
MANIFEST_REPO_OWNER
=
'actions'
;
const
MANIFEST_REPO_NAME
=
'python-versions'
;
const
MANIFEST_REPO_BRANCH
=
'main'
;
export
const
MANIFEST_URL
=
`https://raw.githubusercontent.com/
${
MANIFEST_REPO_OWNER
}
/
${
MANIFEST_REPO_NAME
}
/
${
MANIFEST_REPO_BRANCH
}
/versions-manifest.json`
;
interface
LinuxOsRelease
{
id
:
string
;
versionId
:
string
;
}
function
getLinuxOsRelease
(
)
:
LinuxOsRelease
|
null
{
try
{
const
content
=
fs
.
readFileSync
(
'/etc/os-release'
,
'utf8'
)
;
const
lines
=
content
.
split
(
'\n'
)
;
let
id
=
''
;
let
versionId
=
''
;
for
(
const
line
of
lines
)
{
const
parts
=
line
.
split
(
'='
)
;
if
(
parts
.
length
===
2
)
{
const
key
=
parts
[
0
]
.
trim
(
)
;
const
value
=
parts
[
1
]
.
trim
(
)
.
replace
(
/
^
"
/
,
''
)
.
replace
(
/
"
$
/
,
''
)
;
if
(
key
===
'ID'
)
id
=
value
;
if
(
key
===
'VERSION_ID'
)
versionId
=
value
;
}
}
if
(
id
&&
versionId
)
{
return
{
id
,
versionId
}
;
}
return
null
;
}
catch
{
return
null
;
}
}
function
findRhelRelease
(
semanticVersionSpec
:
string
,
architecture
:
string
,
manifest
:
tc
.
IToolRelease
[
]
,
osVersion
:
string
)
:
tc
.
IToolRelease
|
undefined
{
for
(
const
candidate
of
manifest
)
{
const
version
=
candidate
.
version
;
core
.
debug
(
`check
${
version
}
satisfies
${
semanticVersionSpec
}
`
)
;
if
(
!
semver
.
satisfies
(
version
,
semanticVersionSpec
)
)
continue
;
const
file
=
candidate
.
files
.
find
(
item
=>
{
core
.
debug
(
`
${
item
.
arch
}
===
${
architecture
}
&&
${
item
.
platform
}
===rhel &&
${
item
.
platform_version
}
===
${
osVersion
}
`
)
;
const
archMatch
=
item
.
arch
===
architecture
;
const
platformMatch
=
item
.
platform
===
'rhel'
;
const
versionMatch
=
!
item
.
platform_version
||
item
.
platform_version
===
osVersion
||
osVersion
.
startsWith
(
item
.
platform_version
)
;
return
archMatch
&&
platformMatch
&&
versionMatch
;
}
)
;
if
(
file
)
{
core
.
debug
(
`matched
${
candidate
.
version
}
`
)
;
const
result
=
Object
.
assign
(
{
}
,
candidate
)
;
result
.
files
=
[
file
]
;
return
result
;
}
}
return
undefined
;
}
const
MANIFEST_FETCH_MAX_ATTEMPTS
=
3
;
const
MANIFEST_FETCH_RETRY_BASE_DELAY_MS
=
1000
;
export
async
function
findReleaseFromManifest
(
semanticVersionSpec
:
string
,
architecture
:
string
,
manifest
:
tc
.
IToolRelease
[
]
|
null
)
:
Promise
<
tc
.
IToolRelease
|
undefined
>
{
if
(
!
manifest
)
{
manifest
=
await
getManifest
(
)
;
}
// On RHEL, tc.findFromManifest() won't match because os.platform() returns 'linux'
// but manifest entries use platform 'rhel'. Use custom filtering for RHEL.
if
(
IS_LINUX
)
{
const
osRelease
=
getLinuxOsRelease
(
)
;
if
(
osRelease
&&
osRelease
.
id
===
'rhel'
)
{
core
.
debug
(
`Detected RHEL
${
osRelease
.
versionId
}
, using custom manifest filtering`
)
;
return
findRhelRelease
(
semanticVersionSpec
,
architecture
,
manifest
,
osRelease
.
versionId
)
;
}
}
const
foundRelease
=
await
tc
.
findFromManifest
(
semanticVersionSpec
,
false
,
manifest
,
architecture
)
;
return
foundRelease
;
}
function
isIToolRelease
(
obj
:
any
)
:
obj
is
IToolRelease
{
return
(
typeof
obj
===
'object'
&&
obj
!==
null
&&
typeof
obj
.
version
===
'string'
&&
typeof
obj
.
stable
===
'boolean'
&&
Array
.
isArray
(
obj
.
files
)
&&
obj
.
files
.
every
(
(
file
:
any
)
=>
typeof
file
.
filename
===
'string'
&&
typeof
file
.
platform
===
'string'
&&
typeof
file
.
arch
===
'string'
&&
typeof
file
.
download_url
===
'string'
)
)
;
}
// Rejects empty or truncated manifest responses.
function
isValidManifest
(
manifest
:
unknown
)
:
manifest
is
tc
.
IToolRelease
[
]
{
return
(
Array
.
isArray
(
manifest
)
&&
manifest
.
length
>
0
&&
manifest
.
every
(
isIToolRelease
)
)
;
}
function
sleep
(
ms
:
number
)
:
Promise
<
void
>
{
return
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
)
)
;
}
// HTTP 403/429 from http-client (`statusCode`) or tool-cache (`httpStatusCode`).
function
isRateLimitError
(
err
:
unknown
)
:
boolean
{
const
e
=
err
as
|
{
httpStatusCode
?:
number
;
statusCode
?:
number
}
|
null
|
undefined
;
const
status
=
e
?.
httpStatusCode
??
e
?.
statusCode
;
return
status
===
403
||
status
===
429
;
}
// Fetches and validates a manifest, retrying transient failures with backoff.
async
function
fetchValidManifest
(
source
:
string
,
fetcher
:
(
)
=>
Promise
<
tc
.
IToolRelease
[
]
>
)
:
Promise
<
tc
.
IToolRelease
[
]
>
{
let
lastError
:
Error
|
undefined
;
let
attempts
=
0
;
for
(
let
attempt
=
1
;
attempt
<=
MANIFEST_FETCH_MAX_ATTEMPTS
;
attempt
++
)
{
attempts
=
attempt
;
try
{
const
manifest
=
await
fetcher
(
)
;
if
(
isValidManifest
(
manifest
)
)
{
return
manifest
;
}
throw
new
Error
(
`The manifest fetched from
${
source
}
is empty, truncated, or does not contain any valid tool release entries.`
)
;
}
catch
(
err
)
{
lastError
=
err
instanceof
Error
?
err
:
new
Error
(
String
(
err
)
)
;
core
.
debug
(
`Attempt
${
attempt
}
/
${
MANIFEST_FETCH_MAX_ATTEMPTS
}
to fetch the manifest from
${
source
}
failed:
${
lastError
.
message
}
`
)
;
// Rate limits won't clear within the backoff window; fall back instead.
if
(
isRateLimitError
(
err
)
)
{
core
.
debug
(
`
${
source
}
is rate-limited; skipping retries for this source.`
)
;
break
;
}
if
(
attempt
<
MANIFEST_FETCH_MAX_ATTEMPTS
)
{
const
delay
=
MANIFEST_FETCH_RETRY_BASE_DELAY_MS
*
2
**
(
attempt
-
1
)
;
core
.
debug
(
`Retrying in
${
delay
}
ms...`
)
;
await
sleep
(
delay
)
;
}
}
}
throw
new
Error
(
`Failed to fetch a valid manifest from
${
source
}
after
${
attempts
}
attempt(s):
${
lastError
?.
message
}
`
)
;
}
export
async
function
getManifest
(
)
:
Promise
<
tc
.
IToolRelease
[
]
>
{
try
{
return
await
fetchValidManifest
(
'the GitHub API'
,
getManifestFromRepo
)
;
}
catch
(
err
)
{
core
.
debug
(
'Fetching the manifest via the API failed.'
)
;
if
(
err
instanceof
Error
)
{
core
.
debug
(
err
.
message
)
;
}
else
{
core
.
debug
(
'An unexpected error occurred while fetching the manifest.'
)
;
}
}
try
{
return
await
fetchValidManifest
(
'the raw URL'
,
getManifestFromURL
)
;
}
catch
(
err
)
{
const
message
=
err
instanceof
Error
?
err
.
message
:
String
(
err
)
;
// Fail loudly so the action doesn't exit 0 without installing Python.
throw
new
Error
(
`Failed to fetch the Python versions manifest. The response was empty, truncated, or invalid, and all retries were exhausted.
${
message
}
`
,
{
cause
:
err
}
)
;
}
}
export
function
getManifestFromRepo
(
)
:
Promise
<
tc
.
IToolRelease
[
]
>
{
core
.
debug
(
`Getting manifest from
${
MANIFEST_REPO_OWNER
}
/
${
MANIFEST_REPO_NAME
}
@
${
MANIFEST_REPO_BRANCH
}
`
)
;
return
tc
.
getManifestFromRepo
(
MANIFEST_REPO_OWNER
,
MANIFEST_REPO_NAME
,
AUTH
,
MANIFEST_REPO_BRANCH
)
;
}
export
async
function
getManifestFromURL
(
)
:
Promise
<
tc
.
IToolRelease
[
]
>
{
core
.
debug
(
'Falling back to fetching the manifest using raw URL.'
)
;
const
http
:
httpm
.
HttpClient
=
new
httpm
.
HttpClient
(
'tool-cache'
)
;
const
response
=
await
http
.
getJson
<
tc
.
IToolRelease
[
]
>
(
MANIFEST_URL
)
;
if
(
!
response
.
result
)
{
throw
new
Error
(
`Unable to get manifest from
${
MANIFEST_URL
}
`
)
;
}
return
response
.
result
;
}
async
function
installPython
(
workingDirectory
:
string
)
{
const
options
:
ExecOptions
=
{
cwd
:
workingDirectory
,
env
:
{
...
process
.
env
,
...
(
IS_LINUX
&&
{
LD_LIBRARY_PATH
:
path
.
join
(
workingDirectory
,
'lib'
)
}
)
}
,
silent
:
true
,
listeners
:
{
stdout
:
(
data
:
Buffer
)
=>
{
core
.
info
(
data
.
toString
(
)
.
trim
(
)
)
;
}
,
stderr
:
(
data
:
Buffer
)
=>
{
const
msg
=
data
.
toString
(
)
.
trim
(
)
;
if
(
/
^
W
A
R
N
I
N
G
:
/
im
.
test
(
msg
)
)
{
core
.
warning
(
msg
)
;
}
else
{
core
.
error
(
msg
)
;
}
}
}
}
;
if
(
IS_WINDOWS
)
{
await
exec
.
exec
(
'powershell'
,
[
'./setup.ps1'
]
,
options
)
;
}
else
{
await
exec
.
exec
(
'bash'
,
[
'./setup.sh'
]
,
options
)
;
}
}
export
async
function
installCpythonFromRelease
(
release
:
tc
.
IToolRelease
)
{
if
(
!
release
.
files
||
release
.
files
.
length
===
0
)
{
throw
new
Error
(
'No files found in the release to download.'
)
;
}
const
downloadUrl
=
release
.
files
[
0
]
.
download_url
;
core
.
info
(
`Download from "
${
downloadUrl
}
"`
)
;
let
pythonPath
=
''
;
try
{
const
fileName
=
getDownloadFileName
(
downloadUrl
)
;
pythonPath
=
await
tc
.
downloadTool
(
downloadUrl
,
fileName
,
AUTH
)
;
core
.
info
(
'Extract downloaded archive'
)
;
let
pythonExtractedFolder
;
if
(
IS_WINDOWS
)
{
pythonExtractedFolder
=
await
tc
.
extractZip
(
pythonPath
)
;
}
else
{
pythonExtractedFolder
=
await
tc
.
extractTar
(
pythonPath
)
;
}
core
.
info
(
'Execute installation script'
)
;
await
installPython
(
pythonExtractedFolder
)
;
}
catch
(
err
)
{
if
(
err
instanceof
tc
.
HTTPError
)
{
// Rate limit?
if
(
err
.
httpStatusCode
===
403
)
{
core
.
error
(
`Received HTTP status code 403. This indicates a permission issue or restricted access.`
)
;
}
else
if
(
err
.
httpStatusCode
===
429
)
{
core
.
info
(
`Received HTTP status code 429. This usually indicates the rate limit has been exceeded`
)
;
}
else
{
core
.
info
(
err
.
message
)
;
}
if
(
err
.
stack
)
{
core
.
debug
(
err
.
stack
)
;
}
}
throw
err
;
}
}
Back
|
FazBrowse Home
|
New Git URL