misc.system_tools.environ_var

Descriptive description.

  1"""Descriptive description."""
  2
  3import os
  4import re
  5import sys
  6import multiprocessing.context as ctx
  7import platform
  8
  9
 10class OpenBlasSingleThread:
 11    """
 12    A context manager class to set OpenBLAS multi threading environment variable to 1 (i.e., single threaded). The
 13    class is used in a 'with'-statement to ensure that everything inside the statement is run single threaded,
 14    and outside the statement is run using whatever the environment variable was set before the 'with'-statement.
 15    The environment variable setting threading in OpenBLAS is OMP_NUM_THREADS.
 16
 17    Example
 18    -------
 19
 20    >>> from system_tools.environ_var import OpenBlasSingleThread
 21    ... import ctypes
 22    ... import multiprocessing as mp
 23    ...
 24    ... def justdoit():
 25    ...     # Load OpenBLAS library and print number of threads
 26    ...     openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
 27    ...     print(openblas_lib.openblas_get_num_threads())
 28    ...
 29    ... if __name__ == "__main__":
 30    ...     # Load OpenBLAS library and print number of threads before the with-statement
 31    ...     openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
 32    ...     print(openblas_lib.openblas_get_num_threads())
 33    ...
 34    ...     # Run a Process inside the with-statement with the OpenBlasSingleThread class.
 35    ...     with OpenBlasSingleThread ():
 36    ...         p = mp.Process (target=justdoit)
 37    ...         p.start ()
 38    ...         p.join ()
 39    ...
 40    ... # Load OpenBLAS library and print number of threads before the with-statement
 41    ... openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
 42    ... print(openblas_lib.openblas_get_num_threads())
 43    """
 44
 45    def __init__(self):
 46        """
 47        Init. the class with no inputs. Use this to initialize internal variables for storing number of threads an
 48        the Process context manager before the change to single thread.
 49
 50        Parameters
 51        ----------
 52        num_threads:
 53            String with number of OpenBLAS threads before change to single
 54            threaded (it is the content of OMP_NUM_THREADS)
 55        ctx:
 56            The context variable from Process (default is 'fork' context, but
 57            we want to use 'spawn')
 58
 59        Changelog
 60        ---------
 61        - ST 31/10-17
 62        """
 63        self.num_threads = ''
 64        self.ctx = None
 65
 66    def __enter__(self):
 67        """
 68        Method that is run when class is initiated by a 'with'-statement
 69
 70        Changelog
 71        ---------
 72        - ST 31/10-17
 73        """
 74        # Save OMP_NUM_THREADS environment variable to restore later
 75        if 'OMP_NUM_THREADS' in os.environ:
 76            self.num_threads = os.environ['OMP_NUM_THREADS']
 77        else:
 78            self.num_threads = ''
 79
 80        # Set OMP_NUM_THREADS to 1 to ensure single threaded OpenBLAS
 81        os.environ['OMP_NUM_THREADS'] = '1'
 82
 83        # Save Process context variable to restore later
 84        self.ctx = ctx._default_context
 85
 86        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
 87        # with the newly set OMP_NUM_THREADS=1 environment variable. (The default, 'fork', context only copy its
 88        # parent environment variables without taking into account changes made after a Python program is
 89        # initialized)
 90        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
 91
 92        # Return self
 93        return self
 94
 95    def __exit__(self, exc_typ, exc_val, exc_trb):
 96        """
 97        Method that is run when 'with'-statement closes. Here, we reset OMP_NUM_THREADS and Process context to what
 98        is was set to before the 'with'-statement. Input here in this method are required to work in 'with'-statement.
 99
100        Changelog
101        ---------
102        - ST 31/10-17
103        """
104        # Check if there was any OMP_NUM_THREADS at all before the with-statement, and reset it thereafter.
105        if len(self.num_threads):
106            os.environ['OMP_NUM_THREADS'] = self.num_threads
107        else:
108            os.environ.unsetenv('OMP_NUM_THREADS')
109
110        # Reset Process context
111        ctx._default_context = self.ctx
112
113        # Return False (exit 0?)
114        return False
115
116
117class CmgRunEnvironment:
118    """
119    A context manager class to run CMG simulators with correct environmental variables.
120    """
121
122    def __init__(self, root, simulator, version, license):
123        """
124        We initialize the context manager by setting up correct paths and environment variable names that we set in
125        __enter__.
126
127        Parameters
128        ----------
129        root : str
130            Root folder where CMG simulator(s) are installed.
131
132        simulator : str
133            Simulator name.
134
135        version : str
136            Version of the simulator.
137
138        license : str
139            License server name.
140
141        Changelog
142        ---------
143        - ST 25/10-18
144
145        Notes
146        -----
147        'version' is the release version of CMG, e.g., 2017.101.G.
148        """
149        # In
150        self.root = root
151        self.sim = simulator
152        self.ver = version
153        self.lic = license
154
155        # Internal
156        self.ctx = None
157        self.path = ''
158        self.ld_path = ''
159
160        # Check system platform.
161        # TODO: Figure out paths in other systems (read Windows...) and remove assertion.
162        assert (platform.system() == 'Linux'), \
163            'Sorry, we have only set up paths for Linux systems... But hey, maybe you can implemented it for your ' \
164            'system? :)'
165
166        # Base path to simulator folders
167        self.path_base = self.root + self.ver + os.sep + self.sim + os.sep + self.ver[:-3] + os.sep + \
168            'linux_x64' + os.sep
169
170        # Path to exe file
171        self.path_exe = self.path_base + 'exe'
172
173        # Path to libraries
174        self.path_lib = self.path_base + 'lib'
175
176    def __enter__(self):
177        """
178        Method that is run when class is initiated by a 'with'-statement
179
180        Changelog
181        ---------
182        - ST 25/10-18
183        """
184        # Append if environment variable already exist, or generate it if not.
185        # We also save all environment variables that we intend to alter, so we can restore them when closing the
186        # context manager class
187        # PATH
188        if 'PATH' in os.environ:
189            self.path = os.environ['PATH']
190            os.environ['PATH'] = self.path_exe + os.pathsep + os.environ['PATH']
191        else:
192            self.path = ''
193            os.environ['PATH'] = self.path_exe
194
195        # LD_LIBRARY_PATH
196        if 'LD_LIBRARY_PATH' in os.environ:
197            self.ld_path = os.environ['LD_LIBRARY_PATH']
198            os.environ['LD_LIBRARY_PATH'] = self.path_lib + \
199                os.pathsep + os.environ['LD_LIBRARY_PATH']
200        else:
201            self.ld_path = ''
202            os.environ['LD_LIBRARY_PATH'] = self.path_lib
203
204        # Create environment variable for CMG license server
205        os.environ['CMG_LIC_HOST'] = self.lic
206
207        # Save Process context variable to restore later
208        self.ctx = ctx._default_context
209
210        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
211        # with the newly set environment variables. (The default, 'fork', context only copy its
212        # parent environment variables without taking into account changes made after a Python program is
213        # initialized)
214        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
215
216        # Return self
217        return self
218
219    def __exit__(self, exc_typ, exc_val, exc_trb):
220        """
221        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
222        what is was set to before the 'with'-statement. Input here in this method are required to work in
223        'with'-statement.
224
225        Changelog
226        ---------
227        - ST 25/10-18
228        """
229        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
230        # them from the environment variables
231        if len(self.path):
232            os.environ['PATH'] = self.path
233        else:
234            os.environ.unsetenv('PATH')
235
236        if len(self.ld_path):
237            os.environ['LD_LIBRARY_PATH'] = self.ld_path
238        else:
239            os.environ.unsetenv('LD_LIBRARY_PATH')
240
241        # We unset the CMG license server path
242        os.environ.unsetenv('CMG_LIC_HOST')
243
244        # Reset Process context
245        ctx._default_context = self.ctx
246
247        # Return False (exit 0?)
248        return False
249
250
251class OPMRunEnvironment:
252    """
253    A context manager class to run OPM simulators with correct environmental variables.
254    """
255
256    def __init__(self, filename, suffix, matchstring):
257        """
258
259        - filename: OPM run file, needed to check for errors (string)
260        - suffix: What file to search for complete sign
261        - matchstring: what is the complete sign
262
263        Changelog
264        ---------
265        - KF 30/10-19
266        """
267        self.filename = filename
268        self.suffix = suffix
269        if type(matchstring) != list:
270            self.mstring = list(matchstring)
271        else:
272            self.mstring = matchstring
273
274    def __enter__(self):
275        """
276        Method that is run when class is initiated by a 'with'-statement
277
278        Changelog
279        ---------
280        - KF 30/10-19
281        """
282        # Append if environment variable already exist, or generate it if not.
283        # We also save all environment variables that we intend to alter, so we can restore them when closing the
284        # context manager class
285
286        # Save Process context variable to restore later
287        self.ctx = ctx._default_context
288
289        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
290        # with the newly set environment variables. (The default, 'fork', context only copy its
291        # parent environment variables without taking into account changes made after a Python program is
292        # initialized)
293        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
294
295        # Return self
296        return self
297
298    def __exit__(self, exc_typ, exc_val, exc_trb):
299        """
300        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
301        what it was set to before the 'with'-statement. Input here in this method are required to work in
302        'with'-statement.
303
304        Changelog
305        ---------
306        - ST 25/10-18
307        """
308        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
309        # them from the environment variables
310
311        # Reset Process context
312        ctx._default_context = self.ctx
313
314        member = False
315
316        with open(self.filename + '.' + self.suffix, 'r') as fid:
317            for line in fid:
318                if any([re.search(elem, line) for elem in self.mstring]):
319                    # TODO: not do time.sleep()
320                    # time.sleep(0.1)
321                    member = True
322        if member == False:
323            return False
324        return True
325
326
327class FlowRockRunEnvironment:
328    """
329    A context manager class to run flowRock simulators with correct environmental variables.
330    """
331
332    def __init__(self, filename):
333        """
334
335        - filename: dummy run file
336
337        Changelog
338        ---------
339        - KF 30/10-19
340        """
341        self.filename = filename
342
343    def __enter__(self):
344        """
345        Method that is run when class is initiated by a 'with'-statement
346
347        Changelog
348        ---------
349        - KF 30/10-19
350        """
351        # Append if environment variable already exist, or generate it if not.
352        # We also save all environment variables that we intend to alter, so we can restore them when closing the
353        # context manager class
354
355        # Save Process context variable to restore later
356        self.ctx = ctx._default_context
357
358        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
359        # with the newly set environment variables. (The default, 'fork', context only copy its
360        # parent environment variables without taking into account changes made after a Python program is
361        # initialized)
362        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
363
364        # Return self
365        return self
366
367    def __exit__(self, exc_typ, exc_val, exc_trb):
368        """
369        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
370        what is was set to before the 'with'-statement. Input here in this method are required to work in
371        'with'-statement.
372
373        Changelog
374        ---------
375        - ST 25/10-18
376        """
377        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
378        # them from the environment variables
379
380        # Reset Process context
381        ctx._default_context = self.ctx
382
383        member = False
384
385        if len(self.filename.split(os.sep)) == 1:
386            if self.filename in os.listdir():
387                member = True
388        else:
389            if self.filename.split(os.sep)[1] in os.listdir(self.filename.split(os.sep)[0]):
390                member = True
391
392        if member == False:
393            sys.exit(1)
394
395        return False
396
397
398class EclipseRunEnvironment:
399    """
400    A context manager class to run eclipse simulators with correct environmental variables.
401    """
402
403    def __init__(self, filename):
404        """
405        input
406        filename: eclipse run file, needed to check for errors (string)
407
408        Changelog
409        ---------
410        - KF 30/10-19
411        """
412        self.filename = filename
413
414    def __enter__(self):
415        """
416        Method that is run when class is initiated by a 'with'-statement
417
418        Changelog
419        ---------
420        - KF 30/10-19
421        """
422        # Append if environment variable already exist, or generate it if not.
423        # We also save all environment variables that we intend to alter, so we can restore them when closing the
424        # context manager class
425
426        # Save Process context variable to restore later
427        self.ctx = ctx._default_context
428
429        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
430        # with the newly set environment variables. (The default, 'fork', context only copy its
431        # parent environment variables without taking into account changes made after a Python program is
432        # initialized)
433        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
434
435        # Return self
436        return self
437
438    def __exit__(self, exc_typ, exc_val, exc_trb):
439        """
440        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
441        what is was set to before the 'with'-statement. Input here in this method are required to work in
442        'with'-statement.
443
444        Changelog
445        ---------
446        - ST 25/10-18
447        """
448        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
449        # them from the environment variables
450
451        # Reset Process context
452        ctx._default_context = self.ctx
453
454        error_dict = {}
455
456        with open(self.filename + '.ECLEND', 'r') as f:
457            txt = [value.strip() for value in (f.read()).split('\n')]
458
459        # Search for the text Error Summary which starts the error summary section
460        for j in range(0, len(txt)):
461            if txt[j] == 'Error summary':
462                for k in range(1, 6):
463                    tmp_line = txt[j + k].split(' ')
464                    # store the error statistics as elements in a dictionary
465                    error_dict[tmp_line[0]] = float(tmp_line[-1])
466        # If there are no errors the run was a success. If 'Error summary' cannot be found the run has
467        # not finished.
468        if len(error_dict) > 0:
469            if error_dict['Errors'] > 0:
470                print('\n\033[1;31mERROR: RUN has failed with {} errors!\033[1;m'.format(
471                    error_dict['Errors']))
472                sys.exit(1)
473
474        # Return False (exit 0?)
475        return False
class OpenBlasSingleThread:
 11class OpenBlasSingleThread:
 12    """
 13    A context manager class to set OpenBLAS multi threading environment variable to 1 (i.e., single threaded). The
 14    class is used in a 'with'-statement to ensure that everything inside the statement is run single threaded,
 15    and outside the statement is run using whatever the environment variable was set before the 'with'-statement.
 16    The environment variable setting threading in OpenBLAS is OMP_NUM_THREADS.
 17
 18    Example
 19    -------
 20
 21    >>> from system_tools.environ_var import OpenBlasSingleThread
 22    ... import ctypes
 23    ... import multiprocessing as mp
 24    ...
 25    ... def justdoit():
 26    ...     # Load OpenBLAS library and print number of threads
 27    ...     openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
 28    ...     print(openblas_lib.openblas_get_num_threads())
 29    ...
 30    ... if __name__ == "__main__":
 31    ...     # Load OpenBLAS library and print number of threads before the with-statement
 32    ...     openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
 33    ...     print(openblas_lib.openblas_get_num_threads())
 34    ...
 35    ...     # Run a Process inside the with-statement with the OpenBlasSingleThread class.
 36    ...     with OpenBlasSingleThread ():
 37    ...         p = mp.Process (target=justdoit)
 38    ...         p.start ()
 39    ...         p.join ()
 40    ...
 41    ... # Load OpenBLAS library and print number of threads before the with-statement
 42    ... openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
 43    ... print(openblas_lib.openblas_get_num_threads())
 44    """
 45
 46    def __init__(self):
 47        """
 48        Init. the class with no inputs. Use this to initialize internal variables for storing number of threads an
 49        the Process context manager before the change to single thread.
 50
 51        Parameters
 52        ----------
 53        num_threads:
 54            String with number of OpenBLAS threads before change to single
 55            threaded (it is the content of OMP_NUM_THREADS)
 56        ctx:
 57            The context variable from Process (default is 'fork' context, but
 58            we want to use 'spawn')
 59
 60        Changelog
 61        ---------
 62        - ST 31/10-17
 63        """
 64        self.num_threads = ''
 65        self.ctx = None
 66
 67    def __enter__(self):
 68        """
 69        Method that is run when class is initiated by a 'with'-statement
 70
 71        Changelog
 72        ---------
 73        - ST 31/10-17
 74        """
 75        # Save OMP_NUM_THREADS environment variable to restore later
 76        if 'OMP_NUM_THREADS' in os.environ:
 77            self.num_threads = os.environ['OMP_NUM_THREADS']
 78        else:
 79            self.num_threads = ''
 80
 81        # Set OMP_NUM_THREADS to 1 to ensure single threaded OpenBLAS
 82        os.environ['OMP_NUM_THREADS'] = '1'
 83
 84        # Save Process context variable to restore later
 85        self.ctx = ctx._default_context
 86
 87        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
 88        # with the newly set OMP_NUM_THREADS=1 environment variable. (The default, 'fork', context only copy its
 89        # parent environment variables without taking into account changes made after a Python program is
 90        # initialized)
 91        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
 92
 93        # Return self
 94        return self
 95
 96    def __exit__(self, exc_typ, exc_val, exc_trb):
 97        """
 98        Method that is run when 'with'-statement closes. Here, we reset OMP_NUM_THREADS and Process context to what
 99        is was set to before the 'with'-statement. Input here in this method are required to work in 'with'-statement.
100
101        Changelog
102        ---------
103        - ST 31/10-17
104        """
105        # Check if there was any OMP_NUM_THREADS at all before the with-statement, and reset it thereafter.
106        if len(self.num_threads):
107            os.environ['OMP_NUM_THREADS'] = self.num_threads
108        else:
109            os.environ.unsetenv('OMP_NUM_THREADS')
110
111        # Reset Process context
112        ctx._default_context = self.ctx
113
114        # Return False (exit 0?)
115        return False

