forked from Azure/simdem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
155 lines (130 loc) · 5.34 KB
/
environment.py
File metadata and controls
155 lines (130 loc) · 5.34 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
155
# For managing the environment in which a SimDem demo executes.
import os
import sys
import json
import config
class Environment(object):
def __init__(self, directory, copy_env=True, is_test=False):
"""Initialize the environment"""
if copy_env:
self.env = os.environ.copy()
else:
self.env = {}
self.is_test = is_test
self.read_simdem_environment(directory)
self.set("SIMDEM_VERSION", config.SIMDEM_VERSION)
self.set("SIMDEM_CWD", directory)
self.set("SIMDEM_EXEC_DIR", os.getcwd())
temp_dir = os.path.expanduser(config.SIMDEM_TEMP_DIR)
self.set("SIMDEM_TEMP_DIR", temp_dir)
def read_simdem_environment(self, directory):
"""Populates each shell environment with a set of environment vars
loaded via env.json and/or env.local.json files. Variables are
loaded in order first from the parent of the current script
directory, then the current scriptdir itself and finally from
the directory in which the `simdem` command was executed (the
CWD).
Values are loaded in the following order, the last file to
define a vlaue is the one that "wins".
- PARENT_OF_SCRIPT_DIR/env.json
- SCRIPT_DIR/env.json
- PARENT_OF_SCRIPT_DIR/env.local.json
- SCRIPT_DIR/env.local.json
- CWD/env.json
- CWD/env.local.json
Note that it is possible to supply test values in an
`env.test.json` file stored in the SCRIPT_DIR, its parent or
the current working directory. If we are running in test mode
then the following three files will be loaded, if they exist,
in the following order at the end of the initialization
procedure. This means they will take precedence over
everything else.
- PARENT_OF_SCRIPT_DIR/env.test.json
- SCRIPT_DIR/env.test.json
- CWD/env.json
"""
env = {}
if not directory.endswith('/'):
directory = directory + "/"
filename = directory + "../env.json"
if os.path.isfile(directory + "../env.json"):
with open(filename) as env_file:
app_env = self.process_env(json.load(env_file))
env.update(app_env)
filename = directory + "env.json"
if os.path.isfile(filename):
with open(filename) as env_file:
script_env = self.process_env(json.load(env_file))
env.update(script_env)
filename = directory + "../env.local.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = self.process_env(json.load(env_file))
env.update(local_env)
filename = directory + "env.local.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = self.process_env(json.load(env_file))
env.update(local_env)
filename = os.getcwd() + "env.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = self.process_env(json.load(env_file))
env.update(local_env)
filename = os.getcwd() + "env.local.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = self.process_env(json.load(env_file))
env.update(local_env)
if self.is_test:
filename = directory + "../env.test.json"
if os.path.isfile(filename):
with open(filename) as env_file:
script_env = self.process_env(json.load(env_file))
env.update(script_env)
filename = directory + "env.test.json"
if os.path.isfile(filename):
with open(filename) as env_file:
script_env = self.process_env(json.load(env_file))
env.update(script_env)
filename = os.getcwd() + "/env.test.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = self.process_env(json.load(env_file))
env.update(local_env)
self.env.update(env)
def process_env(self, new_env):
"""
Takes an environment definition and processes it for use.
For example, expand '~' to home directory.
"""
for key in new_env:
val = new_env[key]
if val.startswith('~'):
new_env[key] = os.path.expanduser(val)
return new_env
def set(self, var, value):
"""Sets a new variable to the environment"""
self.env[var] = value
def get(self, key=None):
"""Returns a either a value for a supplied key or, if key is None, a
dictionary containing the current environment"""
if key:
if key not in self.env:
return "UNDEFINED"
else:
return self.env[key]
else:
return self.env
def dump_env(self):
"""
Prints the environment to the console.
"""
for item in self.env.items():
print(str(item))
def __str__(self):
s = ""
for item in self.env.items():
s += str(item)
s += "\n"
return s