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 Side-by-side Diff
volia/experiment.py
... | ... | @@ -4,34 +4,36 @@ |
4 | 4 | #from decouple import Config, RepositoryEnv |
5 | 5 | import decouple |
6 | 6 | import json |
7 | -import core.env | |
7 | +import core.persistency | |
8 | 8 | |
9 | 9 | # TODO: |
10 | 10 | # Load automatically if exists the persistency file |
11 | 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 | - | |
16 | -def init(): | |
25 | +def initialize_files(): | |
17 | 26 | """ |
18 | - Initialize volia | |
27 | + Initialize volia files : load environment file and persistency data. | |
19 | 28 | """ |
20 | 29 | |
21 | 30 | directory = VOLIA_HOME |
22 | 31 | |
23 | 32 | if not os.path.isdir(directory): |
24 | 33 | os.mkdir(directory) |
25 | - | |
26 | - #config = decouple.AutoConfig(search_path=directory) | |
27 | - #config = Config(RepositoryEnv(directory)) | |
28 | 34 | |
29 | 35 | 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) | |
36 | + | |
35 | 37 | basic_persistency = { |
36 | 38 | "current_experiment": None |
37 | 39 | } |
38 | 40 | |
39 | 41 | |
40 | 42 | |
41 | 43 | |
42 | 44 | |
43 | 45 | |
44 | 46 | |
45 | 47 | |
46 | 48 | |
47 | 49 | |
48 | 50 | |
49 | 51 | |
50 | 52 | |
51 | 53 | |
52 | 54 | |
53 | 55 | |
... | ... | @@ -41,80 +43,95 @@ |
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 | 49 | pass |
46 | 50 | |
47 | 51 | 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 | + print("VOLIA_HOME=", VOLIA_HOME) | |
53 | + print("VOLIA_SHELL=", VOLIA_SHELL) | |
54 | + print("VOLIA EXPERIMENT=", VOLIA_EXPERIMENT) | |
55 | + print("Next feature: list the available environments") | |
52 | 56 | |
53 | -def activate(): | |
54 | - pass | |
57 | +def activate(name: str): | |
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 | 71 | 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 | |
72 | + if VOLIA_SHELL is None: | |
73 | + raise Exception("You need to define VOLIA_SHELL environment variable") | |
74 | + | |
75 | + if VOLIA_SHELL == "bash": | |
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 | 86 | pass |
65 | 87 | |
66 | 88 | |
67 | 89 | if __name__ == '__main__': |
68 | 90 | |
69 | 91 | # Load VOLIA_HOME env var |
92 | + # TODO: encapsulate it into an environment class. | |
70 | 93 | VOLIA_HOME = os.getenv("VOLIA_HOME") |
94 | + VOLIA_SHELL = os.getenv("VOLIA_SHELL") | |
95 | + VOLIA_EXPERIMENT = os.getenv("VOLIA_EXPERIMENT") | |
71 | 96 | |
72 | 97 | if VOLIA_HOME is None: |
73 | 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 | |
76 | - # TODO: Pas besoin de INIT finalement. | |
77 | - CONFIG = None | |
78 | - PERSISTENCY_DATA = None | |
79 | - env_file = os.path.join(VOLIA_HOME, ".env") | |
80 | - | |
81 | - if not os.path.isfile(env_file): | |
100 | + # Check if persistency file exists, or init | |
101 | + persistency_file = os.path.join(VOLIA_HOME, "persistency.json") | |
102 | + if not os.path.isfile(persistency_file): | |
82 | 103 | print("VOLIA is not initialized. Initializing.") |
83 | - init() | |
104 | + initialize_files() | |
84 | 105 | |
85 | - # Load le fichier | |
86 | - CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) | |
87 | - # Then loading the persistency file | |
88 | - persistency_file = os.path.join(VOLIA_HOME, "persistency.json") | |
89 | - PERSISTENCY_DATA = core.env.Env(persistency_file) | |
106 | + # Load persistency data | |
107 | + PERSISTENCY_DATA = core.persistency.PersistencyData(persistency_file) | |
90 | 108 | |
91 | 109 | # PARSER |
92 | 110 | parser = argparse.ArgumentParser(description="...") |
93 | 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 | 114 | parser_init = subparsers.add_parser("init", help="init volia environment") |
115 | + parser_init.add_argument("--shell", type=str, default="bash", help="Help") | |
97 | 116 | parser_init.set_defaults(which="init") |
98 | 117 | |
99 | 118 | # create a new env |
100 | 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 | 121 | parser_create.set_defaults(which="create") |
102 | 122 | |
103 | 123 | # activate env |
104 | 124 | parser_activate = subparsers.add_parser("activate", help="activate an experimental environment") |
125 | + parser_activate.add_argument("name", type=str, help="environment name") | |
105 | 126 | parser_activate.set_defaults(which="activate") |
106 | - ''' | |
107 | - Pour activater, il suffit d'ajouter la variable d'environnement. | |
108 | - ''' | |
127 | + | |
109 | 128 | # deactivate env |
110 | 129 | parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment") |
111 | 130 | parser_deactivate.set_defaults(which="deactivate") |
112 | - ''' | |
113 | - Pour désactiver il suffit d'enlever la variable d'environnement. | |
114 | - ''' | |
115 | 131 | |
116 | 132 | # remove env |
117 | 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 | 135 | parser_remove.set_defaults(which="remove") |
119 | 136 | |
120 | 137 | # show env |