A context manager class to set OpenBLAS multi threading environment variable to 1 (i.e., single threaded). The class is used in a 'with'-statement to ensure that everything inside the statement is run single threaded, and outside the statement is run using whatever the environment variable was set before the 'with'-statement. The environment variable setting threading in OpenBLAS is OMP_NUM_THREADS.

Example
>>> from system_tools.environ_var import OpenBlasSingleThread
... import ctypes
... import multiprocessing as mp
...
... def justdoit():
...     # Load OpenBLAS library and print number of threads
...     openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
...     print(openblas_lib.openblas_get_num_threads())
...
... if __name__ == "__main__":
...     # Load OpenBLAS library and print number of threads before the with-statement
...     openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
...     print(openblas_lib.openblas_get_num_threads())
...
...     # Run a Process inside the with-statement with the OpenBlasSingleThread class.
...     with OpenBlasSingleThread ():
...         p = mp.Process (target=justdoit)
...         p.start ()
...         p.join ()
...
... # Load OpenBLAS library and print number of threads before the with-statement
... openblas_lib = ctypes.cdll.LoadLibrary('/scratch/openblas/lib/libopenblas.so')
... print(openblas_lib.openblas_get_num_threads())
OpenBlasSingleThread()
46    def __init__(self):
47        """
48        Init. the class with no inputs. Use this to initialize internal variables for storing number of threads an
49        the Process context manager before the change to single thread.
50
51        Parameters
52        ----------
53        num_threads:
54            String with number of OpenBLAS threads before change to single
55            threaded (it is the content of OMP_NUM_THREADS)
56        ctx:
57            The context variable from Process (default is 'fork' context, but
58            we want to use 'spawn')
59
60        Changelog
61        ---------
62        - ST 31/10-17
63        """
64        self.num_threads = ''
65        self.ctx = None

