Imports the previously untracked working tree as-is so that the review changes that follow (requirements -> design -> implementation) can be reviewed as diffs and reverted if needed. No content changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
6.2 KiB
JavaScript
117 lines
6.2 KiB
JavaScript
import { createHash, randomUUID } from 'node:crypto';
|
|
import { execFile } from 'node:child_process';
|
|
import { readFile, writeFile, mkdir, rename, rm, stat } from 'node:fs/promises';
|
|
import { dirname, join } from 'node:path';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
import { promisify } from 'node:util';
|
|
|
|
const lock = JSON.parse(await readFile(new URL('./python-debugger.lock.json', import.meta.url), 'utf8'));
|
|
const maxBytes = 64 * 1024 * 1024;
|
|
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
const run = promisify(execFile);
|
|
|
|
export async function installPythonDebugger({
|
|
platform = `${process.platform}-${process.arch}`,
|
|
archive,
|
|
pythonArchive,
|
|
pyrightArchive,
|
|
directory = fileURLToPath(new URL('../plugins/', import.meta.url)),
|
|
fetcher = fetch,
|
|
} = {}) {
|
|
const expected = lock.platforms[platform];
|
|
if (!expected) throw new Error(`Python debugger package is not pinned for ${platform}`);
|
|
const filename = `${lock.id}-${lock.version}@${platform}.vsix`;
|
|
const dependency = lock.pythonDependency;
|
|
const python = await installArchive({
|
|
expected: dependency.sha256, filename: `${dependency.id}-${dependency.version}.vsix`,
|
|
id: dependency.id, version: dependency.version, url: dependency.url, archive: pythonArchive, directory, fetcher,
|
|
});
|
|
const debuggerExtension = await installArchive({
|
|
expected, filename, archive, directory, fetcher, id: lock.id, version: lock.version,
|
|
url: `https://open-vsx.org/api/ms-python/debugpy/${platform}/${lock.version}/file/${filename}`,
|
|
});
|
|
const languageServer = await installArchive({
|
|
expected: lock.languageServer.sha256,
|
|
filename: `${lock.languageServer.id}-${lock.languageServer.version}.vsix`,
|
|
id: lock.languageServer.id, version: lock.languageServer.version,
|
|
url: lock.languageServer.url, archive: pyrightArchive, directory, fetcher,
|
|
});
|
|
return { version: lock.version, platform, python, debugger: debuggerExtension, languageServer };
|
|
}
|
|
|
|
async function installArchive({ expected, filename, url, archive, directory, fetcher, id, version }) {
|
|
const target = join(directory, '.downloads', filename);
|
|
let bytes;
|
|
let cached = false;
|
|
if (!archive) {
|
|
try {
|
|
bytes = await readFile(target);
|
|
if (sha256(bytes) !== expected) throw new Error(`Cached Python debugger checksum mismatch: ${target}`);
|
|
cached = true;
|
|
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
}
|
|
if (archive) {
|
|
if ((await stat(archive)).size > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB');
|
|
bytes = await readFile(archive);
|
|
} else if (!cached) {
|
|
const response = await fetcher(url, { signal: AbortSignal.timeout(120_000) });
|
|
if (!response.ok || !response.body) throw new Error(`Python debugger download failed: HTTP ${response.status}`);
|
|
if (Number(response.headers.get('content-length')) > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB');
|
|
const chunks = [];
|
|
let size = 0;
|
|
for await (const chunk of response.body) {
|
|
size += chunk.length;
|
|
if (size > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB');
|
|
chunks.push(chunk);
|
|
}
|
|
bytes = Buffer.concat(chunks);
|
|
}
|
|
if (sha256(bytes) !== expected) throw new Error('Python debugger checksum mismatch; nothing was installed');
|
|
await mkdir(dirname(target), { recursive: true });
|
|
const temporary = `${target}.${randomUUID()}.partial`;
|
|
try {
|
|
await writeFile(temporary, bytes, { flag: 'wx', mode: 0o644 });
|
|
await rename(temporary, target);
|
|
} finally { await rm(temporary, { force: true }); }
|
|
// Theia's local-dir resolver requires unpacked system plugins. Keep VSIX files
|
|
// in its explicitly ignored dot-directory and verify before every extraction.
|
|
const unpacked = join(directory, `.${id}-${randomUUID()}`);
|
|
const plugin = join(directory, id);
|
|
const previous = `${unpacked}-previous`;
|
|
let moved = false;
|
|
try {
|
|
await run('python3', ['-c', `
|
|
import json,os,pathlib,stat,sys,zipfile
|
|
archive,target,expected_id,expected_version=sys.argv[1:]
|
|
with zipfile.ZipFile(archive) as z:
|
|
entries=z.infolist()
|
|
if len(entries)>100000 or sum(i.file_size for i in entries)>268435456: raise ValueError('Plugin extraction size limit')
|
|
for item in entries:
|
|
p=pathlib.PurePosixPath(item.filename)
|
|
if p.is_absolute() or '..' in p.parts or '\\\\' in item.filename or stat.S_ISLNK(item.external_attr>>16): raise ValueError('Unsafe plugin archive path')
|
|
manifest=json.loads(z.read('extension/package.json'))
|
|
if manifest.get('publisher')+'.'+manifest.get('name')!=expected_id or manifest.get('version')!=expected_version: raise ValueError('Plugin identity mismatch')
|
|
z.extractall(target)
|
|
for item in entries:
|
|
if not item.is_dir(): os.chmod(pathlib.Path(target)/item.filename, 0o755 if (item.external_attr>>16)&0o111 else 0o644)
|
|
`, target, unpacked, id, version], { timeout: 30_000 });
|
|
try { await rename(plugin, previous); moved = true; } catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
try { await rename(unpacked, plugin); } catch (error) { if (moved) await rename(previous, plugin); throw error; }
|
|
await rm(previous, { recursive: true, force: true });
|
|
} finally { await rm(unpacked, { recursive: true, force: true }); }
|
|
return { target: plugin, archive: target, sha256: expected, cached };
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
const args = process.argv.slice(2);
|
|
const flags = new Map();
|
|
for (let index = 0; index < args.length; index += 2) flags.set(args[index], args[index + 1]);
|
|
if (args.length % 2 || [...flags].some(([key, value]) => !['--archive', '--python-archive', '--pyright-archive'].includes(key) || !value) || flags.size * 2 !== args.length) {
|
|
console.error('Usage: node scripts/download-python-debugger.mjs [--archive /path/to/debugpy.vsix] [--python-archive /path/to/python.vsix] [--pyright-archive /path/to/pyright.vsix]');
|
|
process.exitCode = 1;
|
|
} else {
|
|
try { console.log(JSON.stringify(await installPythonDebugger({ archive: flags.get('--archive'), pythonArchive: flags.get('--python-archive'), pyrightArchive: flags.get('--pyright-archive') }))); }
|
|
catch (error) { console.error(error.message); process.exitCode = 1; }
|
|
}
|
|
}
|