From d1ed1cbbf6ad24e94062b916a9909024b40b9f3d Mon Sep 17 00:00:00 2001 From: Ross Beyer Date: Mon, 27 Jul 2020 11:29:42 -0700 Subject: [PATCH] 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). --- README.md | 6 +- isis/scripts/isis3VarInit.py | 175 ----------------------------------- isis/scripts/isisVarInit.py | 146 +++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 178 deletions(-) delete mode 100755 isis/scripts/isis3VarInit.py create mode 100755 isis/scripts/isisVarInit.py diff --git a/README.md b/README.md index f40ed0af64..af22d34ca8 100644 --- a/README.md +++ b/README.md @@ -92,15 +92,15 @@ This installation guide is for ISIS users interested in installing ISIS (3.6.0)+ #Execute the ISIS variable initialization script with default arguments. #This script prepares default values for: $ISISROOT, $ISISDATA, $ISISTESTDATA - python $CONDA_PREFIX/scripts/isis3VarInit.py + python $CONDA_PREFIX/scripts/isisVarInit.py Executing this script with no arguments will result in $ISISDATA=$CONDA\_PREFIX/data, and $ISISTESTDATA=$CONDA\_PREFIX/testdata. The user can specify different directories for both of these optional values: - python $CONDA_PREFIX/scripts/isis3VarInit.py --data-dir=[path to data directory] --test-dir=[path to test data directory] + python $CONDA_PREFIX/scripts/isisVarInit.py --data-dir=[path to data directory] --test-dir=[path to test data directory] - More information about the ISISDATA environment variable and the ISIS Data Area can be found [here]("#The-ISIS-Data-Area"). Now everytime the isis environment is activated, $ISISROOT, $ISISDATA, and $ISISTESTDATA will be set to the values passed to isis3VarInit.py. This does not happen retroactively, re-activate the isis envionment with one of the following commands: + More information about the ISISDATA environment variable and the ISIS Data Area can be found [here]("#The-ISIS-Data-Area"). Now everytime the isis environment is activated, $ISISROOT, $ISISDATA, and $ISISTESTDATA will be set to the values passed to isisVarInit.py. This does not happen retroactively, so re-activate the isis envionment with one of the following commands: for Anaconda 3.4 and up - conda activate isis prior to Anaconda 3.4 - source activate isis diff --git a/isis/scripts/isis3VarInit.py b/isis/scripts/isis3VarInit.py deleted file mode 100755 index ca0793517c..0000000000 --- a/isis/scripts/isis3VarInit.py +++ /dev/null @@ -1,175 +0,0 @@ -#!/usr/bin/env python -""" -This program builds shell scripts that define ISIS3 environment variables during -conda environment activation and deactivation, and creates some directories. -""" - -import argparse -import os -import sys - -########################################################################################################## -# -# This work is free and unencumbered software released into the public domain. -# In jurisdictions that recognize copyright laws, the author or authors -# of this software dedicate any and all copyright interest in the -# software to the public domain. -# -# -# Description: This program builds the shell scripts that define the -# ISISROOT/ISISDATA/ISISTESTDATA environment variables for the user -# when the ISIS3 conda environment is activated, and clean up when it is -# deactivated. -# -# The data directory and test directory are optional command line arguments. -# If the user chooses not to set them, they will both be created in the -# $ISISROOT directory. -# -# History: -# Author: Tyler Wilson, USGS -# Date: 2018-10-05 -# Description: Initial commit. -# -# Author: Tyler Wilson, USGS -# Date: 2018-11-01 -# Description: Removed a pair of lines which were causing output errors on Mac OS X and were not -# required anyway. -# -# Author: Ross Beyer -# Date: 2018-11-19 -# Description: Streamlined the program, improved documentation, and made the directory and -# file creation more `pythonic' rather than using system calls. -# -# Author: Jesse Mapel -# Date: 2019-03-25 -# Description: Added C-Shell support. -# -# Author: K.-Michael Aye -# Date: 2019-08-09 -# Description: Added support for fish shell. -# -# Author: Adam Paquette -# Date: 2019-12-02 -# Description: Added ALESPICEROOT environment variable for ISIS 4. -# -########################################################################################################## - -# There are still a lot of Python 2 installations out there, and if people don't have -# their conda environment set up properly, the error message they'll get will be hard -# to decipher. This might help: -assert sys.version_info >= (3, 2) # Must be using Python 3.2 or later - - -# This just wraps and reports on the directory creation: -def mkdir(p): - if os.path.exists(p): - print("Tried to create " + p + ", but it already exists.") - else: - os.makedirs(p) - print("Created " + p) - return - - -# 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", - help="ISIS3 Data Directory, default: %(default)s", -) -parser.add_argument( - "-t", - "--test-dir", - default=os.environ["CONDA_PREFIX"] + "/testData", - help="ISIS3 Test Data Directory, default: %(default)s", -) - -parser.add_argument( - "-a", - "--ale-dir", - default=os.environ["CONDA_PREFIX"] + "/aleData", - help="ISIS3 Ale Data Directory, default: %(default)s", -) -args = parser.parse_args() - -# Create the data directories: -mkdir(args.data_dir) -mkdir(args.test_dir) -mkdir(args.ale_dir) - -# Create the conda activation and deactivation directories: -activate_dir = os.environ["CONDA_PREFIX"] + "/etc/conda/activate.d" -deactivate_dir = os.environ["CONDA_PREFIX"] + "/etc/conda/deactivate.d" - -mkdir(activate_dir) -mkdir(deactivate_dir) - -# Write the files that manage the ISIS3 environments: -# Three coralated lists for zsh, csh, and fish shell - -# Associated file -activate_deactivate_files = ["/env_vars.sh", "/env_vars.csh", "/env_vars.fish"] - -# Activation script -activate_scripts = [""" - #!/usr/bin/env sh - export ISISROOT={} - export ISISDATA={} - export ISISTESTDATA={} - export ALESPICEROOT={} - """, - """ - #!/usr/bin/env csh - setenv ISISROOT {} - setenv ISISDATA {} - setenv ISISTESTDATA {} - setenv ALESPICEROOT {} - - source $CONDA_PREFIX/scripts/tabCompletion.csh - """, - """ - #!/usr/bin/env fish - set -gx ISISROOT {} - set -gx ISISDATA {} - set -gx ISISTESTDATA {} - set -gx ALESPICEROOT {} - """] - -# Deactivation script -deactivate_scripts = [""" - #!/usr/bin/env sh - unset ISISROOT - unset ISISDATA - unset ISISTESTDATA - unset ALESPICEROOT - """, - """ - #!/usr/bin/env csh - unsetenv ISISROOT - unsetenv ISISDATA - unsetenv ISISTESTDATA - unsetenv ALESPICEROOT - """, - """ - #!/usr/bin/env fish - set -e ISISROOT - set -e ISISDATA - set -e ISISTESTDATA - set -e ALESPICEROOT - """] - -# Loop over the files and create the correct script associated with -# said file -for i, file in enumerate(activate_deactivate_files): - with open(activate_dir + file, mode="w") as a: - script = activate_scripts[i].format( - os.environ["CONDA_PREFIX"], args.data_dir, args.test_dir, args.ale_dir - ) - a.write(script) - print("Wrote " + activate_dir + file) - - with open(deactivate_dir + file, mode="w") as d: - d.write(deactivate_scripts[i]) - print("Wrote " + deactivate_dir + file) diff --git a/isis/scripts/isisVarInit.py b/isis/scripts/isisVarInit.py new file mode 100755 index 0000000000..bdaf84fb87 --- /dev/null +++ b/isis/scripts/isisVarInit.py @@ -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}")