experiment.py
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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")