|
1 | 1 | // Usage: node fetch-python-package-resources.js --target-dir=$DIR --conda_env_dir=$DIR
|
2 |
| -const https = require('https'); |
3 |
| -const fs = require('fs'); |
4 |
| -const path = require('path'); |
5 |
| -const { HttpsProxyAgent } = require('https-proxy-agent'); |
| 2 | +const https = require('https') |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const { HttpsProxyAgent } = require('https-proxy-agent') |
6 | 6 |
|
7 |
| - |
8 |
| -const argv = require('minimist')(process.argv.slice(2)); |
| 7 | +const argv = require('minimist')(process.argv.slice(2)) |
9 | 8 | const targetDirArg = argv.target_dir
|
10 | 9 | const condaEnvDirArg = argv.conda_env_dir
|
11 | 10 |
|
12 | 11 | if (!targetDirArg || !condaEnvDirArg) {
|
13 |
| - console.error('Usage: node fetch-python-package-resources.js --target_dir=$DIR --conda_env_dir=$DIR\n'); |
14 |
| - process.exit(1); |
| 12 | + console.error( |
| 13 | + 'Usage: node fetch-python-package-resources.js --target_dir=$DIR --conda_env_dir=$DIR\n', |
| 14 | + ) |
| 15 | + process.exit(1) |
15 | 16 | }
|
16 | 17 |
|
17 |
| -const targetDir = path.resolve(targetDirArg); |
| 18 | +const targetDir = path.resolve(targetDirArg) |
18 | 19 | const condaTargetDir = path.join(targetDir, 'conda-env-lib')
|
19 |
| -const condaEnvLibraryDir = path.resolve(path.join(condaEnvDirArg, 'Library')); |
| 20 | +const condaEnvLibraryDir = path.resolve(path.join(condaEnvDirArg, 'Library')) |
20 | 21 |
|
21 |
| -const embeddablePythonUrl = 'https://raw.githubusercontent.com/adang1345/PythonWindows/master/3.11.10/python-3.11.10-embed-amd64.zip'; |
| 22 | +const embeddablePythonUrl = |
| 23 | + 'https://raw.githubusercontent.com/adang1345/PythonWindows/master/3.11.10/python-3.11.10-embed-amd64.zip' |
22 | 24 | const getPipScriptUrl = 'https://bootstrap.pypa.io/get-pip.py'
|
23 | 25 | const sevenZrExeUrl = 'https://www.7-zip.org/a/7zr.exe'
|
24 | 26 |
|
25 | 27 | function fetchFileIfNotPresent(url) {
|
26 |
| - const expectedFilePath = path.join(targetDir, getBaseFileName(url)) |
27 |
| - if (fs.existsSync(expectedFilePath)) { |
28 |
| - console.log(`omitting fetching of ${url} as ${expectedFilePath} already exists`) |
29 |
| - } else { |
30 |
| - fetchFile(url) |
31 |
| - } |
| 28 | + const expectedFilePath = path.join(targetDir, getBaseFileName(url)) |
| 29 | + if (fs.existsSync(expectedFilePath)) { |
| 30 | + console.log(`omitting fetching of ${url} as ${expectedFilePath} already exists`) |
| 31 | + } else { |
| 32 | + fetchFile(url) |
| 33 | + } |
32 | 34 | }
|
33 | 35 |
|
34 | 36 | function fetchFile(url) {
|
35 |
| - const proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy; |
36 |
| - const options = proxy ? { agent: new HttpsProxyAgent(proxy) } : {}; |
37 |
| - https.get(url, options, (response) => { |
38 |
| - const filePath = path.join(targetDir, getBaseFileName(url)) |
39 |
| - const file = fs.createWriteStream(filePath); |
40 |
| - response.pipe(file); |
| 37 | + const proxy = |
| 38 | + process.env.HTTPS_PROXY || |
| 39 | + process.env.https_proxy || |
| 40 | + process.env.HTTP_PROXY || |
| 41 | + process.env.http_proxy |
| 42 | + const options = proxy ? { agent: new HttpsProxyAgent(proxy) } : {} |
| 43 | + https |
| 44 | + .get(url, options, (response) => { |
| 45 | + const filePath = path.join(targetDir, getBaseFileName(url)) |
| 46 | + const file = fs.createWriteStream(filePath) |
| 47 | + response.pipe(file) |
41 | 48 |
|
42 |
| - file.on('finish', () => { |
43 |
| - file.close(); |
44 |
| - console.log(`Downloaded ${filePath} successfully!`); |
45 |
| - }); |
46 |
| - }).on('error', (err) => { |
47 |
| - console.error(`Error downloading ${embeddablePythonUrl}: ${err}`); |
48 |
| - }); |
| 49 | + file.on('finish', () => { |
| 50 | + file.close() |
| 51 | + console.log(`Downloaded ${filePath} successfully!`) |
| 52 | + }) |
| 53 | + }) |
| 54 | + .on('error', (err) => { |
| 55 | + console.error(`Error downloading ${embeddablePythonUrl}: ${err}`) |
| 56 | + }) |
49 | 57 | }
|
50 | 58 |
|
51 |
| - |
52 | 59 | function getBaseFileName(url) {
|
53 |
| - const urlPathSegments = url.split('/'); |
54 |
| - const baseFileName = urlPathSegments[urlPathSegments.length - 1] |
55 |
| - return baseFileName; |
| 60 | + const urlPathSegments = url.split('/') |
| 61 | + const baseFileName = urlPathSegments[urlPathSegments.length - 1] |
| 62 | + return baseFileName |
56 | 63 | }
|
57 | 64 |
|
58 | 65 | function prepareTargetPath() {
|
59 |
| - if (!fs.existsSync(condaTargetDir)) { |
60 |
| - fs.mkdirSync(targetDir, { recursive: true }); |
61 |
| - } |
| 66 | + if (!fs.existsSync(condaTargetDir)) { |
| 67 | + fs.mkdirSync(targetDir, { recursive: true }) |
| 68 | + } |
62 | 69 | }
|
63 | 70 |
|
64 | 71 | function copyLibuvDllsIfNotPresent() {
|
65 |
| - if (fs.existsSync(path.join(condaTargetDir, 'Library', 'bin', 'uv.dll'))) { |
66 |
| - console.log(`omitting fetching copying of libuv DLLs, as they already exist`) |
67 |
| - } else { |
68 |
| - if (!path.join(condaEnvLibraryDir, 'bin', 'uv.dll')) { |
69 |
| - console.log(`provided conda env at ${condaEnvLibraryDir} is missing uv.dll. Aborting`) |
70 |
| - process.exit(1); |
71 |
| - } |
72 |
| - fs.cp(condaEnvLibraryDir, path.join(condaTargetDir, 'Library'), { recursive: true }, (err) => { |
73 |
| - if (err) { |
74 |
| - console.error(err); |
75 |
| - console.log('Failed to copy directory'); |
76 |
| - process.exit(1) |
77 |
| - } else { |
78 |
| - console.log('Directory copied successfully'); |
79 |
| - } |
80 |
| - }); |
| 72 | + if (fs.existsSync(path.join(condaTargetDir, 'Library', 'bin', 'uv.dll'))) { |
| 73 | + console.log(`omitting fetching copying of libuv DLLs, as they already exist`) |
| 74 | + } else { |
| 75 | + if (!path.join(condaEnvLibraryDir, 'bin', 'uv.dll')) { |
| 76 | + console.log(`provided conda env at ${condaEnvLibraryDir} is missing uv.dll. Aborting`) |
| 77 | + process.exit(1) |
81 | 78 | }
|
| 79 | + fs.cp(condaEnvLibraryDir, path.join(condaTargetDir, 'Library'), { recursive: true }, (err) => { |
| 80 | + if (err) { |
| 81 | + console.error(err) |
| 82 | + console.log('Failed to copy directory') |
| 83 | + process.exit(1) |
| 84 | + } else { |
| 85 | + console.log('Directory copied successfully') |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
82 | 89 | }
|
83 | 90 |
|
84 |
| - |
85 | 91 | function main() {
|
86 |
| - prepareTargetPath() |
87 |
| - fetchFileIfNotPresent(embeddablePythonUrl) |
88 |
| - fetchFileIfNotPresent(getPipScriptUrl) |
89 |
| - fetchFileIfNotPresent(sevenZrExeUrl) |
90 |
| - copyLibuvDllsIfNotPresent() |
| 92 | + prepareTargetPath() |
| 93 | + fetchFileIfNotPresent(embeddablePythonUrl) |
| 94 | + fetchFileIfNotPresent(getPipScriptUrl) |
| 95 | + fetchFileIfNotPresent(sevenZrExeUrl) |
| 96 | + copyLibuvDllsIfNotPresent() |
91 | 97 | }
|
92 | 98 |
|
93 | 99 | main()
|
0 commit comments