-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_python_source.ts
169 lines (150 loc) · 4.68 KB
/
generate_python_source.ts
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
import os from 'os';
import {x} from 'tar';
import fs,{createWriteStream} from 'fs'
import path from 'path';
import { Readable } from 'stream';
import {SingleBar} from 'cli-progress';
import { fetch, ProxyAgent, RequestInit } from 'undici';
const bar = new SingleBar({
format: 'Downloading |{bar}| {percentage}% || {value}/{total} Chunks',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
function selectCPythonBuild() {
const platform = os.platform();
const arch = os.arch();
let build = 'cpython-3.10.14+20240415-';
if (platform === 'win32') {
if (arch === 'x64') {
build += 'x86_64-pc-windows-msvc-install_only.tar.gz';
} else if (arch === 'ia32') {
build += 'i686-pc-windows-msvc-install_only.tar.gz';
} else {
return null;
}
} else if (platform === 'darwin') {
if (arch === 'x64') {
build += 'x86_64-apple-darwin-install_only.tar.gz';
} else if (arch === 'arm64') {
build += 'aarch64-apple-darwin-install_only.tar.gz';
} else {
return null;
}
} else if (platform === 'linux') {
if (arch === 'x64') {
build += 'x86_64-unknown-linux-gnu-install_only.tar.gz';
} else if (arch.startsWith('arm')) {
build += 'armv7-unknown-linux-gnueabihf-install_only.tar.gz';
} else {
return null;
}
} else {
return null;
}
return build;
}
function delay(seconds?:number) {
if (!seconds) seconds = Math.floor(5 + 5 * Math.random());
const delay = seconds * 1000;
return new Promise(resolve => setTimeout(resolve, delay))
}
async function retry<T>(action: () => Promise<T>): Promise<T> {
for (let attempt = 0;;) {
try {
return await action();
} catch (err) {
if (++attempt === 3) throw err;
}
await delay();
}
}
function fetchSafely(url:string, token:string, options: RequestInit = {}) {
return retry(async () => {
if (!token) token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN;
options.headers = {
'User-Agent': 'prantlf/grab-github-release',
'X-GitHub-Api-Version': '2022-11-28',
}
if (token) {
options.headers = {
Authorization: `Bearer ${token}`,
...options.headers
};
}
const proxy = process.env.http_proxy || process.env.https_proxy
const res = await fetch(url, {
...options,
dispatcher: proxy ? new ProxyAgent(proxy) : undefined
});
if (!res.ok) {
if (res.status === 403 || res.status === 429) {
const after = res.headers.get('retry-after');
const reset = res.headers.get('x-ratelimit-reset');
// const {
// 'retry-after': after,
// 'x-ratelimit-limit': limit,
// 'x-ratelimit-remaining': remaining,
// 'x-ratelimit-used': used,
// 'x-ratelimit-reset': reset,
// 'x-ratelimit-resource': resource
// } = res.headers;
const wait = after || reset;
if (wait) {
await delay(Number(wait));
}
}
const err = new Error(`GET "${url}" failed: ${res.status} ${res.statusText}`) as Error & { response: Response };
// err.response = res;
throw err
}
return res
})
}
async function download(url:string, archive:string, token?:string) {
const res = await fetchSafely(url, token)
const totalSize = parseInt(res.headers.get('content-length'), 10);
await new Promise((resolve, reject) => {
bar.start(totalSize, 0);
const stream = Readable.fromWeb(res.body)
stream
.on('data', chunk=>{
bar.increment(chunk.length)
})
stream.pipe(createWriteStream(archive))
.on('finish', ()=>{
bar.stop()
resolve('')
})
.on('error', ()=>{
bar.stop()
reject('')
})
})
}
async function downloadAndExtractBuild(downloadUrl:string, extractPath:string) {
// 检查提取路径是否存在,如果不存在则创建
if (!fs.existsSync(extractPath)) {
fs.mkdirSync(extractPath, { recursive: true });
}
const buildName = downloadUrl.split('/').pop()
// 创建一个临时文件来保存下载的tar.gz文件
const tempFilePath = path.join(extractPath, buildName);
await download(downloadUrl, tempFilePath);
console.log(`${buildName} start extract`);
await x({
file: tempFilePath,
C: extractPath,
strip: 1
});
// 删除临时tar.gz文件
fs.unlinkSync(tempFilePath);
console.log(`${buildName} has been downloaded and extracted to ${extractPath}`);
}
const buildName = selectCPythonBuild()
downloadAndExtractBuild(
`https://github.com/indygreg/python-build-standalone/releases/download/20240415/${buildName}`,
path.join(process.cwd(),'src','assets','python_source')
).then(()=>{
console.log('python install done')
})