Blame view
volia/experiment.py
4.91 KB
9fd9bc58f First proposition... |
1 2 3 4 5 6 |
import argparse import os from utils import SubCommandRunner #from decouple import Config, RepositoryEnv import decouple import json |
d6dec28e5 No, experiment sh... |
7 |
import core.persistency |
9fd9bc58f First proposition... |
8 9 10 11 |
# TODO: # Load automatically if exists the persistency file # Create a class that is a sigleton with configuration variables |
d6dec28e5 No, experiment sh... |
12 13 |
# Search how to play `python -m volia.experiment deactivate` with the `` # automatically. |
9fd9bc58f First proposition... |
14 |
|
d6dec28e5 No, experiment sh... |
15 16 |
def init(shell: str): """ |
9fd9bc58f First proposition... |
17 |
""" |
d6dec28e5 No, experiment sh... |
18 19 20 21 22 23 24 25 26 27 |
# 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. |
9fd9bc58f First proposition... |
28 29 30 31 32 33 |
""" directory = VOLIA_HOME if not os.path.isdir(directory): os.mkdir(directory) |
9fd9bc58f First proposition... |
34 35 |
persistency_file = os.path.join(directory, "persistency.json") |
d6dec28e5 No, experiment sh... |
36 |
|
9fd9bc58f First proposition... |
37 38 39 40 41 42 43 |
basic_persistency = { "current_experiment": None } with open(persistency_file, "w") as f: json.dump(basic_persistency, f, indent=4) |
d6dec28e5 No, experiment sh... |
44 45 46 |
def create(name: str): print("Will allow the user to create an environment") # TODO: check if the environment already exists |
9fd9bc58f First proposition... |
47 48 49 |
pass def show(): |
d6dec28e5 No, experiment sh... |
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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.") |
9fd9bc58f First proposition... |
68 69 |
def deactivate(): |
d6dec28e5 No, experiment sh... |
70 71 72 73 74 75 76 77 78 |
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.") |
9fd9bc58f First proposition... |
79 |
|
d6dec28e5 No, experiment sh... |
80 81 82 83 |
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. |
9fd9bc58f First proposition... |
84 85 86 87 88 89 |
pass if __name__ == '__main__': # Load VOLIA_HOME env var |
d6dec28e5 No, experiment sh... |
90 |
# TODO: encapsulate it into an environment class. |
9fd9bc58f First proposition... |
91 |
VOLIA_HOME = os.getenv("VOLIA_HOME") |
d6dec28e5 No, experiment sh... |
92 93 |
VOLIA_SHELL = os.getenv("VOLIA_SHELL") VOLIA_EXPERIMENT = os.getenv("VOLIA_EXPERIMENT") |
9fd9bc58f First proposition... |
94 95 96 97 |
if VOLIA_HOME is None: raise Exception("VOLIA_HOME is not defined. Please, define where you want to initialize VOLIA_HOME. We suggest to set this variable to following value: /home/[your name]/.volia.") |
d6dec28e5 No, experiment sh... |
98 99 100 |
# Check if persistency file exists, or init persistency_file = os.path.join(VOLIA_HOME, "persistency.json") if not os.path.isfile(persistency_file): |
9fd9bc58f First proposition... |
101 |
print("VOLIA is not initialized. Initializing.") |
d6dec28e5 No, experiment sh... |
102 |
initialize_files() |
9fd9bc58f First proposition... |
103 |
|
d6dec28e5 No, experiment sh... |
104 105 |
# Load persistency data PERSISTENCY_DATA = core.persistency.PersistencyData(persistency_file) |
9fd9bc58f First proposition... |
106 |
|
5fcaeae1e New way to set en... |
107 |
# PARSER |
9fd9bc58f First proposition... |
108 109 |
parser = argparse.ArgumentParser(description="...") subparsers = parser.add_subparsers(title="actions") |
d6dec28e5 No, experiment sh... |
110 |
# init : initialize the volia environment tool with shell script |
9fd9bc58f First proposition... |
111 |
parser_init = subparsers.add_parser("init", help="init volia environment") |
d6dec28e5 No, experiment sh... |
112 |
parser_init.add_argument("--shell", type=str, default="bash", help="Help") |
9fd9bc58f First proposition... |
113 114 115 116 |
parser_init.set_defaults(which="init") # create a new env parser_create = subparsers.add_parser("create", help="create an experimental environment") |
d6dec28e5 No, experiment sh... |
117 |
parser_create.add_argument("name", type=str, help="name of the created environment") |
9fd9bc58f First proposition... |
118 119 120 121 |
parser_create.set_defaults(which="create") # activate env parser_activate = subparsers.add_parser("activate", help="activate an experimental environment") |
d6dec28e5 No, experiment sh... |
122 |
parser_activate.add_argument("name", type=str, help="environment name") |
9fd9bc58f First proposition... |
123 |
parser_activate.set_defaults(which="activate") |
d6dec28e5 No, experiment sh... |
124 |
|
9fd9bc58f First proposition... |
125 126 127 |
# deactivate env parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment") parser_deactivate.set_defaults(which="deactivate") |
9fd9bc58f First proposition... |
128 129 130 |
# remove env parser_remove = subparsers.add_parser("remove", help="remove an experimental environment") |
d6dec28e5 No, experiment sh... |
131 |
parser_remove.add_argument("name", type=str, help="name of the removed environment") |
9fd9bc58f First proposition... |
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
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") |