From df896ef3816d8839c8e0f58419e936db3f10e09a Mon Sep 17 00:00:00 2001 From: Michael <m.langguth@fz-juelich.de> Date: Thu, 27 Jan 2022 13:03:29 +0100 Subject: [PATCH] Fixes to runscript-generator to work without active virtual environment (has to exist only). --- .../env_setup/generate_runscript.py | 18 +++++++--- .../env_setup/modules_preprocess.sh | 1 - .../runscript_generator/configurations.py | 34 ++++++++++++++----- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/video_prediction_tools/env_setup/generate_runscript.py b/video_prediction_tools/env_setup/generate_runscript.py index 6bc4f467..4b8eec7a 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 6bc4f4d0..c8675547 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 669d5c5f..a62be3ff 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 -- GitLab