From 9fd9bc58ff2b0eb7568a7e3e0240dd0202b17358 Mon Sep 17 00:00:00 2001 From: Quillot Mathias Date: Wed, 5 May 2021 15:22:14 +0200 Subject: [PATCH] First proposition of an experiment script that allow users to manage their experiments --- volia/experiment.py | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 volia/experiment.py diff --git a/volia/experiment.py b/volia/experiment.py new file mode 100644 index 0000000..384d596 --- /dev/null +++ b/volia/experiment.py @@ -0,0 +1,144 @@ +import argparse +import os +from utils import SubCommandRunner +#from decouple import Config, RepositoryEnv +import decouple +import json + + +# TODO: +# Load automatically if exists the persistency file +# Create a class that is a sigleton with configuration variables + +VOLIA_ENV_VAR="VOLIA_EXPERIMENT" + + +def init(): + """ + Initialize volia + """ + + directory = VOLIA_HOME + + if not os.path.isdir(directory): + os.mkdir(directory) + + #config = decouple.AutoConfig(search_path=directory) + #config = Config(RepositoryEnv(directory)) + + persistency_file = os.path.join(directory, "persistency.json") + env_file = os.path.join(directory, ".env") + + with open(env_file, "w") as f: + f.write(f"VOLIA_PERSISTENCY=\"{persistency_file}\"") + #exit(1) + basic_persistency = { + "current_experiment": None + } + + with open(persistency_file, "w") as f: + json.dump(basic_persistency, f, indent=4) + + + +def create(): + pass + +def show(): + print("Volia env var : " + VOLIA_ENV_VAR) + print("Value : " + str(os.getenv(VOLIA_ENV_VAR))) + config = decouple.AutoConfig(search_path="") + pass + +def activate(): + pass + +def deactivate(): + #pycrosskit.envariables.SysEnv.set_var(VOLIA_ENV_VAR, "UNE VARIABLE") + print("VARIABLE: " + pycrosskit.envariables.SysEnv.get_var(VOLIA_ENV_VAR, delete=True)) + print("Desactivation") + print("New var : " + os.getenv(VOLIA_ENV_VAR)) + pass + +def remove(): + pass + + +if __name__ == '__main__': + + # Load VOLIA_HOME env var + VOLIA_HOME = os.getenv("VOLIA_HOME") + + if VOLIA_HOME is None: + raise Exception("VOLIA_HOME is not defined. Please, define where you want to initialize VOLIA_HOME.\nWe suggest to set this variable to following value: /home/[your name]/.volia.") + + # Load environment file if exists + # TODO: Pas besoin de INIT finalement. + CONFIG = None + PERSISTENCY_DATA = None + env_file = os.path.join(VOLIA_HOME, ".env") + if os.path.isfile(env_file): + # Load le fichier + CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) + # Then loading the persistency file + persistency_file = os.path.join(VOLIA_HOME, "persistency.json") + + with open(persistency_file, "r") as f: + PERSISTENCY_DATA = json.load(f) + + else: + # Volia not initialized. + print("VOLIA is not initialized. Initializing.") + init() + pass + + print("Config: ", CONFIG) + print("PERSISTENCY_DATA: ", PERSISTENCY_DATA) + + + parser = argparse.ArgumentParser(description="...") + subparsers = parser.add_subparsers(title="actions") + + # init : initialize the volia environment tool + parser_init = subparsers.add_parser("init", help="init volia environment") + parser_init.set_defaults(which="init") + + # create a new env + parser_create = subparsers.add_parser("create", help="create an experimental environment") + parser_create.set_defaults(which="create") + + # activate env + parser_activate = subparsers.add_parser("activate", help="activate an experimental environment") + parser_activate.set_defaults(which="activate") + ''' + Pour activater, il suffit d'ajouter la variable d'environnement. + ''' + # deactivate env + parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment") + parser_deactivate.set_defaults(which="deactivate") + ''' + Pour désactiver il suffit d'enlever la variable d'environnement. + ''' + + # remove env + parser_remove = subparsers.add_parser("remove", help="remove an experimental environment") + parser_remove.set_defaults(which="remove") + + # show env + parser_show = subparsers.add_parser("show", help="show the list of experimental environment") + parser_show.set_defaults(which="show") + + # Parse + args = parser.parse_args() + + # Run commands + runner = SubCommandRunner({ + "create": create, + "activate": activate, + "deactivate": deactivate, + "remove": remove, + "show" : show, + "init" : init + }) + + runner.run(args.which, args.__dict__, remove="which") -- 1.8.2.3