Init. the class with no inputs. Use this to initialize internal variables for storing number of threads an the Process context manager before the change to single thread.

Parameters
  • num_threads:: String with number of OpenBLAS threads before change to single threaded (it is the content of OMP_NUM_THREADS)
  • ctx:: The context variable from Process (default is 'fork' context, but we want to use 'spawn')
Changelog
  • ST 31/10-17
num_threads
ctx
class CmgRunEnvironment:
118class CmgRunEnvironment:
119    """
120    A context manager class to run CMG simulators with correct environmental variables.
121    """
122
123    def __init__(self, root, simulator, version, license):
124        """
125        We initialize the context manager by setting up correct paths and environment variable names that we set in
126        __enter__.
127
128        Parameters
129        ----------
130        root : str
131            Root folder where CMG simulator(s) are installed.
132
133        simulator : str
134            Simulator name.
135
136        version : str
137            Version of the simulator.
138
139        license : str
140            License server name.
141
142        Changelog
143        ---------
144        - ST 25/10-18
145
146        Notes
147        -----
148        'version' is the release version of CMG, e.g., 2017.101.G.
149        """
150        # In
151        self.root = root
152        self.sim = simulator
153        self.ver = version
154        self.lic = license
155
156        # Internal
157        self.ctx = None
158        self.path = ''
159        self.ld_path = ''
160
161        # Check system platform.
162        # TODO: Figure out paths in other systems (read Windows...) and remove assertion.
163        assert (platform.system() == 'Linux'), \
164            'Sorry, we have only set up paths for Linux systems... But hey, maybe you can implemented it for your ' \
165            'system? :)'
166
167        # Base path to simulator folders
168        self.path_base = self.root + self.ver + os.sep + self.sim + os.sep + self.ver[:-3] + os.sep + \
169            'linux_x64' + os.sep
170
171        # Path to exe file
172        self.path_exe = self.path_base + 'exe'
173
174        # Path to libraries
175        self.path_lib = self.path_base + 'lib'
176
177    def __enter__(self):
178        """
179        Method that is run when class is initiated by a 'with'-statement
180
181        Changelog
182        ---------
183        - ST 25/10-18
184        """
185        # Append if environment variable already exist, or generate it if not.
186        # We also save all environment variables that we intend to alter, so we can restore them when closing the
187        # context manager class
188        # PATH
189        if 'PATH' in os.environ:
190            self.path = os.environ['PATH']
191            os.environ['PATH'] = self.path_exe + os.pathsep + os.environ['PATH']
192        else:
193            self.path = ''
194            os.environ['PATH'] = self.path_exe
195
196        # LD_LIBRARY_PATH
197        if 'LD_LIBRARY_PATH' in os.environ:
198            self.ld_path = os.environ['LD_LIBRARY_PATH']
199            os.environ['LD_LIBRARY_PATH'] = self.path_lib + \
200                os.pathsep + os.environ['LD_LIBRARY_PATH']
201        else:
202            self.ld_path = ''
203            os.environ['LD_LIBRARY_PATH'] = self.path_lib
204
205        # Create environment variable for CMG license server
206        os.environ['CMG_LIC_HOST'] = self.lic
207
208        # Save Process context variable to restore later
209        self.ctx = ctx._default_context
210
211        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
212        # with the newly set environment variables. (The default, 'fork', context only copy its
213        # parent environment variables without taking into account changes made after a Python program is
214        # initialized)
215        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
216
217        # Return self
218        return self
219
220    def __exit__(self, exc_typ, exc_val, exc_trb):
221        """
222        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
223        what is was set to before the 'with'-statement. Input here in this method are required to work in
224        'with'-statement.
225
226        Changelog
227        ---------
228        - ST 25/10-18
229        """
230        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
231        # them from the environment variables
232        if len(self.path):
233            os.environ['PATH'] = self.path
234        else:
235            os.environ.unsetenv('PATH')
236
237        if len(self.ld_path):
238            os.environ['LD_LIBRARY_PATH'] = self.ld_path
239        else:
240            os.environ.unsetenv('LD_LIBRARY_PATH')
241
242        # We unset the CMG license server path
243        os.environ.unsetenv('CMG_LIC_HOST')
244
245        # Reset Process context
246        ctx._default_context = self.ctx
247
248        # Return False (exit 0?)
249        return False

