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

Modified the isisVarInit.py script #3945

Merged
merged 3 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
# This just wraps and reports on the directory creation:
Copy link
Contributor

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.

Copy link
Contributor Author

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).

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 {}={}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setenv doesn't use an =, this should be setenv {} {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}")