Skip to content
Snippets Groups Projects
Commit df896ef3 authored by Michael Langguth's avatar Michael Langguth
Browse files

Fixes to runscript-generator to work without active virtual environment (has to exist only).

parent ce1d5007
No related branches found
No related tags found
No related merge requests found
Pipeline #89861 failed
......@@ -13,10 +13,13 @@ import sys, os
import socket
if sys.version_info[0] < 3:
raise Exception("This script has to be run with Python 3!")
# append path to get runscript-generator scripts
sys.path.append(os.path.dirname(sys.path[0]))
from runscript_generator.config_utils import check_virtualenv
# sanity check (is Python running in a virtual environment)
_ = check_virtualenv(labort=True)
workdir = os.path.dirname(os.getcwd())
sys.path.append(os.path.join(workdir, "utils"))
import argparse
from runscript_generator.configurations import check_virtualenv
from runscript_generator.config_utils import Config_runscript_base
from runscript_generator.config_extraction import Config_Extraction
......@@ -51,7 +54,14 @@ def get_runscript_cls(target_runscript_name, venv_name, lhpc):
#
def main():
venv_name = check_virtualenv(labort=True)
parser = argparse.ArgumentParser()
parser.add_argument("--venv_path", "-venv", dest="venv_name", type=str, required=True,
help="Name of virtual environment to be used (created with create_env.sh).")
args = parser.parse_args()
venv_path = os.path.join(os.path.dirname(os.getcwd()), "virtual_envs", args.venv_name)
venv_name = check_virtualenv(lactive=False, venv_path=venv_path, labort=True)
# check if we are on a known HPC
lhpc = False
......
......@@ -10,7 +10,6 @@ HOST_NAME=`hostname`
echo "Start loading modules on ${HOST_NAME} required for preprocessing..."
echo "modules_preprocess.sh is subject to: "
echo "* data_extraction_era5.sh"
echo "* preprocess_data_era5_step1.sh"
module purge
......
......@@ -4,6 +4,8 @@ They are used for facilating the customized conversion of the preprocessing step
to executable runscripts
"""
import os, sys
# robust check if script is running in virtual env from
# https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv/38939054
def get_base_prefix_compat():
......@@ -25,26 +27,42 @@ def path_rec_split(full_path):
#--------------------------------------------------------------------------------------------------------
#
def in_virtualenv():
return get_base_prefix_compat() != sys.prefix
"""
New version! -> relies on "VIRTUAL_ENV" environmental variable which also works in conjunction with loaded modules
Checks if a virtual environment is activated
:return: True if virtual environment is running, else False
"""
stat = bool(os.environ.get("VIRTUAL_ENV"))
return stat
#
#--------------------------------------------------------------------------------------------------------
#
def check_virtualenv(labort=False):
def check_virtualenv(lactive: bool= True, venv_path: str = "",labort=False):
'''
Checks if current script is running a virtual environment and returns the directory's name
:param labort: If True, the an Exception is raised. If False, only a Warning is given
:param lactive: If True, virtual environment must be activated. If False, the existence is required only.
:param labort: If True, an Exception is raised. If False, only a Warning is given
:return: name of virtual environment
'''
method = check_virtualenv.__name__
if lactive:
lvirt = in_virtualenv()
err_mess = "%{0}: No virtual environment is running.".format(method)
venv_path = os.environ.get("VIRTUAL_ENV")
else:
lvirt = os.path.isfile(os.path.join(venv_path, "bin", "activate"))
err_mess = "%{0}: Virtual environment is not existing under '{1}'".format(method, venv_path)
if not lvirt:
if labort:
raise EnvironmentError("config_train.py has to run in an activated virtual environment!")
raise EnvironmentError(err_mess)
else:
raise Warning("config_train.py is not running in an activated virtual environment!")
raise Warning(err_mess)
return
else:
return os.path.basename(sys.prefix)
return os.path.basename(venv_path)
#
# --------------------------------------------------------------------------------------------------------
#
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment