Skip to content

Commit

Permalink
Modified the isisVarInit.py script (#3945)
Browse files Browse the repository at this point in the history
* 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
rbeyer authored Jul 27, 2020
1 parent 99c4d34 commit d1ed1cb
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 178 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
175 changes: 0 additions & 175 deletions isis/scripts/isis3VarInit.py

This file was deleted.

146 changes: 146 additions & 0 deletions isis/scripts/isisVarInit.py
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}")

0 comments on commit d1ed1cb

Please sign in to comment.