-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified the isisVarInit.py script (#3945)
* Removed version number from filename. * feat: Changed the name of the VarInit script, changed how it worked internally, and made it write out isis-activate.* and isis_deactivate.* files. * Fix: Now has correct csh syntax, updated mkdir() with a proper docstring and to return a string (only main should print(), functions shouldn't or should log, but that's overkill for this).
- Loading branch information
Showing
3 changed files
with
149 additions
and
178 deletions.
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) -> str: | ||
"""Returns a string with a message about the creation or existance | ||
of the path, *p*.""" | ||
if p.exists(): | ||
return f"{p} exists, don't need to create." | ||
else: | ||
p.mkdir(parents=False) | ||
return f"Created {p}" | ||
|
||
|
||
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: | ||
print(mkdir(args.data_dir)) | ||
print(mkdir(args.test_dir)) | ||
print(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" | ||
|
||
print(mkdir(activate_dir)) | ||
print(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 {} {}", | ||
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}") |