-
Notifications
You must be signed in to change notification settings - Fork 169
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
Modified the isisVarInit.py script #3945
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#!/usr/bin/env python | ||
""" | ||
This program builds shell scripts that define the ISIS environment | ||
variables that are sourced during conda environment activation and | ||
removed at deactivation. | ||
|
||
If the directories don't exist, they are created. If their path is | ||
not specified, they are placed in $ISISROOT. | ||
""" | ||
|
||
import argparse | ||
import os | ||
from pathlib import Path | ||
|
||
# This is free and unencumbered software released into the public domain. | ||
# | ||
# The authors of ISIS do not claim copyright on the contents of this file. | ||
# For more details about the LICENSE terms and the AUTHORS, you will | ||
# find files of those names at the top level of this repository. | ||
# | ||
# SPDX-License-Identifier: CC0-1.0 | ||
|
||
|
||
def mkdir(p: Path): | ||
# This just wraps and reports on the directory creation: | ||
if p.exists(): | ||
print(f"{p} exists, don't need to create.") | ||
else: | ||
p.mkdir(parents=False) | ||
print(f"Created {p}") | ||
return | ||
|
||
|
||
def activate_text(shell: dict, env_vars: dict) -> str: | ||
"""Returns the formatted text to write to the activation script | ||
based on the passed dictionaries.""" | ||
|
||
lines = [shell["shebang"]] | ||
for k, v in env_vars.items(): | ||
lines.append(shell["activate"].format(k, v)) | ||
|
||
if shell["activate_extra"] != "": | ||
lines.append(shell["activate_extra"]) | ||
|
||
lines.append("cat $ISISROOT/version") | ||
|
||
return "\n".join(lines) | ||
|
||
|
||
def deactivate_text(shell: dict, env_vars: dict) -> str: | ||
"""Returns the formatted text to write to the deactivation script | ||
based on the passed dictionaries.""" | ||
|
||
lines = [shell["shebang"]] | ||
for k in env_vars.keys(): | ||
lines.append(shell["deactivate"].format(k)) | ||
|
||
return "\n".join(lines) | ||
|
||
|
||
# Set up and then parse the command line: | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
|
||
parser.add_argument( | ||
"-d", | ||
"--data-dir", | ||
default=os.environ["CONDA_PREFIX"] + "/data", | ||
type=Path, | ||
help="ISIS Data Directory, default: %(default)s", | ||
) | ||
parser.add_argument( | ||
"-t", | ||
"--test-dir", | ||
default=os.environ["CONDA_PREFIX"] + "/testData", | ||
type=Path, | ||
help="ISIS Test Data Directory, default: %(default)s", | ||
) | ||
|
||
parser.add_argument( | ||
"-a", | ||
"--ale-dir", | ||
default=os.environ["CONDA_PREFIX"] + "/aleData", | ||
type=Path, | ||
help="ISIS Ale Data Directory, default: %(default)s", | ||
) | ||
args = parser.parse_args() | ||
|
||
print("-- ISIS Data Directories --") | ||
# Create the data directories: | ||
mkdir(args.data_dir) | ||
mkdir(args.test_dir) | ||
mkdir(args.ale_dir) | ||
|
||
print("-- Conda activation and deactivation scripts --") | ||
# Create the conda activation and deactivation directories: | ||
activate_dir = Path(os.environ["CONDA_PREFIX"]) / "etc/conda/activate.d" | ||
deactivate_dir = Path(os.environ["CONDA_PREFIX"]) / "etc/conda/deactivate.d" | ||
|
||
mkdir(activate_dir) | ||
mkdir(deactivate_dir) | ||
|
||
# Set the environment variables to manage | ||
env_vars = dict( | ||
ISISROOT=os.environ["CONDA_PREFIX"], | ||
ISISDATA=args.data_dir, | ||
ISISTESTDATA=args.test_dir, | ||
ALESPICEROOT=args.ale_dir | ||
) | ||
|
||
# These dicts define the unique aspects of the shell languages | ||
# for setting and unsetting the environment variables. | ||
|
||
sh = dict( # this covers bash and zsh | ||
extension=".sh", | ||
shebang="#!/usr/bin/env sh", | ||
activate="export {}={}", | ||
activate_extra="", | ||
deactivate="unset {}" | ||
) | ||
|
||
csh = dict( | ||
extension=".csh", | ||
shebang="#!/usr/bin/env csh", | ||
activate="setenv {}={}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setenv doesn't use an =, this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I did mention that I didn't test it. |
||
activate_extra="source $CONDA_PREFIX/scripts/tabCompletion.csh", | ||
deactivate="unsetenv {}" | ||
) | ||
|
||
fish = dict( | ||
extension=".fish", | ||
shebang="#!/usr/bin/env fish", | ||
activate="set -gx {} {}", | ||
activate_extra="", | ||
deactivate="set -e {}" | ||
) | ||
|
||
# Loop over the shell types and create the correct scripts associated with | ||
# each: | ||
for shell in (sh, csh, fish): | ||
a_path = activate_dir / ("isis-activate" + shell["extension"]) | ||
a_path.write_text(activate_text(shell, env_vars)) | ||
print(f"Wrote {a_path}") | ||
|
||
d_path = deactivate_dir / ("isis-deactivate" + shell["extension"]) | ||
d_path.write_text(deactivate_text(shell, env_vars)) | ||
print(f"Wrote {d_path}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert this to a proper doc-string to match the other helper functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did. I also altered this to return a string instead of None. I don't like functions calling print() (unless I'm debugging).