ai-election-manipulation-cases / scripts /build-release-manifest.mjs
apol's picture
Fix Hub Viewer conversion fan-out
122b842 verified
Raw
History Blame Contribute Delete
3.58 kB
#!/usr/bin/env node
/**
* Build deterministic checksums for the frozen v0.4.0 release artifacts.
*
* This script does not build the ZIP or workbook. The workbook currently
* depends on an external runtime and is therefore hashed, not claimed to be
* reproducible from the public npm dependency graph.
*/
import fs from "node:fs/promises";
import path from "node:path";
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const RELEASE_VERSION = "0.4.0";
const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(here, "..");
const releaseDir = path.join(root, "release");
const packageRoot = `agency-transfer-election-cases-v${RELEASE_VERSION}`;
const packageZipName = `${packageRoot}.zip`;
const requested = process.argv.slice(2);
const releasePattern = new RegExp(`-v${RELEASE_VERSION.replaceAll(".", "\\.")}\\.(?:zip|xlsx)$`);
const packageFiles = ["README.md", "CITATION.cff", "package.json", "data/datapackage.json"];
function sha256(buffer) {
return createHash("sha256").update(buffer).digest("hex");
}
function safeArtifactName(value) {
const name = path.basename(value);
if (name !== value || !releasePattern.test(name)) {
throw new Error(`Release artifact must be a plain v${RELEASE_VERSION} ZIP or XLSX filename: ${value}`);
}
return name;
}
function readZipMember(zipPath, member) {
const result = spawnSync("unzip", ["-p", zipPath, member], {
encoding: null,
maxBuffer: 128 * 1024 * 1024
});
if (result.error) throw result.error;
if (result.status !== 0) {
throw new Error(`Could not read ${member} from ${path.basename(zipPath)}: ${String(result.stderr || "").trim()}`);
}
return Buffer.from(result.stdout);
}
let artifactNames;
if (requested.length) {
artifactNames = requested.map(safeArtifactName);
} else {
artifactNames = (await fs.readdir(releaseDir)).filter((name) => releasePattern.test(name));
}
artifactNames = [...new Set(artifactNames)].sort();
if (!artifactNames.length) {
throw new Error(`No v${RELEASE_VERSION} ZIP/XLSX artifacts found in release/. Build them before checksums.`);
}
if (!artifactNames.includes(packageZipName)) {
throw new Error(`The frozen package ${packageZipName} is required to build release package-file checksums.`);
}
const artifacts = [];
for (const name of artifactNames) {
const buffer = await fs.readFile(path.join(releaseDir, name));
artifacts.push({ name, bytes: buffer.byteLength, sha256: sha256(buffer) });
}
const packageFileEntries = [];
const packageZipPath = path.join(releaseDir, packageZipName);
for (const name of packageFiles) {
const buffer = readZipMember(packageZipPath, `${packageRoot}/${name}`);
packageFileEntries.push({ name, bytes: buffer.byteLength, sha256: sha256(buffer) });
}
const manifest = {
manifest_version: 1,
dataset_version: RELEASE_VERSION,
artifacts,
package_files: packageFileEntries,
reproducibility: {
csv_and_parquet: "validated_from_pinned_public_dependencies",
workbook: "checksum_only_external_artifact_runtime_not_in_public_dependency_graph"
}
};
const sums = artifacts.map(({ name, sha256: digest }) => `${digest} ${name}`).join("\n") + "\n";
const manifestPath = path.join(releaseDir, "release-manifest.json");
const sumsPath = path.join(releaseDir, "SHA256SUMS");
await fs.writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
await fs.writeFile(sumsPath, sums);
process.stdout.write(`${JSON.stringify({ version: RELEASE_VERSION, artifacts: artifacts.length })}\n`);