-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecosystem.config.js
More file actions
151 lines (123 loc) · 3.92 KB
/
ecosystem.config.js
File metadata and controls
151 lines (123 loc) · 3.92 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
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
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const PREFERRED_PYTHON_VERSIONS = ["3.10.8", "3.10"];
const FALLBACK_BINARIES = ["python3.10", "python3", "python"];
function collectCandidates(versionSpecs) {
const normalizedVersionSpecs = Array.isArray(versionSpecs) ? versionSpecs : [versionSpecs];
const candidates = [];
const explicitEnv = process.env.PYENV_PYTHON_3_10_8 || process.env.SCRAPLING_PYTHON_PATH;
if (explicitEnv) {
candidates.push(explicitEnv);
}
const potentialRoots = new Set();
try {
const pyenvRoot = execSync("pyenv root", { encoding: "utf8" }).trim();
if (pyenvRoot) {
potentialRoots.add(pyenvRoot);
}
} catch (error) {
// Ignore errors when pyenv is not available in PATH.
}
[
process.env.PYENV,
process.env.PYENV_ROOT,
process.env.HOME && path.join(process.env.HOME, ".pyenv"),
process.env.USERPROFILE && path.join(process.env.USERPROFILE, ".pyenv", "pyenv-win"),
process.env.USERPROFILE && path.join(process.env.USERPROFILE, ".pyenv"),
]
.filter(Boolean)
.forEach((root) => potentialRoots.add(root));
normalizedVersionSpecs.forEach((versionSpec) => {
potentialRoots.forEach((root) => {
buildPyenvPaths(root, versionSpec).forEach((resolvedPath) => {
candidates.push(resolvedPath);
});
});
});
return candidates;
}
function buildPyenvPaths(root, versionSpec) {
const isWindows = process.platform === "win32";
const paths = [];
const baseRelativePath = isWindows
? ["versions", versionSpec, "python.exe"]
: ["versions", versionSpec, "bin", "python"];
paths.push(path.join(root, ...baseRelativePath));
const isPartialVersion = versionSpec.split(".").length < 3;
if (!isPartialVersion) {
return paths;
}
const versionsDir = path.join(root, "versions");
try {
const entries = fs.readdirSync(versionsDir, { withFileTypes: true });
const matching = entries
.filter((entry) => entry.isDirectory() && entry.name.startsWith(`${versionSpec}.`))
.map((entry) => entry.name)
.sort((a, b) => compareVersionStrings(b, a));
matching.forEach((versionName) => {
const execRelativePath = isWindows
? ["versions", versionName, "python.exe"]
: ["versions", versionName, "bin", "python"];
paths.push(path.join(root, ...execRelativePath));
});
} catch (error) {
// Ignore missing versions directories or read errors.
}
return paths;
}
function compareVersionStrings(left, right) {
const leftParts = left.split(".").map(Number);
const rightParts = right.split(".").map(Number);
const maxLength = Math.max(leftParts.length, rightParts.length);
for (let index = 0; index < maxLength; index += 1) {
const difference = (leftParts[index] || 0) - (rightParts[index] || 0);
if (difference !== 0) {
return difference;
}
}
return 0;
}
function findPyenvPython(versionSpecs) {
const candidates = collectCandidates(versionSpecs);
for (const candidate of candidates) {
if (candidate && fs.existsSync(candidate)) {
return candidate;
}
}
return null;
}
function commandExists(command) {
const lookup = process.platform === "win32" ? `where ${command}` : `command -v ${command}`;
try {
execSync(lookup, { stdio: "ignore" });
return true;
} catch (error) {
return false;
}
}
function findPythonBinary(versionSpecs) {
const pyenvBinary = findPyenvPython(versionSpecs);
if (pyenvBinary) {
return pyenvBinary;
}
for (const command of FALLBACK_BINARIES) {
if (commandExists(command)) {
return command;
}
}
return "python";
}
const pythonBinary = findPythonBinary(PREFERRED_PYTHON_VERSIONS);
module.exports = {
apps: [
{
name: "scrapling-api",
script: pythonBinary,
args: "-m uvicorn app.main:app --host 0.0.0.0 --port 5680",
interpreter: "none",
cwd: __dirname,
autorestart: true,
},
],
};