-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
96 lines (86 loc) · 3.39 KB
/
setup.js
File metadata and controls
96 lines (86 loc) · 3.39 KB
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
const https = require('https');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// 定义下载 URL 和目标路径
const tarUrl = 'https://huggingface.co/datasets/Deep-sea/test/resolve/main/modules.tar';
const tarFile = path.join(__dirname, 'modules.tar');
const extractPath = './'; // 解压到当前目录
// 下载文件,支持重定向
function downloadFile(url, dest, redirects = 0, maxRedirects = 10) {
return new Promise((resolve, reject) => {
if (redirects >= maxRedirects) {
reject(new Error('重定向次数过多,超过最大限制:' + maxRedirects));
return;
}
https.get(url, { headers: { 'User-Agent': 'Node.js' } }, (response) => {
// 处理 302 重定向
if (response.statusCode === 302 || response.statusCode === 301) {
const redirectUrl = response.headers.location;
if (!redirectUrl) {
reject(new Error('重定向响应缺少 Location 头'));
return;
}
console.log(`检测到 ${response.statusCode} 重定向,正在跟随至:${redirectUrl}`);
// 递归调用,处理重定向
return downloadFile(redirectUrl, dest, redirects + 1, maxRedirects).then(resolve).catch(reject);
}
if (response.statusCode !== 200) {
reject(new Error(`下载失败,状态码: ${response.statusCode}`));
return;
}
const file = fs.createWriteStream(dest);
response.pipe(file);
file.on('finish', () => {
file.close();
resolve();
});
}).on('error', (err) => {
fs.unlink(dest, () => reject(err)); // 删除部分下载的文件
});
});
}
// 主逻辑
try {
// 下载 modules.tar
console.log('正在下载 modules.tar 从 Hugging Face...');
downloadFile(tarUrl, tarFile)
.then(() => {
console.log('下载成功,验证文件...');
// 检查文件是否存在
if (!fs.existsSync(tarFile)) {
console.error('错误:下载的 modules.tar 文件不存在');
process.exit(1);
}
// 设置环境变量以支持 UTF-8 编码,防止中文乱码
const env = { ...process.env, LC_ALL: 'en_US.UTF-8' };
// 验证文件格式
console.log('验证 modules.tar 格式...');
const fileOutput = execSync(`file "${tarFile}"`, { encoding: 'utf8', env });
console.log('file 输出:', fileOutput);
if (!fileOutput.includes('tar archive')) {
console.error('错误:modules.tar 不是有效的 tar 归档文件');
process.exit(1);
}
// 尝试解压(支持 .tar 或 .tar.gz)
console.log('正在解压 modules.tar...');
try {
execSync(`tar -xzf "${tarFile}" -C "${extractPath}"`, { stdio: 'inherit', env });
console.log('成功将 modules.tar 解压到 ./');
} catch (err) {
console.error('解压 tar.gz 失败,尝试纯 tar 格式...');
execSync(`tar -xf "${tarFile}" -C "${extractPath}"`, { stdio: 'inherit', env });
console.log('成功将 modules.tar 解压到 ./');
}
// 清理下载的文件(可选)
fs.unlinkSync(tarFile);
console.log('已删除临时文件 modules.tar');
})
.catch((err) => {
console.error('下载或验证出错:', err.message);
process.exit(1);
});
} catch (err) {
console.error('解压 tar 文件时出错:', err.message);
process.exit(1);
}