diff --git a/changelog.d/+env-scrub-policy.feature.md b/changelog.d/+env-scrub-policy.feature.md new file mode 100644 index 0000000..66ae8fd --- /dev/null +++ b/changelog.d/+env-scrub-policy.feature.md @@ -0,0 +1,2 @@ +`env_scrub.childClearNames` returns the install-location names in +`src/env-scrub.json`, in the order `eb-local env` prints them. diff --git a/docs/orgmode/reference/env-scrub.org b/docs/orgmode/reference/env-scrub.org new file mode 100644 index 0000000..b933a98 --- /dev/null +++ b/docs/orgmode/reference/env-scrub.org @@ -0,0 +1,27 @@ +#+TITLE: Variables cleared for a child +#+OPTIONS: toc:nil todo:nil num:nil author:nil title:nil +#+STARTUP: content + +* Variables cleared for a child + +=src/env-scrub.json= is a JSON array of environment names. +[[zig:zig_common.env_scrub.childClearNames]] returns them in the order the file has them, which is the order =eb-local env= prints its =unset= lines. +A child command clears each name, so an install location inherited from the parent shell does not decide where the child writes. + +| Name | +|-------------------------------------| +| =EESSI_CVMFS_INSTALL= | +| =EESSI_SITE_INSTALL= | +| =EESSI_USER_INSTALL= | +| =EESSI_PROJECT_INSTALL= | +| =EESSI_SITE_SOFTWARE_PREFIX= | +| =EESSI_SOFTWARE_SUBDIR_OVERRIDE= | +| =EESSI_ACCELERATOR_TARGET_OVERRIDE= | +| =EESSI_OVERRIDE_GPU_CHECK= | +| =EASYBUILD_INSTALLPATH= | +| =EASYBUILD_INSTALLPATH_SOFTWARE= | +| =EASYBUILD_INSTALLPATH_MODULES= | +| =EASYBUILD_PREFIX= | + +eb-staged-rebuild also clears =EASYBUILD_REMOVE_GHOST_INSTALL_DIRS=, and =EASYBUILD_BUILDPATH= when the build path is private. +Those two are that tool's own. diff --git a/docs/orgmode/reference/index.org b/docs/orgmode/reference/index.org index 6589374..4f0fc49 100644 --- a/docs/orgmode/reference/index.org +++ b/docs/orgmode/reference/index.org @@ -14,4 +14,5 @@ Each page states facts in the order the thing itself has them. cli-conventions compatibility api + env-scrub #+end_export diff --git a/src/env-scrub.json b/src/env-scrub.json new file mode 100644 index 0000000..6b30520 --- /dev/null +++ b/src/env-scrub.json @@ -0,0 +1,14 @@ +[ + "EESSI_CVMFS_INSTALL", + "EESSI_SITE_INSTALL", + "EESSI_USER_INSTALL", + "EESSI_PROJECT_INSTALL", + "EESSI_SITE_SOFTWARE_PREFIX", + "EESSI_SOFTWARE_SUBDIR_OVERRIDE", + "EESSI_ACCELERATOR_TARGET_OVERRIDE", + "EESSI_OVERRIDE_GPU_CHECK", + "EASYBUILD_INSTALLPATH", + "EASYBUILD_INSTALLPATH_SOFTWARE", + "EASYBUILD_INSTALLPATH_MODULES", + "EASYBUILD_PREFIX" +] diff --git a/src/env_scrub.zig b/src/env_scrub.zig new file mode 100644 index 0000000..fca9e4a --- /dev/null +++ b/src/env_scrub.zig @@ -0,0 +1,83 @@ +//! Install-location names a child command clears before it runs. +//! +//! `env-scrub.json` is the list. The names are the ones `eb-local env` +//! prints, in that order: the European Environment for Scientific Software +//! Installations (EESSI) install locations, the overrides that move a stack, +//! and EasyBuild's install paths. eb-staged-rebuild also clears +//! `EASYBUILD_REMOVE_GHOST_INSTALL_DIRS`, and `EASYBUILD_BUILDPATH` when the +//! build path is private. Those two stay with that tool. +const std = @import("std"); +const env = @import("env.zig"); + +const source = @embedFile("env-scrub.json"); + +fn countQuoted(comptime text: []const u8) usize { + @setEvalBranchQuota(100_000); + var n: usize = 0; + var i: usize = 0; + while (i < text.len) : (i += 1) { + if (text[i] != '"') continue; + n += 1; + i += 1; + while (i < text.len and text[i] != '"') : (i += 1) {} + } + return n; +} + +fn quoted(comptime text: []const u8) [countQuoted(text)][]const u8 { + @setEvalBranchQuota(100_000); + var out: [countQuoted(text)][]const u8 = undefined; + var n: usize = 0; + var i: usize = 0; + while (i < text.len) : (i += 1) { + if (text[i] != '"') continue; + const start = i + 1; + i += 1; + while (i < text.len and text[i] != '"') : (i += 1) {} + out[n] = text[start..i]; + n += 1; + } + return out; +} + +const names = quoted(source); + +comptime { + @setEvalBranchQuota(100_000); + if (names.len == 0) @compileError("env-scrub.json has no names"); + for (names, 0..) |name, i| { + if (std.mem.indexOfScalar(u8, name, '\\') != null) @compileError("env-scrub.json: " ++ name); + if (!env.validName(name)) @compileError("env-scrub.json: " ++ name); + for (names[i + 1 ..]) |later| { + if (std.mem.eql(u8, name, later)) @compileError("env-scrub.json repeats " ++ name); + } + } +} + +/// The names in `env-scrub.json`, in file order. +pub const child_clear: []const []const u8 = &names; + +/// The names in `env-scrub.json`, in file order. +pub fn childClearNames() []const []const u8 { + return child_clear; +} + +test "childClearNames is the install-location list, in env's print order" { + const expect = [_][]const u8{ + "EESSI_CVMFS_INSTALL", + "EESSI_SITE_INSTALL", + "EESSI_USER_INSTALL", + "EESSI_PROJECT_INSTALL", + "EESSI_SITE_SOFTWARE_PREFIX", + "EESSI_SOFTWARE_SUBDIR_OVERRIDE", + "EESSI_ACCELERATOR_TARGET_OVERRIDE", + "EESSI_OVERRIDE_GPU_CHECK", + "EASYBUILD_INSTALLPATH", + "EASYBUILD_INSTALLPATH_SOFTWARE", + "EASYBUILD_INSTALLPATH_MODULES", + "EASYBUILD_PREFIX", + }; + const got = childClearNames(); + try std.testing.expectEqual(expect.len, got.len); + for (expect, got) |want, have| try std.testing.expectEqualStrings(want, have); +} diff --git a/src/root.zig b/src/root.zig index 58fbc0f..18e2c8c 100644 --- a/src/root.zig +++ b/src/root.zig @@ -11,6 +11,8 @@ pub const logs = @import("logs.zig"); pub const cli = @import("cli.zig"); /// The environment as the C library sees it. pub const env = @import("env.zig"); +/// Install-location names a child command clears. +pub const env_scrub = @import("env_scrub.zig"); /// Private state files in a directory other users may write. pub const state = @import("state.zig"); /// JSON as the Python tools these programs replace read and wrote it.