Blame view

volia/experiment.py 4.02 KB
9fd9bc58f   Quillot Mathias   First proposition...
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
  import argparse
  import os
  from utils import SubCommandRunner
  #from decouple import Config, RepositoryEnv
  import decouple
  import json
  
  
  # TODO:
  # Load automatically if exists the persistency file
  # Create a class that is a sigleton with configuration variables
  
  VOLIA_ENV_VAR="VOLIA_EXPERIMENT"
  
  
  def init():
      """
      Initialize volia
      """
  
      directory = VOLIA_HOME
  
      if not os.path.isdir(directory):
          os.mkdir(directory)
  
      #config = decouple.AutoConfig(search_path=directory)
      #config = Config(RepositoryEnv(directory))
      
      persistency_file = os.path.join(directory, "persistency.json")
      env_file = os.path.join(directory, ".env")
  
      with open(env_file, "w") as f:
          f.write(f"VOLIA_PERSISTENCY=\"{persistency_file}\"")
      #exit(1)
      basic_persistency = {
          "current_experiment": None
      }
  
      with open(persistency_file, "w") as f:
          json.dump(basic_persistency, f, indent=4)
      
  
  
  def create():
      pass
  
  def show():
      print("Volia env var : " + VOLIA_ENV_VAR)
      print("Value : " + str(os.getenv(VOLIA_ENV_VAR)))
      config = decouple.AutoConfig(search_path="")
      pass
  
  def activate():
      pass
  
  def deactivate():
      #pycrosskit.envariables.SysEnv.set_var(VOLIA_ENV_VAR, "UNE VARIABLE")
      print("VARIABLE: " + pycrosskit.envariables.SysEnv.get_var(VOLIA_ENV_VAR, delete=True))
      print("Desactivation")
      print("New var : " + os.getenv(VOLIA_ENV_VAR))
      pass
  
  def remove():
      pass
  
  
  if __name__ == '__main__':
  
      # Load VOLIA_HOME env var
      VOLIA_HOME = os.getenv("VOLIA_HOME")
  
      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.")
  
      # Load environment file if exists
      # TODO: Pas besoin de INIT finalement.
      CONFIG = None
      PERSISTENCY_DATA = None
      env_file = os.path.join(VOLIA_HOME, ".env")
      if os.path.isfile(env_file):
          # Load le fichier
          CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME)
          # Then loading the persistency file
          persistency_file = os.path.join(VOLIA_HOME, "persistency.json")
  
          with open(persistency_file, "r") as f:
              PERSISTENCY_DATA = json.load(f)
  
      else:
          # Volia not initialized.
          print("VOLIA is not initialized. Initializing.")
          init()
          pass
      
      print("Config: ", CONFIG)
      print("PERSISTENCY_DATA: ", PERSISTENCY_DATA)
  
  
      parser = argparse.ArgumentParser(description="...")
      subparsers = parser.add_subparsers(title="actions")
  
      # init : initialize the volia environment tool
      parser_init = subparsers.add_parser("init", help="init volia environment")
      parser_init.set_defaults(which="init")
  
      # create a new env
      parser_create = subparsers.add_parser("create", help="create an experimental environment")
      parser_create.set_defaults(which="create")
  
      # activate env
      parser_activate = subparsers.add_parser("activate", help="activate an experimental environment")
      parser_activate.set_defaults(which="activate")
      '''
      Pour activater, il suffit d'ajouter la variable d'environnement.
      '''
      # deactivate env
      parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment")
      parser_deactivate.set_defaults(which="deactivate")
      '''
      Pour désactiver il suffit d'enlever la variable d'environnement.
      '''
  
      # remove env
      parser_remove = subparsers.add_parser("remove", help="remove an experimental 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")