A context manager class to run CMG simulators with correct environmental variables.

CmgRunEnvironment(root, simulator, version, license)
123    def __init__(self, root, simulator, version, license):
124        """
125        We initialize the context manager by setting up correct paths and environment variable names that we set in
126        __enter__.
127
128        Parameters
129        ----------
130        root : str
131            Root folder where CMG simulator(s) are installed.
132
133        simulator : str
134            Simulator name.
135
136        version : str
137            Version of the simulator.
138
139        license : str
140            License server name.
141
142        Changelog
143        ---------
144        - ST 25/10-18
145
146        Notes
147        -----
148        'version' is the release version of CMG, e.g., 2017.101.G.
149        """
150        # In
151        self.root = root
152        self.sim = simulator
153        self.ver = version
154        self.lic = license
155
156        # Internal
157        self.ctx = None
158        self.path = ''
159        self.ld_path = ''
160
161        # Check system platform.
162        # TODO: Figure out paths in other systems (read Windows...) and remove assertion.
163        assert (platform.system() == 'Linux'), \
164            'Sorry, we have only set up paths for Linux systems... But hey, maybe you can implemented it for your ' \
165            'system? :)'
166
167        # Base path to simulator folders
168        self.path_base = self.root + self.ver + os.sep + self.sim + os.sep + self.ver[:-3] + os.sep + \
169            'linux_x64' + os.sep
170
171        # Path to exe file
172        self.path_exe = self.path_base + 'exe'
173
174        # Path to libraries
175        self.path_lib = self.path_base + 'lib'

