Commit 6026d729 authored by feichtinger's avatar feichtinger

Merge branch '95-tool-and-recipe-for-installing-jupyter-kernel-wrappers' into 'master'

Resolve "Tool and recipe for installing jupyter kernel wrappers"

Closes #95

See merge request Pmodules/buildblocks!131
parents 832c4808 414a46ff
#!/bin/bash
# Script to install logging into the activation hook directories
# of environments
#
# 2020 - Derek Feichtinger
function envlist() {
conda env list --json | jq -r '.envs[]' | grep -E -v 'conda$'
}
if [[ -z "$CONDA_PREFIX" ]]; then
echo "Error: No conda distribution seems active (CONDA_PREFIX)" >&2
exit 1
fi
for env in $(envlist); do
if [[ -e "$env/etc" ]]; then
echo "Installing into $env"
mkdir -p $env/etc/conda/activate.d
cat <<'EOF' > $env/etc/conda/activate.d/log-envname.sh
#!/bin/bash
logger "activated conda module: ${CONDA_PREFIX}"
EOF
else
echo "WARNING: SKIPPING ENV: $env has no /etc" >&2
fi
done
#!/usr/bin/env python
# installs a jupyter kernel wrapper solution into the environment's
# $ENVPATH/share/jupyter/kernels/python3/kernel.json kernel definition
# typical envpath at PSI
# "/opt/psi/Programming/anaconda/2019.07/conda/envs/datascience_py37"
import re
import os
import sys
import shutil
from datetime import datetime
import argparse
parser = argparse.ArgumentParser()
# DEFAULTS
verbose_flag = False
parser.add_argument("envpath", help="conda env path")
parser.add_argument("-v", "--verbose", help="verbose mode",action="store_true")
args = parser.parse_args()
if args.verbose:
verbose_flag = True
envpath = args.envpath
envname = os.path.basename(envpath)
condaroot = re.sub(r'(.*)/conda/envs/.*', r'\1', envpath)
kwrapper = os.path.join(condaroot, 'admintools/kernelwrapper.sh')
kfile = os.path.join(envpath,"share/jupyter/kernels/python3/kernel.json")
date_time = datetime.now().strftime("%Y%m%d-%H%M%S")
if verbose_flag:
print("env: %s" % envname)
print(kwrapper)
if not os.path.isfile(kwrapper):
sys.stderr.write("Error: no wrapper found at %s\n" % (kwrapper))
sys.exit(1)
searchstr = r'"argv": \[\n( *"/.*python", *)'
subststr = r'"argv": [\n' + ' "' + kwrapper + '",\n' + ' "' + envname + r'",\n\1'
try:
with open(kfile,"r") as fp:
filestr = "".join(fp.readlines())
except IOError:
sys.stderr.write("Error: Env has no kernel file: %s\n" % kfile)
sys.exit(0)
if re.search(kwrapper, filestr):
if verbose_flag:
print("env %s already contains kernelwrapper" % envpath)
sys.exit(0)
if re.search(searchstr, filestr):
filestr = re.sub(searchstr,
subststr,
filestr)
print(filestr)
bupfile = kfile + "-" + date_time + ".bup"
if verbose_flag:
print("copying original file to %s" % bupfile)
shutil.copyfile(kfile, bupfile)
if verbose_flag:
print("writing new file %s" % kfile)
print(filestr)
with open(kfile,"w") as fp:
fp.write(filestr)
else:
sys.stderr.write("ERROR: Could not do substitution in %s" % kfile)
sys.stderr.write(filestr)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment