|
1 | | -#!/usr/bin/env node |
2 | | - |
3 | 1 | // SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors) |
4 | 2 | // |
5 | 3 | // SPDX-License-Identifier: MIT |
|
12 | 10 | // a full build exceeds. Looking each record up by name has no such limit and uses the same |
13 | 11 | // internal artifact API, so it costs nothing against the REST rate limit. |
14 | 12 | // |
15 | | -// Usage: BOARDS='<the scheduler job's "ports" output>' node ci_download_sizes.mjs <out-dir> |
16 | | -// Needs @actions/artifact where node can resolve it: `npm install` at the repository root. |
| 13 | +// That API needs the runner's ACTIONS_RUNTIME_TOKEN, which only `uses:` action steps get, |
| 14 | +// so build.yml calls downloadSizes() from an actions/github-script step rather than from |
| 15 | +// `node` in a `run:` step. @actions/artifact must be installed where this file can resolve |
| 16 | +// it: `npm install` at the repository root. |
17 | 17 |
|
18 | 18 | import { DefaultArtifactClient } from "@actions/artifact"; |
19 | 19 |
|
20 | | -const outDir = process.argv[2] ?? "sizes"; |
21 | | -const spec = JSON.parse(process.env.BOARDS || "{}"); |
22 | | -const boards = (spec.ports ?? []).flatMap((port) => spec[port]); |
23 | | -const client = new DefaultArtifactClient(); |
24 | | -const missing = []; |
25 | | -let downloaded = 0; |
26 | | - |
27 | | -async function fetchRecord(board) { |
28 | | - try { |
29 | | - const { artifact } = await client.getArtifact(`zz-sizes-${board}`); |
30 | | - await client.downloadArtifact(artifact.id, { path: outDir }); |
31 | | - downloaded++; |
32 | | - } catch (error) { |
33 | | - missing.push(board); |
34 | | - console.log(`No size record for ${board}: ${error.message}`); |
35 | | - } |
| 20 | +// The boards in the scheduler job's "ports" output: {<port>: [boards...], ports: [<port>...]}. |
| 21 | +export function boardsFromSchedule(schedule) { |
| 22 | + return (schedule.ports ?? []).flatMap((port) => schedule[port]); |
36 | 23 | } |
37 | 24 |
|
38 | | -// A few downloads at a time: enough to get through a full build in a minute or two, |
39 | | -// few enough not to trip the artifact service's throttling. |
40 | | -const queue = [...boards]; |
41 | | -await Promise.all( |
42 | | - Array.from({ length: 8 }, async () => { |
43 | | - while (queue.length) { |
44 | | - await fetchRecord(queue.shift()); |
| 25 | +export async function downloadSizes({ boards, outDir, info = console.log, warning = console.warn }) { |
| 26 | + const client = new DefaultArtifactClient(); |
| 27 | + const missing = []; |
| 28 | + let downloaded = 0; |
| 29 | + |
| 30 | + async function fetchRecord(board) { |
| 31 | + try { |
| 32 | + const { artifact } = await client.getArtifact(`zz-sizes-${board}`); |
| 33 | + await client.downloadArtifact(artifact.id, { path: outDir }); |
| 34 | + downloaded++; |
| 35 | + } catch (error) { |
| 36 | + missing.push(board); |
| 37 | + info(`No size record for ${board}: ${error.message}`); |
45 | 38 | } |
46 | | - }), |
47 | | -); |
| 39 | + } |
| 40 | + |
| 41 | + // A few downloads at a time: enough to get through a full build in a minute or two, |
| 42 | + // few enough not to trip the artifact service's throttling. |
| 43 | + const queue = [...boards]; |
| 44 | + await Promise.all( |
| 45 | + Array.from({ length: 8 }, async () => { |
| 46 | + while (queue.length) { |
| 47 | + await fetchRecord(queue.shift()); |
| 48 | + } |
| 49 | + }), |
| 50 | + ); |
48 | 51 |
|
49 | | -console.log(`Downloaded ${downloaded} of ${boards.length} size records to ${outDir}`); |
50 | | -if (missing.length) { |
51 | | - console.log(`::warning::No firmware size record for ${missing.length} board(s): ${missing.join(" ")}`); |
| 52 | + info(`Downloaded ${downloaded} of ${boards.length} size records to ${outDir}`); |
| 53 | + if (missing.length) { |
| 54 | + warning(`No firmware size record for ${missing.length} board(s): ${missing.join(" ")}`); |
| 55 | + } |
| 56 | + return { downloaded, missing }; |
52 | 57 | } |
0 commit comments