diff --git a/video_prediction_tools/env_setup/generate_runscript.py b/video_prediction_tools/env_setup/generate_runscript.py index 6bc4f4674facd5482858b7aa9c0a422334aa30fc..4b8eec7a7988b69a78d71ecd1d72ae57571ff8ad 100755 --- a/video_prediction_tools/env_setup/generate_runscript.py +++ b/video_prediction_tools/env_setup/generate_runscript.py @@ -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 diff --git a/video_prediction_tools/env_setup/modules_preprocess.sh b/video_prediction_tools/env_setup/modules_preprocess.sh index 6bc4f4d09c13d1bd73db2d38ff2c263e72a1f8a8..c867554716e49f9fbe5c66275a158fefd505f927 100755 --- a/video_prediction_tools/env_setup/modules_preprocess.sh +++ b/video_prediction_tools/env_setup/modules_preprocess.sh @@ -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 diff --git a/video_prediction_tools/utils/runscript_generator/configurations.py b/video_prediction_tools/utils/runscript_generator/configurations.py index 669d5c5f0f67ec83f9cb4ae7cbb13add0fd5d718..a62be3ff16780e481282910719215b7a25e7cf4b 100644 --- a/video_prediction_tools/utils/runscript_generator/configurations.py +++ b/video_prediction_tools/utils/runscript_generator/configurations.py @@ -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 ''' - lvirt = in_virtualenv() + 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) # # -------------------------------------------------------------------------------------------------------- # @@ -111,4 +129,4 @@ def keyboard_interaction(console_str,check_input,err,ntries=1): else: raise err - return input_req \ No newline at end of file + return input_req