Commit 9fd9bc58ff2b0eb7568a7e3e0240dd0202b17358
1 parent
9f044abf81
Exists in
master
First proposition of an experiment script that allow users to manage their experiments
Showing 1 changed file with 144 additions and 0 deletions Side-by-side Diff
volia/experiment.py
1 | +import argparse | |
2 | +import os | |
3 | +from utils import SubCommandRunner | |
4 | +#from decouple import Config, RepositoryEnv | |
5 | +import decouple | |
6 | +import json | |
7 | + | |
8 | + | |
9 | +# TODO: | |
10 | +# Load automatically if exists the persistency file | |
11 | +# Create a class that is a sigleton with configuration variables | |
12 | + | |
13 | +VOLIA_ENV_VAR="VOLIA_EXPERIMENT" | |
14 | + | |
15 | + | |
16 | +def init(): | |
17 | + """ | |
18 | + Initialize volia | |
19 | + """ | |
20 | + | |
21 | + directory = VOLIA_HOME | |
22 | + | |
23 | + if not os.path.isdir(directory): | |
24 | + os.mkdir(directory) | |
25 | + | |
26 | + #config = decouple.AutoConfig(search_path=directory) | |
27 | + #config = Config(RepositoryEnv(directory)) | |
28 | + | |
29 | + persistency_file = os.path.join(directory, "persistency.json") | |
30 | + env_file = os.path.join(directory, ".env") | |
31 | + | |
32 | + with open(env_file, "w") as f: | |
33 | + f.write(f"VOLIA_PERSISTENCY=\"{persistency_file}\"") | |
34 | + #exit(1) | |
35 | + basic_persistency = { | |
36 | + "current_experiment": None | |
37 | + } | |
38 | + | |
39 | + with open(persistency_file, "w") as f: | |
40 | + json.dump(basic_persistency, f, indent=4) | |
41 | + | |
42 | + | |
43 | + | |
44 | +def create(): | |
45 | + pass | |
46 | + | |
47 | +def show(): | |
48 | + print("Volia env var : " + VOLIA_ENV_VAR) | |
49 | + print("Value : " + str(os.getenv(VOLIA_ENV_VAR))) | |
50 | + config = decouple.AutoConfig(search_path="") | |
51 | + pass | |
52 | + | |
53 | +def activate(): | |
54 | + pass | |
55 | + | |
56 | +def deactivate(): | |
57 | + #pycrosskit.envariables.SysEnv.set_var(VOLIA_ENV_VAR, "UNE VARIABLE") | |
58 | + print("VARIABLE: " + pycrosskit.envariables.SysEnv.get_var(VOLIA_ENV_VAR, delete=True)) | |
59 | + print("Desactivation") | |
60 | + print("New var : " + os.getenv(VOLIA_ENV_VAR)) | |
61 | + pass | |
62 | + | |
63 | +def remove(): | |
64 | + pass | |
65 | + | |
66 | + | |
67 | +if __name__ == '__main__': | |
68 | + | |
69 | + # Load VOLIA_HOME env var | |
70 | + VOLIA_HOME = os.getenv("VOLIA_HOME") | |
71 | + | |
72 | + if VOLIA_HOME is None: | |
73 | + 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.") | |
74 | + | |
75 | + # Load environment file if exists | |
76 | + # TODO: Pas besoin de INIT finalement. | |
77 | + CONFIG = None | |
78 | + PERSISTENCY_DATA = None | |
79 | + env_file = os.path.join(VOLIA_HOME, ".env") | |
80 | + if os.path.isfile(env_file): | |
81 | + # Load le fichier | |
82 | + CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) | |
83 | + # Then loading the persistency file | |
84 | + persistency_file = os.path.join(VOLIA_HOME, "persistency.json") | |
85 | + | |
86 | + with open(persistency_file, "r") as f: | |
87 | + PERSISTENCY_DATA = json.load(f) | |
88 | + | |
89 | + else: | |
90 | + # Volia not initialized. | |
91 | + print("VOLIA is not initialized. Initializing.") | |
92 | + init() | |
93 | + pass | |
94 | + | |
95 | + print("Config: ", CONFIG) | |
96 | + print("PERSISTENCY_DATA: ", PERSISTENCY_DATA) | |
97 | + | |
98 | + | |
99 | + parser = argparse.ArgumentParser(description="...") | |
100 | + subparsers = parser.add_subparsers(title="actions") | |
101 | + | |
102 | + # init : initialize the volia environment tool | |
103 | + parser_init = subparsers.add_parser("init", help="init volia environment") | |
104 | + parser_init.set_defaults(which="init") | |
105 | + | |
106 | + # create a new env | |
107 | + parser_create = subparsers.add_parser("create", help="create an experimental environment") | |
108 | + parser_create.set_defaults(which="create") | |
109 | + | |
110 | + # activate env | |
111 | + parser_activate = subparsers.add_parser("activate", help="activate an experimental environment") | |
112 | + parser_activate.set_defaults(which="activate") | |
113 | + ''' | |
114 | + Pour activater, il suffit d'ajouter la variable d'environnement. | |
115 | + ''' | |
116 | + # deactivate env | |
117 | + parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment") | |
118 | + parser_deactivate.set_defaults(which="deactivate") | |
119 | + ''' | |
120 | + Pour désactiver il suffit d'enlever la variable d'environnement. | |
121 | + ''' | |
122 | + | |
123 | + # remove env | |
124 | + parser_remove = subparsers.add_parser("remove", help="remove an experimental environment") | |
125 | + parser_remove.set_defaults(which="remove") | |
126 | + | |
127 | + # show env | |
128 | + parser_show = subparsers.add_parser("show", help="show the list of experimental environment") | |
129 | + parser_show.set_defaults(which="show") | |
130 | + | |
131 | + # Parse | |
132 | + args = parser.parse_args() | |
133 | + | |
134 | + # Run commands | |
135 | + runner = SubCommandRunner({ | |
136 | + "create": create, | |
137 | + "activate": activate, | |
138 | + "deactivate": deactivate, | |
139 | + "remove": remove, | |
140 | + "show" : show, | |
141 | + "init" : init | |
142 | + }) | |
143 | + | |
144 | + runner.run(args.which, args.__dict__, remove="which") |