We initialize the context manager by setting up correct paths and environment variable names that we set in __enter__.

Parameters
  • root (str): Root folder where CMG simulator(s) are installed.
  • simulator (str): Simulator name.
  • version (str): Version of the simulator.
  • license (str): License server name.
Changelog
  • ST 25/10-18
Notes

'version' is the release version of CMG, e.g., 2017.101.G.

root
sim
ver
lic
ctx
path
ld_path
path_base
path_exe
path_lib
class OPMRunEnvironment:
252class OPMRunEnvironment:
253    """
254    A context manager class to run OPM simulators with correct environmental variables.
255    """
256
257    def __init__(self, filename, suffix, matchstring):
258        """
259
260        - filename: OPM run file, needed to check for errors (string)
261        - suffix: What file to search for complete sign
262        - matchstring: what is the complete sign
263
264        Changelog
265        ---------
266        - KF 30/10-19
267        """
268        self.filename = filename
269        self.suffix = suffix
270        if type(matchstring) != list:
271            self.mstring = list(matchstring)
272        else:
273            self.mstring = matchstring
274
275    def __enter__(self):
276        """
277        Method that is run when class is initiated by a 'with'-statement
278
279        Changelog
280        ---------
281        - KF 30/10-19
282        """
283        # Append if environment variable already exist, or generate it if not.
284        # We also save all environment variables that we intend to alter, so we can restore them when closing the
285        # context manager class
286
287        # Save Process context variable to restore later
288        self.ctx = ctx._default_context
289
290        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
291        # with the newly set environment variables. (The default, 'fork', context only copy its
292        # parent environment variables without taking into account changes made after a Python program is
293        # initialized)
294        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
295
296        # Return self
297        return self
298
299    def __exit__(self, exc_typ, exc_val, exc_trb):
300        """
301        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
302        what it was set to before the 'with'-statement. Input here in this method are required to work in
303        'with'-statement.
304
305        Changelog
306        ---------
307        - ST 25/10-18
308        """
309        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
310        # them from the environment variables
311
312        # Reset Process context
313        ctx._default_context = self.ctx
314
315        member = False
316
317        with open(self.filename + '.' + self.suffix, 'r') as fid:
318            for line in fid:
319                if any([re.search(elem, line) for elem in self.mstring]):
320                    # TODO: not do time.sleep()
321                    # time.sleep(0.1)
322                    member = True
323        if member == False:
324            return False
325        return True

