|
1 | 1 | import * as fs from "fs"; |
2 | | -import * as os from "os"; |
3 | 2 | import * as path from "path"; |
4 | 3 |
|
5 | 4 | import * as core from "@actions/core"; |
6 | | -import * as io from "@actions/io"; |
7 | 5 |
|
8 | 6 | import * as input from "./input"; |
9 | 7 | import * as utils from "./utils"; |
10 | 8 |
|
11 | 9 | /** |
12 | | - * Clean up the conda cache directory |
| 10 | + * Clean up extracted packages from the conda packages directory, keeping only |
| 11 | + * the compressed archive cache and loose archive files. |
| 12 | + * |
| 13 | + * Instead of recursively deleting each extracted folder (extremely slow on |
| 14 | + * Windows due to filesystem overhead), we rename them to a sibling directory |
| 15 | + * on the same filesystem. The renamed directory is left for the ephemeral |
| 16 | + * runner VM to discard. |
13 | 17 | */ |
14 | 18 | async function run(): Promise<void> { |
15 | 19 | try { |
16 | 20 | const inputs = await core.group("Gathering Inputs...", input.parseInputs); |
17 | | - let pkgsDirs = utils.parsePkgsDirs(inputs.condaConfig.pkgs_dirs); |
| 21 | + const pkgsDirs = utils.parsePkgsDirs(inputs.condaConfig.pkgs_dirs); |
18 | 22 | if (!pkgsDirs.length) return; |
19 | 23 | core.startGroup( |
20 | 24 | "Removing uncompressed packages to trim down packages directory...", |
21 | 25 | ); |
22 | 26 | for (const pkgsDir of pkgsDirs) { |
23 | | - if (fs.existsSync(pkgsDir) && fs.lstatSync(pkgsDir).isDirectory()) { |
24 | | - let fullPath: string; |
25 | | - for (let folder_or_file of fs.readdirSync(pkgsDir)) { |
26 | | - fullPath = path.join(pkgsDir, folder_or_file); |
27 | | - if ( |
28 | | - fs.existsSync(fullPath) && |
29 | | - fs.lstatSync(fullPath).isDirectory() && |
30 | | - folder_or_file != "cache" |
31 | | - ) { |
32 | | - core.info(`Removing "${fullPath}"`); |
33 | | - try { |
34 | | - await io.rmRF(fullPath); |
35 | | - } catch (err) { |
36 | | - // If file could not be deleted, move to a temp folder |
37 | | - core.info(`Remove failed, moving "${fullPath}" to temp folder`); |
38 | | - await io.mv(fullPath, path.join(os.tmpdir(), folder_or_file)); |
39 | | - } |
40 | | - } |
| 27 | + if (!fs.existsSync(pkgsDir) || !fs.lstatSync(pkgsDir).isDirectory()) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + // Stash directory is a sibling to pkgsDir so rename stays on the |
| 32 | + // same filesystem and never fails with EXDEV |
| 33 | + const stashDir = `${pkgsDir}_stash_${Date.now()}`; |
| 34 | + fs.mkdirSync(stashDir, { recursive: true }); |
| 35 | + |
| 36 | + for (const entry of fs.readdirSync(pkgsDir)) { |
| 37 | + if (entry === "cache") continue; |
| 38 | + |
| 39 | + const fullPath = path.join(pkgsDir, entry); |
| 40 | + if (!fs.existsSync(fullPath) || !fs.lstatSync(fullPath).isDirectory()) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + const dest = path.join(stashDir, entry); |
| 45 | + core.info(`Stashing "${fullPath}"`); |
| 46 | + try { |
| 47 | + fs.renameSync(fullPath, dest); |
| 48 | + } catch (err) { |
| 49 | + core.warning(`Could not stash "${fullPath}": ${err}. Skipping.`); |
41 | 50 | } |
42 | 51 | } |
| 52 | + |
| 53 | + core.info( |
| 54 | + `Stashed extracted packages to "${stashDir}", ` + |
| 55 | + "will be discarded with the runner VM.", |
| 56 | + ); |
43 | 57 | } |
44 | 58 | core.endGroup(); |
45 | 59 | } catch (err) { |
|
0 commit comments