experiment.py 4.91 KB
import argparse
import os
from utils import SubCommandRunner
#from decouple import Config, RepositoryEnv
import decouple
import json
import core.persistency

# TODO:
# Load automatically if exists the persistency file
# Create a class that is a sigleton with configuration variables
# Search how to play `python -m volia.experiment deactivate` with the ``
# automatically.

def init(shell: str):
    """
    """
    # TODO: add first scripts 
    if shell == "bash":
        print("export VOLIA_EXPERIMENT=\"\"")
        print("export VOLIA_BASH=\"bash\"")
    else:
        print("NO SHELL CHOOSE")

def initialize_files():
    """
    Initialize volia files : load environment file and persistency data.
    """

    directory = VOLIA_HOME

    if not os.path.isdir(directory):
        os.mkdir(directory)
    
    persistency_file = os.path.join(directory, "persistency.json")
    
    basic_persistency = {
        "current_experiment": None
    }

    with open(persistency_file, "w") as f:
        json.dump(basic_persistency, f, indent=4)
    


def create(name: str):
    print("Will allow the user to create an environment")
    # TODO: check if the environment already exists
    pass

def show():
    print("VOLIA_HOME=", VOLIA_HOME)
    print("VOLIA_SHELL=", VOLIA_SHELL)
    print("VOLIA EXPERIMENT=", VOLIA_EXPERIMENT)
    print("Next feature: list the available environments")

def activate(name: str):
    # TODO: have a class hierarchy to manage all the possible
    # shells. An abstract mother class will define all the needed methods.
    # Or maybe an interface instead of abstract class.
    if VOLIA_SHELL is None:
        raise Exception("You need to define VOLIA_SHELL environment variable")
    
    if VOLIA_SHELL == "bash":
        print(f"export VOLIA_EXPERIMENT={name}")
    
    else:
        raise Exception(f"Do not know {VOLIA_SHELL} shell.")
    

def deactivate():
    if VOLIA_SHELL is None:
        raise Exception("You need to define VOLIA_SHELL environment variable")
    
    if VOLIA_SHELL == "bash":
        print("export VOLIA_EXPERIMENT=")

    else:
        raise Exception(f"Do not know {VOLIA_SHELL} shell.")
    

def remove(name: str):
    print("Under construction: Will soon allow you to remove an experimental environment.")
    # TODO: check if the name exists
    # Remove the experimental environment if the name exists.
    pass


if __name__ == '__main__':

    # Load VOLIA_HOME env var
    # TODO: encapsulate it into an environment class.
    VOLIA_HOME = os.getenv("VOLIA_HOME")
    VOLIA_SHELL = os.getenv("VOLIA_SHELL")
    VOLIA_EXPERIMENT = os.getenv("VOLIA_EXPERIMENT")

    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.")

    # Check if persistency file exists, or init
    persistency_file = os.path.join(VOLIA_HOME, "persistency.json")
    if not os.path.isfile(persistency_file):
        print("VOLIA is not initialized. Initializing.")
        initialize_files()
    
    # Load persistency data
    PERSISTENCY_DATA = core.persistency.PersistencyData(persistency_file)

    # PARSER
    parser = argparse.ArgumentParser(description="...")
    subparsers = parser.add_subparsers(title="actions")

    # init : initialize the volia environment tool with shell script
    parser_init = subparsers.add_parser("init", help="init volia environment")
    parser_init.add_argument("--shell", type=str, default="bash", help="Help")
    parser_init.set_defaults(which="init")

    # create a new env
    parser_create = subparsers.add_parser("create", help="create an experimental environment")
    parser_create.add_argument("name", type=str, help="name of the created environment")
    parser_create.set_defaults(which="create")

    # activate env
    parser_activate = subparsers.add_parser("activate", help="activate an experimental environment")
    parser_activate.add_argument("name", type=str, help="environment name")
    parser_activate.set_defaults(which="activate")
    
    # deactivate env
    parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment")
    parser_deactivate.set_defaults(which="deactivate")

    # remove env
    parser_remove = subparsers.add_parser("remove", help="remove an experimental environment")
    parser_remove.add_argument("name", type=str, help="name of the removed 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")