A context manager class to run OPM simulators with correct environmental variables.

OPMRunEnvironment(filename, suffix, matchstring)
257    def __init__(self, filename, suffix, matchstring):
258        """
259
260        - filename: OPM run file, needed to check for errors (string)
261        - suffix: What file to search for complete sign
262        - matchstring: what is the complete sign
263
264        Changelog
265        ---------
266        - KF 30/10-19
267        """
268        self.filename = filename
269        self.suffix = suffix
270        if type(matchstring) != list:
271            self.mstring = list(matchstring)
272        else:
273            self.mstring = matchstring
  • filename: OPM run file, needed to check for errors (string)
  • suffix: What file to search for complete sign
  • matchstring: what is the complete sign
Changelog
  • KF 30/10-19
filename
suffix
class FlowRockRunEnvironment:
328class FlowRockRunEnvironment:
329    """
330    A context manager class to run flowRock simulators with correct environmental variables.
331    """
332
333    def __init__(self, filename):
334        """
335
336        - filename: dummy run file
337
338        Changelog
339        ---------
340        - KF 30/10-19
341        """
342        self.filename = filename
343
344    def __enter__(self):
345        """
346        Method that is run when class is initiated by a 'with'-statement
347
348        Changelog
349        ---------
350        - KF 30/10-19
351        """
352        # Append if environment variable already exist, or generate it if not.
353        # We also save all environment variables that we intend to alter, so we can restore them when closing the
354        # context manager class
355
356        # Save Process context variable to restore later
357        self.ctx = ctx._default_context
358
359        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
360        # with the newly set environment variables. (The default, 'fork', context only copy its
361        # parent environment variables without taking into account changes made after a Python program is
362        # initialized)
363        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
364
365        # Return self
366        return self
367
368    def __exit__(self, exc_typ, exc_val, exc_trb):
369        """
370        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
371        what is was set to before the 'with'-statement. Input here in this method are required to work in
372        'with'-statement.
373
374        Changelog
375        ---------
376        - ST 25/10-18
377        """
378        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
379        # them from the environment variables
380
381        # Reset Process context
382        ctx._default_context = self.ctx
383
384        member = False
385
386        if len(self.filename.split(os.sep)) == 1:
387            if self.filename in os.listdir():
388                member = True
389        else:
390            if self.filename.split(os.sep)[1] in os.listdir(self.filename.split(os.sep)[0]):
391                member = True
392
393        if member == False:
394            sys.exit(1)
395
396        return False

A context manager class to run flowRock simulators with correct environmental variables.

FlowRockRunEnvironment(filename)
333    def __init__(self, filename):
334        """
335
336        - filename: dummy run file
337
338        Changelog
339        ---------
340        - KF 30/10-19
341        """
342        self.filename = filename
  • filename: dummy run file
Changelog
  • KF 30/10-19
filename
class EclipseRunEnvironment:
399class EclipseRunEnvironment:
400    """
401    A context manager class to run eclipse simulators with correct environmental variables.
402    """
403
404    def __init__(self, filename):
405        """
406        input
407        filename: eclipse run file, needed to check for errors (string)
408
409        Changelog
410        ---------
411        - KF 30/10-19
412        """
413        self.filename = filename
414
415    def __enter__(self):
416        """
417        Method that is run when class is initiated by a 'with'-statement
418
419        Changelog
420        ---------
421        - KF 30/10-19
422        """
423        # Append if environment variable already exist, or generate it if not.
424        # We also save all environment variables that we intend to alter, so we can restore them when closing the
425        # context manager class
426
427        # Save Process context variable to restore later
428        self.ctx = ctx._default_context
429
430        # Change context to 'spawn' to ensure that any use of Process inside the 'with'-statement will initialize
431        # with the newly set environment variables. (The default, 'fork', context only copy its
432        # parent environment variables without taking into account changes made after a Python program is
433        # initialized)
434        ctx._default_context = (ctx.DefaultContext(ctx._concrete_contexts['spawn']))
435
436        # Return self
437        return self
438
439    def __exit__(self, exc_typ, exc_val, exc_trb):
440        """
441        Method that is run when 'with'-statement closes. Here, we reset environment variables and Process context to
442        what is was set to before the 'with'-statement. Input here in this method are required to work in
443        'with'-statement.
444
445        Changelog
446        ---------
447        - ST 25/10-18
448        """
449        # Reset PATH and LD_LIBRARY_PATH to what they were before our intervention. If they were not set we delete
450        # them from the environment variables
451
452        # Reset Process context
453        ctx._default_context = self.ctx
454
455        error_dict = {}
456
457        with open(self.filename + '.ECLEND', 'r') as f:
458            txt = [value.strip() for value in (f.read()).split('\n')]
459
460        # Search for the text Error Summary which starts the error summary section
461        for j in range(0, len(txt)):
462            if txt[j] == 'Error summary':
463                for k in range(1, 6):
464                    tmp_line = txt[j + k].split(' ')
465                    # store the error statistics as elements in a dictionary
466                    error_dict[tmp_line[0]] = float(tmp_line[-1])
467        # If there are no errors the run was a success. If 'Error summary' cannot be found the run has
468        # not finished.
469        if len(error_dict) > 0:
470            if error_dict['Errors'] > 0:
471                print('\n\033[1;31mERROR: RUN has failed with {} errors!\033[1;m'.format(
472                    error_dict['Errors']))
473                sys.exit(1)
474
475        # Return False (exit 0?)
476        return False

A context manager class to run eclipse simulators with correct environmental variables.

EclipseRunEnvironment(filename)
404    def __init__(self, filename):
405        """
406        input
407        filename: eclipse run file, needed to check for errors (string)
408
409        Changelog
410        ---------
411        - KF 30/10-19
412        """
413        self.filename = filename

input filename: eclipse run file, needed to check for errors (string)

Changelog
  • KF 30/10-19
filename