@platforma-sdk/package-builder-lib 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/conda-dockerfile.template +45 -0
- package/assets/python-dockerfile.template +22 -0
- package/dist/_virtual/_rolldown/runtime.cjs +33 -0
- package/dist/_virtual/_rolldown/runtime.js +13 -0
- package/dist/core/archive.cjs +52 -0
- package/dist/core/archive.cjs.map +1 -0
- package/dist/core/archive.js +47 -0
- package/dist/core/archive.js.map +1 -0
- package/dist/core/conda/builder.cjs +226 -0
- package/dist/core/conda/builder.cjs.map +1 -0
- package/dist/core/conda/builder.js +220 -0
- package/dist/core/conda/builder.js.map +1 -0
- package/dist/core/conda/validate-spec.cjs +76 -0
- package/dist/core/conda/validate-spec.cjs.map +1 -0
- package/dist/core/conda/validate-spec.js +73 -0
- package/dist/core/conda/validate-spec.js.map +1 -0
- package/dist/core/core.cjs +403 -0
- package/dist/core/core.cjs.map +1 -0
- package/dist/core/core.d.ts +82 -0
- package/dist/core/core.d.ts.map +1 -0
- package/dist/core/core.js +397 -0
- package/dist/core/core.js.map +1 -0
- package/dist/core/docker-conda.cjs +75 -0
- package/dist/core/docker-conda.cjs.map +1 -0
- package/dist/core/docker-conda.js +71 -0
- package/dist/core/docker-conda.js.map +1 -0
- package/dist/core/docker-python.cjs +147 -0
- package/dist/core/docker-python.cjs.map +1 -0
- package/dist/core/docker-python.js +144 -0
- package/dist/core/docker-python.js.map +1 -0
- package/dist/core/docker.cjs +223 -0
- package/dist/core/docker.cjs.map +1 -0
- package/dist/core/docker.js +207 -0
- package/dist/core/docker.js.map +1 -0
- package/dist/core/envs.cjs +58 -0
- package/dist/core/envs.cjs.map +1 -0
- package/dist/core/envs.d.ts +26 -0
- package/dist/core/envs.d.ts.map +1 -0
- package/dist/core/envs.js +51 -0
- package/dist/core/envs.js.map +1 -0
- package/dist/core/package-info.cjs +450 -0
- package/dist/core/package-info.cjs.map +1 -0
- package/dist/core/package-info.d.ts +57 -0
- package/dist/core/package-info.d.ts.map +1 -0
- package/dist/core/package-info.js +447 -0
- package/dist/core/package-info.js.map +1 -0
- package/dist/core/paths.cjs +15 -0
- package/dist/core/paths.cjs.map +1 -0
- package/dist/core/paths.js +14 -0
- package/dist/core/paths.js.map +1 -0
- package/dist/core/resolver.cjs +99 -0
- package/dist/core/resolver.cjs.map +1 -0
- package/dist/core/resolver.js +95 -0
- package/dist/core/resolver.js.map +1 -0
- package/dist/core/schemas/artifacts.cjs +136 -0
- package/dist/core/schemas/artifacts.cjs.map +1 -0
- package/dist/core/schemas/artifacts.d.ts +280 -0
- package/dist/core/schemas/artifacts.d.ts.map +1 -0
- package/dist/core/schemas/artifacts.js +122 -0
- package/dist/core/schemas/artifacts.js.map +1 -0
- package/dist/core/schemas/entrypoints.cjs +43 -0
- package/dist/core/schemas/entrypoints.cjs.map +1 -0
- package/dist/core/schemas/entrypoints.d.ts +32 -0
- package/dist/core/schemas/entrypoints.d.ts.map +1 -0
- package/dist/core/schemas/entrypoints.js +41 -0
- package/dist/core/schemas/entrypoints.js.map +1 -0
- package/dist/core/schemas/sw-json.cjs +111 -0
- package/dist/core/schemas/sw-json.cjs.map +1 -0
- package/dist/core/schemas/sw-json.js +110 -0
- package/dist/core/schemas/sw-json.js.map +1 -0
- package/dist/core/storage.cjs +86 -0
- package/dist/core/storage.cjs.map +1 -0
- package/dist/core/storage.js +83 -0
- package/dist/core/storage.js.map +1 -0
- package/dist/core/sw-json-render.cjs +327 -0
- package/dist/core/sw-json-render.cjs.map +1 -0
- package/dist/core/sw-json-render.js +322 -0
- package/dist/core/sw-json-render.js.map +1 -0
- package/dist/core/util.cjs +313 -0
- package/dist/core/util.cjs.map +1 -0
- package/dist/core/util.d.ts +66 -0
- package/dist/core/util.d.ts.map +1 -0
- package/dist/core/util.js +281 -0
- package/dist/core/util.js.map +1 -0
- package/dist/defaults.cjs +54 -0
- package/dist/defaults.cjs.map +1 -0
- package/dist/defaults.d.ts +21 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +37 -0
- package/dist/defaults.js.map +1 -0
- package/dist/index.cjs +31 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
- package/src/__test__/Dockerfile +1 -0
- package/src/__test__/docker.test.ts +67 -0
- package/src/__test__/package.json +39 -0
- package/src/__test__/python-example/main.py +1 -0
- package/src/__test__/python-example/requirements.txt +0 -0
- package/src/__test__/vite.config.mts +7 -0
- package/src/core/__tests__/docker-conda.test.ts +145 -0
- package/src/core/__tests__/docker-entrypoint-generation.test.ts +119 -0
- package/src/core/__tests__/docker-python.test.ts +374 -0
- package/src/core/__tests__/publish-docker-images.test.ts +309 -0
- package/src/core/archive.ts +102 -0
- package/src/core/conda/builder.test.ts +71 -0
- package/src/core/conda/builder.ts +384 -0
- package/src/core/conda/validate-spec.test.ts +213 -0
- package/src/core/conda/validate-spec.ts +107 -0
- package/src/core/core.ts +835 -0
- package/src/core/docker-conda.ts +149 -0
- package/src/core/docker-python.ts +283 -0
- package/src/core/docker.ts +300 -0
- package/src/core/envs.ts +31 -0
- package/src/core/package-info.test.ts +82 -0
- package/src/core/package-info.ts +843 -0
- package/src/core/paths.ts +16 -0
- package/src/core/resolver.ts +195 -0
- package/src/core/schemas/artifacts.test.ts +72 -0
- package/src/core/schemas/artifacts.ts +279 -0
- package/src/core/schemas/entrypoints.test.ts +171 -0
- package/src/core/schemas/entrypoints.ts +150 -0
- package/src/core/schemas/sw-json.ts +216 -0
- package/src/core/schemas/test-artifacts.ts +362 -0
- package/src/core/storage.ts +125 -0
- package/src/core/sw-json-render.test.ts +215 -0
- package/src/core/sw-json-render.ts +682 -0
- package/src/core/util.ts +390 -0
- package/src/defaults.ts +38 -0
- package/src/index.ts +31 -0
- package/src/requirements.txt +1 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { assertNever, currentOS, currentPlatform, splitPlatform } from "../util.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import fsp from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import * as undici from "undici";
|
|
7
|
+
//#region src/core/conda/builder.ts
|
|
8
|
+
const defaultEnvironmentName = "default";
|
|
9
|
+
var micromamba = class {
|
|
10
|
+
constructor(logger, rootPrefix, binaryVersion = "latest", binaryPath = "") {
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
this.rootPrefix = rootPrefix;
|
|
13
|
+
this.binaryVersion = binaryVersion;
|
|
14
|
+
this.binaryPath = binaryPath;
|
|
15
|
+
if (!binaryPath) this.binaryPath = path.join(rootPrefix, "micromamba");
|
|
16
|
+
if (currentOS() === "windows" && !this.binaryPath.endsWith(".exe")) this.binaryPath += ".exe";
|
|
17
|
+
}
|
|
18
|
+
async downloadBinary() {
|
|
19
|
+
if (fs.existsSync(this.binaryPath)) try {
|
|
20
|
+
await fsp.access(this.binaryPath, fs.constants.X_OK);
|
|
21
|
+
this.logger.debug(`micromamba binary '${this.binaryPath}' exists and executable. Download was skipped`);
|
|
22
|
+
return;
|
|
23
|
+
} catch {
|
|
24
|
+
this.logger.debug(`file exists but is not executable: ${this.binaryPath}. Fixing permissions`);
|
|
25
|
+
await fsp.chmod(this.binaryPath, 493);
|
|
26
|
+
}
|
|
27
|
+
await downloadMicromamba(this.logger, {
|
|
28
|
+
platform: currentPlatform(),
|
|
29
|
+
outputPath: this.binaryPath,
|
|
30
|
+
version: this.binaryVersion
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
getVersion() {
|
|
34
|
+
return this.execSync(["--version"]);
|
|
35
|
+
}
|
|
36
|
+
createEnvironment(opts) {
|
|
37
|
+
const { environmentName, environmentPrefix, specFile } = opts;
|
|
38
|
+
this.logger.debug(`Creating conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`);
|
|
39
|
+
const args = [
|
|
40
|
+
"env",
|
|
41
|
+
"create",
|
|
42
|
+
"--file",
|
|
43
|
+
specFile,
|
|
44
|
+
"--yes"
|
|
45
|
+
];
|
|
46
|
+
if (environmentPrefix) args.push("--prefix", environmentPrefix);
|
|
47
|
+
else if (environmentName) args.push("--name", environmentName);
|
|
48
|
+
else args.push("--name", defaultEnvironmentName);
|
|
49
|
+
this.execSync(args, { stdio: "inherit" });
|
|
50
|
+
}
|
|
51
|
+
exportEnvironment(opts) {
|
|
52
|
+
const { environmentName, environmentPrefix, json, outputFile } = opts;
|
|
53
|
+
this.logger.debug(`Exporting conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`);
|
|
54
|
+
const file = outputFile ? fs.openSync(outputFile, "w") : "pipe";
|
|
55
|
+
const args = ["env", "export"];
|
|
56
|
+
if (environmentPrefix) args.push("--prefix", environmentPrefix);
|
|
57
|
+
else if (environmentName) args.push("--name", environmentName);
|
|
58
|
+
else args.push("--name", defaultEnvironmentName);
|
|
59
|
+
if (json) args.push("--json");
|
|
60
|
+
try {
|
|
61
|
+
const envInfo = this.execSync(args, { stdio: [
|
|
62
|
+
"inherit",
|
|
63
|
+
file,
|
|
64
|
+
"inherit"
|
|
65
|
+
] });
|
|
66
|
+
if (!outputFile) return envInfo;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (file !== "pipe") fs.closeSync(file);
|
|
69
|
+
if (outputFile) fs.rmSync(outputFile, { force: true });
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
if (file !== "pipe") fs.closeSync(file);
|
|
73
|
+
}
|
|
74
|
+
deleteEnvironment(opts) {
|
|
75
|
+
const { environmentName, environmentPrefix } = opts;
|
|
76
|
+
if (environmentPrefix) {
|
|
77
|
+
this.logger.debug(`Deleting conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`);
|
|
78
|
+
fs.rmSync(environmentPrefix, { recursive: true });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const result = this.exportEnvironment({
|
|
82
|
+
environmentName,
|
|
83
|
+
environmentPrefix,
|
|
84
|
+
json: true
|
|
85
|
+
});
|
|
86
|
+
const envInfo = JSON.parse(result ?? "{}");
|
|
87
|
+
if (envInfo.prefix) {
|
|
88
|
+
this.logger.debug(`Deleting conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`);
|
|
89
|
+
fs.rmSync(envInfo.prefix, { recursive: true });
|
|
90
|
+
} else {
|
|
91
|
+
const errMsg = "Failed to delete environment: cannot get environment location from micromamba tool";
|
|
92
|
+
this.logger.error(errMsg);
|
|
93
|
+
throw new Error(errMsg);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
execSync(args, opts) {
|
|
97
|
+
this.logger.debug(`Executing micromamba: ${this.binaryPath} ${args.join(" ")}`);
|
|
98
|
+
const result = spawnSync(this.binaryPath, args, {
|
|
99
|
+
encoding: "utf8",
|
|
100
|
+
stdio: "pipe",
|
|
101
|
+
...opts,
|
|
102
|
+
env: {
|
|
103
|
+
...process.env,
|
|
104
|
+
MAMBA_ROOT_PREFIX: this.rootPrefix,
|
|
105
|
+
CONDA_PKGS_DIRS: path.join(this.rootPrefix, "pkgs"),
|
|
106
|
+
...opts?.env
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
if (result.error) throw new Error(`Failed to get micromamba version: ${result.error.message}`);
|
|
110
|
+
if (result.status !== 0) {
|
|
111
|
+
const errMsg = `micromamba command failed with status ${result.status}: ${result.stderr?.toString().trim() ?? "no piped stderr"}`;
|
|
112
|
+
this.logger.error(errMsg);
|
|
113
|
+
throw new Error(errMsg);
|
|
114
|
+
}
|
|
115
|
+
if (!result.stdout) return;
|
|
116
|
+
return result.stdout.toString().trim();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
function micromambaDownloadUrl(version, platform) {
|
|
120
|
+
return version === "latest" ? `https://github.com/mamba-org/micromamba-releases/releases/latest/download/${micromambaAssetName(platform)}` : `https://github.com/mamba-org/micromamba-releases/releases/download/${version}/${micromambaAssetName(platform)}`;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Downloads micromamba binary for the specified platform and version
|
|
124
|
+
*/
|
|
125
|
+
async function downloadMicromamba(logger, options) {
|
|
126
|
+
const { version = "latest", platform, outputPath } = options;
|
|
127
|
+
const targetPlatform = platform ?? currentPlatform();
|
|
128
|
+
const binaryPath = outputPath;
|
|
129
|
+
logger.debug(`Downloading micromamba version ${version} for platform ${targetPlatform}`);
|
|
130
|
+
const downloadUrl = micromambaDownloadUrl(version, targetPlatform);
|
|
131
|
+
logger.debug(`Download URL: ${downloadUrl}`);
|
|
132
|
+
await downloadFile(logger, downloadUrl, binaryPath);
|
|
133
|
+
logger.debug(`making binary executable: ${binaryPath}`);
|
|
134
|
+
await fsp.chmod(binaryPath, 493);
|
|
135
|
+
logger.info(`Micromamba downloaded to: ${binaryPath}`);
|
|
136
|
+
return binaryPath;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get correct asset name with micromamba binary for particular platform,
|
|
140
|
+
* as they are available in micromamba-releases repo.
|
|
141
|
+
*/
|
|
142
|
+
function micromambaAssetName(platform) {
|
|
143
|
+
const { os, arch } = splitPlatform(platform);
|
|
144
|
+
switch (os) {
|
|
145
|
+
case "linux":
|
|
146
|
+
switch (arch) {
|
|
147
|
+
case "x64": return "micromamba-linux-64";
|
|
148
|
+
case "aarch64": return "micromamba-linux-aarch64";
|
|
149
|
+
default: assertNever(arch);
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
case "macosx":
|
|
153
|
+
switch (arch) {
|
|
154
|
+
case "x64": return "micromamba-osx-64";
|
|
155
|
+
case "aarch64": return "micromamba-osx-arm64";
|
|
156
|
+
default: assertNever(arch);
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
case "windows":
|
|
160
|
+
switch (arch) {
|
|
161
|
+
case "x64": return "micromamba-win-64";
|
|
162
|
+
case "aarch64": throw new Error("Micromamba does not support ARM64 on Windows");
|
|
163
|
+
default: assertNever(arch);
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
default: assertNever(os);
|
|
167
|
+
}
|
|
168
|
+
throw new Error("calm down linter, this code is unreachable");
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Downloads a file from URL to local path
|
|
172
|
+
*/
|
|
173
|
+
async function downloadFile(logger, url, outputPath) {
|
|
174
|
+
let response;
|
|
175
|
+
const maxRedirects = 5;
|
|
176
|
+
const maxAttempts = 3;
|
|
177
|
+
let redirCount = 0;
|
|
178
|
+
let attempt = 0;
|
|
179
|
+
for (; attempt <= maxAttempts;) {
|
|
180
|
+
attempt++;
|
|
181
|
+
logger.debug(`Trying to download: ${url}`);
|
|
182
|
+
try {
|
|
183
|
+
const requestResult = await undici.request(url, { method: "GET" });
|
|
184
|
+
response = {
|
|
185
|
+
statusCode: requestResult.statusCode,
|
|
186
|
+
location: requestResult.headers["location"]?.toString() ?? "",
|
|
187
|
+
body: requestResult.body
|
|
188
|
+
};
|
|
189
|
+
} catch (error) {
|
|
190
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
191
|
+
throw new Error(`Failed to make request: ${errorMessage}`);
|
|
192
|
+
}
|
|
193
|
+
if (response.statusCode === 301 || response.statusCode === 302) {
|
|
194
|
+
if (redirCount >= maxRedirects) throw new Error(`Too many redirects: ${redirCount}, last location: ${response.location}`);
|
|
195
|
+
logger.debug(`Processing redirect. New location: ${response.location}`);
|
|
196
|
+
redirCount++;
|
|
197
|
+
url = response.location;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (response.statusCode === 200) break;
|
|
201
|
+
logger.error(`Failed to download: ${response.statusCode}, attempt ${attempt}`);
|
|
202
|
+
}
|
|
203
|
+
if (response.statusCode !== 200) throw new Error(`Failed to download: last attempt status code: ${response.statusCode}`);
|
|
204
|
+
const fileStream = fs.createWriteStream(outputPath);
|
|
205
|
+
response.body.pipe(fileStream);
|
|
206
|
+
return new Promise((resolve, reject) => {
|
|
207
|
+
fileStream.on("finish", () => {
|
|
208
|
+
fileStream.close();
|
|
209
|
+
resolve();
|
|
210
|
+
});
|
|
211
|
+
fileStream.on("error", (err) => {
|
|
212
|
+
fs.unlink(outputPath, () => {});
|
|
213
|
+
reject(err);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
//#endregion
|
|
218
|
+
export { micromamba, micromambaDownloadUrl };
|
|
219
|
+
|
|
220
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","names":["util.currentOS","util.currentPlatform","util.splitPlatform"],"sources":["../../../src/core/conda/builder.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport fsp from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { spawnSync, type SpawnSyncOptions } from \"node:child_process\";\nimport * as undici from \"undici\";\nimport type winston from \"winston\";\n\nimport * as util from \"../util\";\n\nconst defaultEnvironmentName = \"default\";\n\nexport interface BuilderOptions {\n mambaRootPrefix: string; // root directory for all conda data\n logger: winston.Logger;\n micromambaVersion?: string; // use specific version of micromamba\n}\n\nexport interface MicromambaDownloadOptions {\n platform: util.PlatformType;\n outputPath: string; // output path for the binary\n\n version?: string; // defalut: latest\n}\n\nexport interface EnvCreateOptions {\n /** Environment name */\n environmentName?: string;\n /** Prefix where to create the environment */\n environmentPrefix?: string;\n /** Path to the environment specification file */\n specFile: string;\n}\n\nexport interface EnvExportOptions {\n /** Name of the environment to export */\n environmentName?: string;\n /** Prefix of the environment to export */\n environmentPrefix?: string;\n /** Export in JSON format */\n json?: boolean;\n /** Output file path for the exported specification */\n outputFile?: string;\n}\n\nexport interface EnvDeleteOptions {\n /** Environment name */\n environmentName?: string;\n /** Prefix where to create the environment */\n environmentPrefix?: string;\n}\n\nexport class micromamba {\n constructor(\n private readonly logger: winston.Logger,\n private readonly rootPrefix: string,\n private readonly binaryVersion: string = \"latest\",\n private readonly binaryPath: string = \"\",\n ) {\n if (!binaryPath) {\n this.binaryPath = path.join(rootPrefix, \"micromamba\");\n }\n\n if (util.currentOS() === \"windows\" && !this.binaryPath.endsWith(\".exe\")) {\n this.binaryPath += \".exe\";\n }\n }\n\n public async downloadBinary(): Promise<void> {\n if (fs.existsSync(this.binaryPath)) {\n try {\n await fsp.access(this.binaryPath, fs.constants.X_OK);\n this.logger.debug(\n `micromamba binary '${this.binaryPath}' exists and executable. Download was skipped`,\n );\n return;\n } catch {\n this.logger.debug(\n `file exists but is not executable: ${this.binaryPath}. Fixing permissions`,\n );\n await fsp.chmod(this.binaryPath, 0o755);\n }\n }\n\n await downloadMicromamba(this.logger, {\n platform: util.currentPlatform(),\n outputPath: this.binaryPath,\n version: this.binaryVersion,\n });\n }\n\n public getVersion(): string {\n return this.execSync([\"--version\"])!;\n }\n\n public createEnvironment(opts: EnvCreateOptions): void {\n const { environmentName, environmentPrefix, specFile } = opts;\n this.logger.debug(\n `Creating conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`,\n );\n\n const args = [\"env\", \"create\", \"--file\", specFile, \"--yes\"];\n if (environmentPrefix) {\n args.push(\"--prefix\", environmentPrefix);\n } else if (environmentName) {\n args.push(\"--name\", environmentName);\n } else {\n args.push(\"--name\", defaultEnvironmentName);\n }\n\n this.execSync(args, {\n stdio: \"inherit\",\n });\n }\n\n public exportEnvironment(opts: {\n environmentName?: string;\n environmentPrefix?: string;\n json?: boolean;\n }): string;\n public exportEnvironment(opts: {\n environmentName?: string;\n environmentPrefix?: string;\n json?: boolean;\n outputFile: string;\n }): void;\n public exportEnvironment(opts: EnvExportOptions): void | string {\n const { environmentName, environmentPrefix, json, outputFile } = opts;\n this.logger.debug(\n `Exporting conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`,\n );\n\n const file = outputFile ? fs.openSync(outputFile, \"w\") : \"pipe\";\n\n const args = [\"env\", \"export\"];\n if (environmentPrefix) {\n args.push(\"--prefix\", environmentPrefix);\n } else if (environmentName) {\n args.push(\"--name\", environmentName);\n } else {\n args.push(\"--name\", defaultEnvironmentName);\n }\n if (json) {\n args.push(\"--json\");\n }\n\n try {\n const envInfo = this.execSync(args, {\n stdio: [\"inherit\", file, \"inherit\"],\n });\n\n if (!outputFile) {\n return envInfo;\n }\n } catch (error) {\n if (file !== \"pipe\") {\n fs.closeSync(file);\n }\n if (outputFile) {\n fs.rmSync(outputFile, { force: true });\n }\n throw error;\n }\n\n if (file !== \"pipe\") {\n fs.closeSync(file);\n }\n }\n\n public deleteEnvironment(opts: EnvDeleteOptions): void {\n const { environmentName, environmentPrefix } = opts;\n\n if (environmentPrefix) {\n this.logger.debug(\n `Deleting conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`,\n );\n fs.rmSync(environmentPrefix, { recursive: true });\n return;\n }\n\n const result = this.exportEnvironment({ environmentName, environmentPrefix, json: true });\n const envInfo = JSON.parse(result ?? \"{}\") as Record<string, unknown>;\n\n if (envInfo.prefix) {\n this.logger.debug(\n `Deleting conda environment '${environmentName ?? environmentPrefix ?? defaultEnvironmentName}'...`,\n );\n fs.rmSync(envInfo.prefix as string, { recursive: true });\n } else {\n const errMsg =\n \"Failed to delete environment: cannot get environment location from micromamba tool\";\n this.logger.error(errMsg);\n throw new Error(errMsg);\n }\n }\n\n // FIXME: IMHO better to have async exec with promises. I just am not skilled enough to implement it fast. :(\n private execSync(args: string[], opts?: SpawnSyncOptions): string | void {\n this.logger.debug(`Executing micromamba: ${this.binaryPath} ${args.join(\" \")}`);\n\n const result = spawnSync(this.binaryPath, args, {\n encoding: \"utf8\",\n stdio: \"pipe\",\n ...opts,\n env: {\n ...process.env,\n MAMBA_ROOT_PREFIX: this.rootPrefix,\n CONDA_PKGS_DIRS: path.join(this.rootPrefix, \"pkgs\"),\n ...opts?.env,\n },\n });\n\n if (result.error) {\n throw new Error(`Failed to get micromamba version: ${result.error.message}`);\n }\n\n if (result.status !== 0) {\n const errMsg = `micromamba command failed with status ${result.status}: ${result.stderr?.toString().trim() ?? \"no piped stderr\"}`;\n this.logger.error(errMsg);\n throw new Error(errMsg);\n }\n\n if (!result.stdout) {\n return;\n }\n\n return result.stdout.toString().trim();\n }\n}\n\nexport function micromambaDownloadUrl(version: string, platform: util.PlatformType): string {\n return version === \"latest\"\n ? `https://github.com/mamba-org/micromamba-releases/releases/latest/download/${micromambaAssetName(platform)}`\n : `https://github.com/mamba-org/micromamba-releases/releases/download/${version}/${micromambaAssetName(platform)}`;\n}\n\n/**\n * Downloads micromamba binary for the specified platform and version\n */\nexport async function downloadMicromamba(\n logger: winston.Logger,\n options: MicromambaDownloadOptions,\n): Promise<string> {\n const { version = \"latest\", platform, outputPath } = options;\n\n const targetPlatform = platform ?? util.currentPlatform();\n const binaryPath = outputPath;\n\n logger.debug(`Downloading micromamba version ${version} for platform ${targetPlatform}`);\n\n const downloadUrl = micromambaDownloadUrl(version, targetPlatform);\n\n logger.debug(`Download URL: ${downloadUrl}`);\n\n await downloadFile(logger, downloadUrl, binaryPath);\n\n logger.debug(`making binary executable: ${binaryPath}`);\n await fsp.chmod(binaryPath, 0o755);\n\n logger.info(`Micromamba downloaded to: ${binaryPath}`);\n return binaryPath;\n}\n\n/**\n * Get correct asset name with micromamba binary for particular platform,\n * as they are available in micromamba-releases repo.\n */\nfunction micromambaAssetName(platform: util.PlatformType): string {\n const { os, arch } = util.splitPlatform(platform);\n switch (os) {\n case \"linux\": {\n switch (arch) {\n case \"x64\":\n return \"micromamba-linux-64\";\n case \"aarch64\":\n return \"micromamba-linux-aarch64\";\n default:\n util.assertNever(arch);\n }\n break;\n }\n case \"macosx\": {\n switch (arch) {\n case \"x64\":\n return \"micromamba-osx-64\";\n case \"aarch64\":\n return \"micromamba-osx-arm64\";\n default:\n util.assertNever(arch);\n }\n break;\n }\n case \"windows\": {\n switch (arch) {\n case \"x64\":\n return \"micromamba-win-64\";\n case \"aarch64\":\n throw new Error(\"Micromamba does not support ARM64 on Windows\");\n default:\n util.assertNever(arch);\n }\n break;\n }\n default:\n util.assertNever(os);\n }\n throw new Error(\"calm down linter, this code is unreachable\");\n}\n\n/**\n * Downloads a file from URL to local path\n */\nasync function downloadFile(\n logger: winston.Logger,\n url: string,\n outputPath: string,\n): Promise<void> {\n type responseInfo = {\n statusCode: number;\n body: NodeJS.ReadableStream;\n location: string;\n };\n\n let response: responseInfo;\n const maxRedirects = 5;\n const maxAttempts = 3;\n\n let redirCount = 0;\n let attempt = 0;\n for (; attempt <= maxAttempts; ) {\n attempt++;\n\n logger.debug(`Trying to download: ${url}`);\n try {\n const requestResult = await undici.request(url, {\n method: \"GET\",\n });\n response = {\n statusCode: requestResult.statusCode,\n location: requestResult.headers[\"location\"]?.toString() ?? \"\",\n body: requestResult.body,\n };\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n throw new Error(`Failed to make request: ${errorMessage}`);\n }\n\n if (response.statusCode === 301 || response.statusCode === 302) {\n if (redirCount >= maxRedirects) {\n throw new Error(`Too many redirects: ${redirCount}, last location: ${response.location}`);\n }\n\n logger.debug(`Processing redirect. New location: ${response.location}`);\n redirCount++;\n url = response.location;\n continue;\n }\n\n if (response.statusCode === 200) {\n break;\n }\n\n logger.error(`Failed to download: ${response.statusCode}, attempt ${attempt}`);\n }\n\n if (response!.statusCode !== 200) {\n throw new Error(`Failed to download: last attempt status code: ${response!.statusCode}`);\n }\n\n const fileStream = fs.createWriteStream(outputPath);\n\n response!.body.pipe(fileStream);\n\n return new Promise((resolve, reject) => {\n fileStream.on(\"finish\", () => {\n fileStream.close();\n resolve();\n });\n\n fileStream.on(\"error\", (err) => {\n fs.unlink(outputPath, () => {}); // Delete the file on error\n reject(err);\n });\n });\n}\n"],"mappings":";;;;;;;AASA,MAAM,yBAAyB;AA0C/B,IAAa,aAAb,MAAwB;CACtB,YACE,QACA,YACA,gBAAyC,UACzC,aAAsC,IACtC;AAJiB,OAAA,SAAA;AACA,OAAA,aAAA;AACA,OAAA,g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,76 @@
|
|
|
1
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
let node_fs = require("node:fs");
|
|
3
|
+
node_fs = require_runtime.__toESM(node_fs);
|
|
4
|
+
let yaml = require("yaml");
|
|
5
|
+
yaml = require_runtime.__toESM(yaml);
|
|
6
|
+
//#region src/core/conda/validate-spec.ts
|
|
7
|
+
/**
|
|
8
|
+
* Channels that are forbidden because they reference the official Anaconda channel.
|
|
9
|
+
* These channels (main, r, msys2, defaults, anaconda) should be avoided in favor of
|
|
10
|
+
* community-maintained channels like conda-forge to avoid licensing restrictions
|
|
11
|
+
*/
|
|
12
|
+
const FORBIDDEN_CHANNELS = [
|
|
13
|
+
"main",
|
|
14
|
+
"r",
|
|
15
|
+
"msys2",
|
|
16
|
+
"defaults",
|
|
17
|
+
"anaconda"
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Validates a conda spec file to ensure it doesn't use forbidden channels
|
|
21
|
+
* @param logger Winston logger instance
|
|
22
|
+
* @param specPath Path to the conda spec file
|
|
23
|
+
* @throws Error if forbidden channels are found
|
|
24
|
+
*/
|
|
25
|
+
function validateCondaSpec(logger, specPath) {
|
|
26
|
+
logger.debug(`Validating conda spec file: ${specPath}`);
|
|
27
|
+
if (!node_fs.default.existsSync(specPath)) throw new Error(`Conda spec file not found: ${specPath}`);
|
|
28
|
+
const specContent = node_fs.default.readFileSync(specPath, "utf-8");
|
|
29
|
+
const spec = yaml.default.parse(specContent);
|
|
30
|
+
if (!spec || typeof spec !== "object") throw new Error(`Invalid conda spec file format: ${specPath}`);
|
|
31
|
+
const errors = [];
|
|
32
|
+
if (spec.channels) {
|
|
33
|
+
const channelErrors = validateChannels(spec.channels);
|
|
34
|
+
if (channelErrors.length > 0) errors.push(...channelErrors);
|
|
35
|
+
}
|
|
36
|
+
if (spec.dependencies) {
|
|
37
|
+
const depErrors = validateDependencies(spec.dependencies);
|
|
38
|
+
if (depErrors.length > 0) errors.push(...depErrors);
|
|
39
|
+
}
|
|
40
|
+
if (errors.length > 0) throw new Error(`Conda spec file contains forbidden Anaconda channels:\n${errors.join("\n")}`);
|
|
41
|
+
logger.debug(`Conda spec file validation passed: ${specPath}`);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Validates that no forbidden channels are in the channels list
|
|
45
|
+
* @param channels Array of channel names
|
|
46
|
+
* @returns Array of error messages for forbidden channels
|
|
47
|
+
*/
|
|
48
|
+
function validateChannels(channels) {
|
|
49
|
+
const errors = [];
|
|
50
|
+
for (const channel of channels) {
|
|
51
|
+
const channelLower = channel.toLowerCase().trim();
|
|
52
|
+
if (FORBIDDEN_CHANNELS.includes(channelLower)) errors.push(` - Forbidden channel '${channel}' in channels list`);
|
|
53
|
+
}
|
|
54
|
+
return errors;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Validates that no dependencies use forbidden channel prefixes
|
|
58
|
+
* @param dependencies Array of dependencies (can be strings or objects)
|
|
59
|
+
* @returns Array of error messages for forbidden dependencies
|
|
60
|
+
*/
|
|
61
|
+
function validateDependencies(dependencies) {
|
|
62
|
+
const errors = [];
|
|
63
|
+
for (const dep of dependencies) {
|
|
64
|
+
if (typeof dep !== "string") continue;
|
|
65
|
+
if (dep.includes("::")) {
|
|
66
|
+
const [channel, ..._packageParts] = dep.split("::");
|
|
67
|
+
const channelLower = channel.toLowerCase().trim();
|
|
68
|
+
if (FORBIDDEN_CHANNELS.includes(channelLower)) errors.push(` - Forbidden channel prefix '${channel}::' in dependency '${dep}'`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return errors;
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
exports.validateCondaSpec = validateCondaSpec;
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=validate-spec.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-spec.cjs","names":["fs"],"sources":["../../../src/core/conda/validate-spec.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport yaml from \"yaml\";\nimport type winston from \"winston\";\n\nexport interface CondaSpec {\n channels?: string[];\n dependencies?: (string | Record<string, unknown>)[];\n name?: string;\n prefix?: string;\n [key: string]: unknown;\n}\n\n/**\n * Channels that are forbidden because they reference the official Anaconda channel.\n * These channels (main, r, msys2, defaults, anaconda) should be avoided in favor of\n * community-maintained channels like conda-forge to avoid licensing restrictions\n */\nconst FORBIDDEN_CHANNELS = [\"main\", \"r\", \"msys2\", \"defaults\", \"anaconda\"];\n\n/**\n * Validates a conda spec file to ensure it doesn't use forbidden channels\n * @param logger Winston logger instance\n * @param specPath Path to the conda spec file\n * @throws Error if forbidden channels are found\n */\nexport function validateCondaSpec(logger: winston.Logger, specPath: string): void {\n logger.debug(`Validating conda spec file: ${specPath}`);\n\n if (!fs.existsSync(specPath)) {\n throw new Error(`Conda spec file not found: ${specPath}`);\n }\n\n const specContent = fs.readFileSync(specPath, \"utf-8\");\n const spec = yaml.parse(specContent) as CondaSpec;\n\n if (!spec || typeof spec !== \"object\") {\n throw new Error(`Invalid conda spec file format: ${specPath}`);\n }\n\n const errors: string[] = [];\n\n if (spec.channels) {\n const channelErrors = validateChannels(spec.channels);\n if (channelErrors.length > 0) {\n errors.push(...channelErrors);\n }\n }\n\n if (spec.dependencies) {\n const depErrors = validateDependencies(spec.dependencies);\n if (depErrors.length > 0) {\n errors.push(...depErrors);\n }\n }\n\n if (errors.length > 0) {\n throw new Error(`Conda spec file contains forbidden Anaconda channels:\\n${errors.join(\"\\n\")}`);\n }\n\n logger.debug(`Conda spec file validation passed: ${specPath}`);\n}\n\n/**\n * Validates that no forbidden channels are in the channels list\n * @param channels Array of channel names\n * @returns Array of error messages for forbidden channels\n */\nfunction validateChannels(channels: string[]): string[] {\n const errors: string[] = [];\n\n for (const channel of channels) {\n const channelLower = channel.toLowerCase().trim();\n if (FORBIDDEN_CHANNELS.includes(channelLower)) {\n errors.push(` - Forbidden channel '${channel}' in channels list`);\n }\n }\n\n return errors;\n}\n\n/**\n * Validates that no dependencies use forbidden channel prefixes\n * @param dependencies Array of dependencies (can be strings or objects)\n * @returns Array of error messages for forbidden dependencies\n */\nfunction validateDependencies(dependencies: (string | Record<string, unknown>)[]): string[] {\n const errors: string[] = [];\n\n for (const dep of dependencies) {\n // Skip non-string dependencies (they might be pip dependencies, etc.)\n if (typeof dep !== \"string\") {\n continue;\n }\n\n // Check if dependency uses channel prefix (format: channel::package)\n if (dep.includes(\"::\")) {\n const [channel, ..._packageParts] = dep.split(\"::\");\n const channelLower = channel.toLowerCase().trim();\n\n if (FORBIDDEN_CHANNELS.includes(channelLower)) {\n errors.push(` - Forbidden channel prefix '${channel}::' in dependency '${dep}'`);\n }\n }\n }\n\n return errors;\n}\n"],"mappings":";;;;;;;;;;;AAiBA,MAAM,qBAAqB;CAAC;CAAQ;CAAK;CAAS;CAAY;CAAW;;;;;;;AAQzE,SAAgB,kBAAkB,QAAwB,UAAwB;AAChF,QAAO,MAAM,+BAA+B,WAAW;AAEvD,KAAI,CAACA,QAAAA,QAAG,WAAW,SAAS,CAC1B,OAAM,IAAI,MAAM,8BAA8B,WAAW;CAG3D,MAAM,cAAcA,QAAAA,QAAG,aAAa,UAAU,QAAQ;CACtD,MAAM,OAAO,KAAA,QAAK,MAAM,YAAY;AAEpC,KAAI,CAAC,QAAQ,OAAO,SAAS,SAC3B,OAAM,IAAI,MAAM,mCAAmC,WAAW;CAGhE,MAAM,SAAmB,EAAE;AAE3B,KAAI,KAAK,UAAU;EACjB,MAAM,gBAAgB,iBAAiB,KAAK,SAAS;AACrD,MAAI,cAAc,SAAS,EACzB,QAAO,KAAK,GAAG,cAAc;;AAIjC,KAAI,KAAK,cAAc;EACrB,MAAM,YAAY,qBAAqB,KAAK,aAAa;AACzD,MAAI,UAAU,SAAS,EACrB,QAAO,KAAK,GAAG,UAAU;;AAI7B,KAAI,OAAO,SAAS,EAClB,OAAM,IAAI,MAAM,0DAA0D,OAAO,KAAK,KAAK,GAAG;AAGhG,QAAO,MAAM,sCAAsC,WAAW;;;;;;;AAQhE,SAAS,iBAAiB,UAA8B;CACtD,MAAM,SAAmB,EAAE;AAE3B,MAAK,MAAM,WAAW,UAAU;EAC9B,MAAM,eAAe,QAAQ,aAAa,CAAC,MAAM;AACjD,MAAI,mBAAmB,SAAS,aAAa,CAC3C,QAAO,KAAK,0BAA0B,QAAQ,oBAAoB;;AAItE,QAAO;;;;;;;AAQT,SAAS,qBAAqB,cAA8D;CAC1F,MAAM,SAAmB,EAAE;AAE3B,MAAK,MAAM,OAAO,cAAc;AAE9B,MAAI,OAAO,QAAQ,SACjB;AAIF,MAAI,IAAI,SAAS,KAAK,EAAE;GACtB,MAAM,CAAC,SAAS,GAAG,iBAAiB,IAAI,MAAM,KAAK;GACnD,MAAM,eAAe,QAAQ,aAAa,CAAC,MAAM;AAEjD,OAAI,mBAAmB,SAAS,aAAa,CAC3C,QAAO,KAAK,iCAAiC,QAAQ,qBAAqB,IAAI,GAAG;;;AAKvF,QAAO"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import yaml from "yaml";
|
|
3
|
+
//#region src/core/conda/validate-spec.ts
|
|
4
|
+
/**
|
|
5
|
+
* Channels that are forbidden because they reference the official Anaconda channel.
|
|
6
|
+
* These channels (main, r, msys2, defaults, anaconda) should be avoided in favor of
|
|
7
|
+
* community-maintained channels like conda-forge to avoid licensing restrictions
|
|
8
|
+
*/
|
|
9
|
+
const FORBIDDEN_CHANNELS = [
|
|
10
|
+
"main",
|
|
11
|
+
"r",
|
|
12
|
+
"msys2",
|
|
13
|
+
"defaults",
|
|
14
|
+
"anaconda"
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* Validates a conda spec file to ensure it doesn't use forbidden channels
|
|
18
|
+
* @param logger Winston logger instance
|
|
19
|
+
* @param specPath Path to the conda spec file
|
|
20
|
+
* @throws Error if forbidden channels are found
|
|
21
|
+
*/
|
|
22
|
+
function validateCondaSpec(logger, specPath) {
|
|
23
|
+
logger.debug(`Validating conda spec file: ${specPath}`);
|
|
24
|
+
if (!fs.existsSync(specPath)) throw new Error(`Conda spec file not found: ${specPath}`);
|
|
25
|
+
const specContent = fs.readFileSync(specPath, "utf-8");
|
|
26
|
+
const spec = yaml.parse(specContent);
|
|
27
|
+
if (!spec || typeof spec !== "object") throw new Error(`Invalid conda spec file format: ${specPath}`);
|
|
28
|
+
const errors = [];
|
|
29
|
+
if (spec.channels) {
|
|
30
|
+
const channelErrors = validateChannels(spec.channels);
|
|
31
|
+
if (channelErrors.length > 0) errors.push(...channelErrors);
|
|
32
|
+
}
|
|
33
|
+
if (spec.dependencies) {
|
|
34
|
+
const depErrors = validateDependencies(spec.dependencies);
|
|
35
|
+
if (depErrors.length > 0) errors.push(...depErrors);
|
|
36
|
+
}
|
|
37
|
+
if (errors.length > 0) throw new Error(`Conda spec file contains forbidden Anaconda channels:\n${errors.join("\n")}`);
|
|
38
|
+
logger.debug(`Conda spec file validation passed: ${specPath}`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Validates that no forbidden channels are in the channels list
|
|
42
|
+
* @param channels Array of channel names
|
|
43
|
+
* @returns Array of error messages for forbidden channels
|
|
44
|
+
*/
|
|
45
|
+
function validateChannels(channels) {
|
|
46
|
+
const errors = [];
|
|
47
|
+
for (const channel of channels) {
|
|
48
|
+
const channelLower = channel.toLowerCase().trim();
|
|
49
|
+
if (FORBIDDEN_CHANNELS.includes(channelLower)) errors.push(` - Forbidden channel '${channel}' in channels list`);
|
|
50
|
+
}
|
|
51
|
+
return errors;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validates that no dependencies use forbidden channel prefixes
|
|
55
|
+
* @param dependencies Array of dependencies (can be strings or objects)
|
|
56
|
+
* @returns Array of error messages for forbidden dependencies
|
|
57
|
+
*/
|
|
58
|
+
function validateDependencies(dependencies) {
|
|
59
|
+
const errors = [];
|
|
60
|
+
for (const dep of dependencies) {
|
|
61
|
+
if (typeof dep !== "string") continue;
|
|
62
|
+
if (dep.includes("::")) {
|
|
63
|
+
const [channel, ..._packageParts] = dep.split("::");
|
|
64
|
+
const channelLower = channel.toLowerCase().trim();
|
|
65
|
+
if (FORBIDDEN_CHANNELS.includes(channelLower)) errors.push(` - Forbidden channel prefix '${channel}::' in dependency '${dep}'`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return errors;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { validateCondaSpec };
|
|
72
|
+
|
|
73
|
+
//# sourceMappingURL=validate-spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-spec.js","names":[],"sources":["../../../src/core/conda/validate-spec.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport yaml from \"yaml\";\nimport type winston from \"winston\";\n\nexport interface CondaSpec {\n channels?: string[];\n dependencies?: (string | Record<string, unknown>)[];\n name?: string;\n prefix?: string;\n [key: string]: unknown;\n}\n\n/**\n * Channels that are forbidden because they reference the official Anaconda channel.\n * These channels (main, r, msys2, defaults, anaconda) should be avoided in favor of\n * community-maintained channels like conda-forge to avoid licensing restrictions\n */\nconst FORBIDDEN_CHANNELS = [\"main\", \"r\", \"msys2\", \"defaults\", \"anaconda\"];\n\n/**\n * Validates a conda spec file to ensure it doesn't use forbidden channels\n * @param logger Winston logger instance\n * @param specPath Path to the conda spec file\n * @throws Error if forbidden channels are found\n */\nexport function validateCondaSpec(logger: winston.Logger, specPath: string): void {\n logger.debug(`Validating conda spec file: ${specPath}`);\n\n if (!fs.existsSync(specPath)) {\n throw new Error(`Conda spec file not found: ${specPath}`);\n }\n\n const specContent = fs.readFileSync(specPath, \"utf-8\");\n const spec = yaml.parse(specContent) as CondaSpec;\n\n if (!spec || typeof spec !== \"object\") {\n throw new Error(`Invalid conda spec file format: ${specPath}`);\n }\n\n const errors: string[] = [];\n\n if (spec.channels) {\n const channelErrors = validateChannels(spec.channels);\n if (channelErrors.length > 0) {\n errors.push(...channelErrors);\n }\n }\n\n if (spec.dependencies) {\n const depErrors = validateDependencies(spec.dependencies);\n if (depErrors.length > 0) {\n errors.push(...depErrors);\n }\n }\n\n if (errors.length > 0) {\n throw new Error(`Conda spec file contains forbidden Anaconda channels:\\n${errors.join(\"\\n\")}`);\n }\n\n logger.debug(`Conda spec file validation passed: ${specPath}`);\n}\n\n/**\n * Validates that no forbidden channels are in the channels list\n * @param channels Array of channel names\n * @returns Array of error messages for forbidden channels\n */\nfunction validateChannels(channels: string[]): string[] {\n const errors: string[] = [];\n\n for (const channel of channels) {\n const channelLower = channel.toLowerCase().trim();\n if (FORBIDDEN_CHANNELS.includes(channelLower)) {\n errors.push(` - Forbidden channel '${channel}' in channels list`);\n }\n }\n\n return errors;\n}\n\n/**\n * Validates that no dependencies use forbidden channel prefixes\n * @param dependencies Array of dependencies (can be strings or objects)\n * @returns Array of error messages for forbidden dependencies\n */\nfunction validateDependencies(dependencies: (string | Record<string, unknown>)[]): string[] {\n const errors: string[] = [];\n\n for (const dep of dependencies) {\n // Skip non-string dependencies (they might be pip dependencies, etc.)\n if (typeof dep !== \"string\") {\n continue;\n }\n\n // Check if dependency uses channel prefix (format: channel::package)\n if (dep.includes(\"::\")) {\n const [channel, ..._packageParts] = dep.split(\"::\");\n const channelLower = channel.toLowerCase().trim();\n\n if (FORBIDDEN_CHANNELS.includes(channelLower)) {\n errors.push(` - Forbidden channel prefix '${channel}::' in dependency '${dep}'`);\n }\n }\n }\n\n return errors;\n}\n"],"mappings":";;;;;;;;AAiBA,MAAM,qBAAqB;CAAC;CAAQ;CAAK;CAAS;CAAY;CAAW;;;;;;;AAQzE,SAAgB,kBAAkB,QAAwB,UAAwB;AAChF,QAAO,MAAM,+BAA+B,WAAW;AAEvD,KAAI,CAAC,GAAG,WAAW,SAAS,CAC1B,OAAM,IAAI,MAAM,8BAA8B,WAAW;CAG3D,MAAM,cAAc,GAAG,aAAa,UAAU,QAAQ;CACtD,MAAM,OA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