Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function remove_module_lines can produce an invalid bash script #249

Closed
SeanBryan51 opened this issue Jan 31, 2024 · 2 comments
Closed

Function remove_module_lines can produce an invalid bash script #249

SeanBryan51 opened this issue Jan 31, 2024 · 2 comments
Assignees
Labels
bug Something isn't working priority:high High priority issues that should be included in the next release.

Comments

@SeanBryan51
Copy link
Collaborator

For compiling CABLE with a custom build script, benchcab will try to remove any lines that invoke the module command in the build script so that benchcab can load the modules specified in config.yaml when compiling the executables. This ensures that when we run CABLE we have the correct module dependencies loaded (see related issue: #94). This is done via the remove_module_lines function:

benchcab/benchcab/model.py

Lines 154 to 162 in dd9bc12

def remove_module_lines(file_path: Path) -> None:
"""Remove lines from `file_path` that call the environment modules package."""
with file_path.open("r", encoding="utf-8") as file:
contents = file.read()
with file_path.open("w", encoding="utf-8") as file:
for line in contents.splitlines(True):
cmds = shlex.split(line, comments=True)
if "module" not in cmds:
file.write(line)

However, this approach of removing lines from the bash script is brittle and can easily produce a broken bash script. For example, a bash script snippet such as the following would break when applying the above function:

if condition; then
    module load foo
fi

I think it is best to avoid modifying the build script as much as possible.

We could instead disable module invocations by creating a wrapper around the module command which prevents any module load or module add statements from being executed. To invoke the wrapper instead of the actual command when calling subprocess.run, we can prepend the path to the wrapper to the PATH environment variable (see here for using python subprocess with a modified environment). This approach could be relevant for #244 if the user specifies a list of shell commands to compile CABLE instead of a build script path.

@SeanBryan51 SeanBryan51 added the bug Something isn't working label Jan 31, 2024
@SeanBryan51 SeanBryan51 self-assigned this Feb 29, 2024
@SeanBryan51
Copy link
Collaborator Author

I'm going to attempt to implement this, we'll see how I go.

SeanBryan51 added a commit that referenced this issue Mar 1, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
@SeanBryan51 SeanBryan51 linked a pull request Mar 1, 2024 that will close this issue
SeanBryan51 added a commit that referenced this issue Mar 1, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
SeanBryan51 added a commit that referenced this issue Mar 1, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
SeanBryan51 added a commit that referenced this issue Mar 1, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
SeanBryan51 added a commit that referenced this issue Mar 1, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
SeanBryan51 added a commit that referenced this issue Mar 18, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
SeanBryan51 added a commit that referenced this issue Mar 21, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
SeanBryan51 added a commit that referenced this issue Mar 21, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
@SeanBryan51 SeanBryan51 added priority:medium Medium priority issues to become high priority issues after a release. priority:high High priority issues that should be included in the next release. and removed priority:medium Medium priority issues to become high priority issues after a release. labels Mar 21, 2024
SeanBryan51 added a commit that referenced this issue Mar 22, 2024
This change disables module commands that modify the environment via a
wrapper when running a custom build script. This avoids preprocessing
the custom build script for module statements which may produce an
invalid bash script. This also allows for module commands that do not
alter the environment to be executed as we only disable a subset of
subcommands in the wrapper.

Fixes #249
@SeanBryan51
Copy link
Collaborator Author

After discussing with @ccarouge, we have decided to add support for spack and to deprecate support for legacy CABLE build systems in benchcab. This adds the requirement that users will only be able to test benchcab on branches that can be built by spack. ACCESS-NRI will help with transitioning legacy build systems to use the latest build system compatible with spack.

Moving to spack will solve existing issues around supporting bespoke build scripts (e.g. #244, #249). This is the best way forward considering support for legacy build systems is a temporary that will be deprecated sooner or later.

Closing this issue.

@SeanBryan51 SeanBryan51 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority:high High priority issues that should be included in the next release.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant