Commit d6dec28e54afc8d950a9a266e4167ca89d62d588
1 parent
96c04cbe49
Exists in
master
No, experiment show bash scripts to execute to follow the activation and change env vars.
Showing 1 changed file with 65 additions and 48 deletions Inline Diff
volia/experiment.py
1 | import argparse | 1 | import argparse |
2 | import os | 2 | import os |
3 | from utils import SubCommandRunner | 3 | from utils import SubCommandRunner |
4 | #from decouple import Config, RepositoryEnv | 4 | #from decouple import Config, RepositoryEnv |
5 | import decouple | 5 | import decouple |
6 | import json | 6 | import json |
7 | import core.env | 7 | import core.persistency |
8 | 8 | ||
9 | # TODO: | 9 | # TODO: |
10 | # Load automatically if exists the persistency file | 10 | # Load automatically if exists the persistency file |
11 | # Create a class that is a sigleton with configuration variables | 11 | # Create a class that is a sigleton with configuration variables |
12 | # Search how to play `python -m volia.experiment deactivate` with the `` | ||
13 | # automatically. | ||
12 | 14 | ||
13 | VOLIA_ENV_VAR="VOLIA_EXPERIMENT" | 15 | def init(shell: str): |
16 | """ | ||
17 | """ | ||
18 | # TODO: add first scripts | ||
19 | if shell == "bash": | ||
20 | print("export VOLIA_EXPERIMENT=\"\"") | ||
21 | print("export VOLIA_BASH=\"bash\"") | ||
22 | else: | ||
23 | print("NO SHELL CHOOSE") | ||
14 | 24 | ||
15 | 25 | def initialize_files(): | |
16 | def init(): | ||
17 | """ | 26 | """ |
18 | Initialize volia | 27 | Initialize volia files : load environment file and persistency data. |
19 | """ | 28 | """ |
20 | 29 | ||
21 | directory = VOLIA_HOME | 30 | directory = VOLIA_HOME |
22 | 31 | ||
23 | if not os.path.isdir(directory): | 32 | if not os.path.isdir(directory): |
24 | os.mkdir(directory) | 33 | os.mkdir(directory) |
25 | |||
26 | #config = decouple.AutoConfig(search_path=directory) | ||
27 | #config = Config(RepositoryEnv(directory)) | ||
28 | 34 | ||
29 | persistency_file = os.path.join(directory, "persistency.json") | 35 | persistency_file = os.path.join(directory, "persistency.json") |
30 | env_file = os.path.join(directory, ".env") | 36 | |
31 | |||
32 | with open(env_file, "w") as f: | ||
33 | f.write(f"VOLIA_PERSISTENCY=\"{persistency_file}\"") | ||
34 | #exit(1) | ||
35 | basic_persistency = { | 37 | basic_persistency = { |
36 | "current_experiment": None | 38 | "current_experiment": None |
37 | } | 39 | } |
38 | 40 | ||
39 | with open(persistency_file, "w") as f: | 41 | with open(persistency_file, "w") as f: |
40 | json.dump(basic_persistency, f, indent=4) | 42 | json.dump(basic_persistency, f, indent=4) |
41 | 43 | ||
42 | 44 | ||
43 | 45 | ||
44 | def create(): | 46 | def create(name: str): |
47 | print("Will allow the user to create an environment") | ||
48 | # TODO: check if the environment already exists | ||
45 | pass | 49 | pass |
46 | 50 | ||
47 | def show(): | 51 | def show(): |
48 | print("Volia env var : " + VOLIA_ENV_VAR) | 52 | print("VOLIA_HOME=", VOLIA_HOME) |
49 | print("Value : " + str(os.getenv(VOLIA_ENV_VAR))) | 53 | print("VOLIA_SHELL=", VOLIA_SHELL) |
50 | config = decouple.AutoConfig(search_path="") | 54 | print("VOLIA EXPERIMENT=", VOLIA_EXPERIMENT) |
51 | pass | 55 | print("Next feature: list the available environments") |
52 | 56 | ||
53 | def activate(): | 57 | def activate(name: str): |
54 | pass | 58 | # TODO: have a class hierarchy to manage all the possible |
59 | # shells. An abstract mother class will define all the needed methods. | ||
60 | # Or maybe an interface instead of abstract class. | ||
61 | if VOLIA_SHELL is None: | ||
62 | raise Exception("You need to define VOLIA_SHELL environment variable") | ||
63 | |||
64 | if VOLIA_SHELL == "bash": | ||
65 | print(f"export VOLIA_EXPERIMENT={name}") | ||
66 | |||
67 | else: | ||
68 | raise Exception(f"Do not know {VOLIA_SHELL} shell.") | ||
69 | |||
55 | 70 | ||
56 | def deactivate(): | 71 | def deactivate(): |
57 | #pycrosskit.envariables.SysEnv.set_var(VOLIA_ENV_VAR, "UNE VARIABLE") | 72 | if VOLIA_SHELL is None: |
58 | print("VARIABLE: " + pycrosskit.envariables.SysEnv.get_var(VOLIA_ENV_VAR, delete=True)) | 73 | raise Exception("You need to define VOLIA_SHELL environment variable") |
59 | print("Desactivation") | 74 | |
60 | print("New var : " + os.getenv(VOLIA_ENV_VAR)) | 75 | if VOLIA_SHELL == "bash": |
61 | pass | 76 | print("export VOLIA_EXPERIMENT=") |
62 | 77 | ||
63 | def remove(): | 78 | else: |
79 | raise Exception(f"Do not know {VOLIA_SHELL} shell.") | ||
80 | |||
81 | |||
82 | def remove(name: str): | ||
83 | print("Under construction: Will soon allow you to remove an experimental environment.") | ||
84 | # TODO: check if the name exists | ||
85 | # Remove the experimental environment if the name exists. | ||
64 | pass | 86 | pass |
65 | 87 | ||
66 | 88 | ||
67 | if __name__ == '__main__': | 89 | if __name__ == '__main__': |
68 | 90 | ||
69 | # Load VOLIA_HOME env var | 91 | # Load VOLIA_HOME env var |
92 | # TODO: encapsulate it into an environment class. | ||
70 | VOLIA_HOME = os.getenv("VOLIA_HOME") | 93 | VOLIA_HOME = os.getenv("VOLIA_HOME") |
94 | VOLIA_SHELL = os.getenv("VOLIA_SHELL") | ||
95 | VOLIA_EXPERIMENT = os.getenv("VOLIA_EXPERIMENT") | ||
71 | 96 | ||
72 | if VOLIA_HOME is None: | 97 | 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.") | 98 | 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 | 99 | ||
75 | # Load environment file if exists | 100 | # Check if persistency file exists, or init |
76 | # TODO: Pas besoin de INIT finalement. | 101 | persistency_file = os.path.join(VOLIA_HOME, "persistency.json") |
77 | CONFIG = None | 102 | if not os.path.isfile(persistency_file): |
78 | PERSISTENCY_DATA = None | ||
79 | env_file = os.path.join(VOLIA_HOME, ".env") | ||
80 | |||
81 | if not os.path.isfile(env_file): | ||
82 | print("VOLIA is not initialized. Initializing.") | 103 | print("VOLIA is not initialized. Initializing.") |
83 | init() | 104 | initialize_files() |
84 | 105 | ||
85 | # Load le fichier | 106 | # Load persistency data |
86 | CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) | 107 | PERSISTENCY_DATA = core.persistency.PersistencyData(persistency_file) |
87 | # Then loading the persistency file | ||
88 | persistency_file = os.path.join(VOLIA_HOME, "persistency.json") | ||
89 | PERSISTENCY_DATA = core.env.Env(persistency_file) | ||
90 | 108 | ||
91 | # PARSER | 109 | # PARSER |
92 | parser = argparse.ArgumentParser(description="...") | 110 | parser = argparse.ArgumentParser(description="...") |
93 | subparsers = parser.add_subparsers(title="actions") | 111 | subparsers = parser.add_subparsers(title="actions") |
94 | 112 | ||
95 | # init : initialize the volia environment tool | 113 | # init : initialize the volia environment tool with shell script |
96 | parser_init = subparsers.add_parser("init", help="init volia environment") | 114 | parser_init = subparsers.add_parser("init", help="init volia environment") |
115 | parser_init.add_argument("--shell", type=str, default="bash", help="Help") | ||
97 | parser_init.set_defaults(which="init") | 116 | parser_init.set_defaults(which="init") |
98 | 117 | ||
99 | # create a new env | 118 | # create a new env |
100 | parser_create = subparsers.add_parser("create", help="create an experimental environment") | 119 | parser_create = subparsers.add_parser("create", help="create an experimental environment") |
120 | parser_create.add_argument("name", type=str, help="name of the created environment") | ||
101 | parser_create.set_defaults(which="create") | 121 | parser_create.set_defaults(which="create") |
102 | 122 | ||
103 | # activate env | 123 | # activate env |
104 | parser_activate = subparsers.add_parser("activate", help="activate an experimental environment") | 124 | parser_activate = subparsers.add_parser("activate", help="activate an experimental environment") |
125 | parser_activate.add_argument("name", type=str, help="environment name") | ||
105 | parser_activate.set_defaults(which="activate") | 126 | parser_activate.set_defaults(which="activate") |
106 | ''' | 127 | |
107 | Pour activater, il suffit d'ajouter la variable d'environnement. | ||
108 | ''' | ||
109 | # deactivate env | 128 | # deactivate env |
110 | parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment") | 129 | parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment") |
111 | parser_deactivate.set_defaults(which="deactivate") | 130 | parser_deactivate.set_defaults(which="deactivate") |
112 | ''' | ||
113 | Pour désactiver il suffit d'enlever la variable d'environnement. | ||
114 | ''' | ||
115 | 131 | ||
116 | # remove env | 132 | # remove env |
117 | parser_remove = subparsers.add_parser("remove", help="remove an experimental environment") | 133 | parser_remove = subparsers.add_parser("remove", help="remove an experimental environment") |
134 | parser_remove.add_argument("name", type=str, help="name of the removed environment") | ||
118 | parser_remove.set_defaults(which="remove") | 135 | parser_remove.set_defaults(which="remove") |