forked from equinor/ert
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for executing single workflow in CLI
Add tests for entry point Add decorator to requirements
- Loading branch information
Showing
6 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import contextlib | ||
import logging | ||
import os | ||
import tempfile | ||
import shutil | ||
|
||
import decorator | ||
import time | ||
|
||
""" | ||
Swiped from | ||
https://github.com/equinor/everest/blob/master/tests/utils/__init__.py | ||
""" | ||
|
||
SOURCE_DIR = os.path.realpath(os.path.join(__file__, '../../../')) | ||
|
||
def tmpdir(path=None, teardown=True): | ||
""" Decorator based on the `tmp` context """ | ||
def real_decorator(function): | ||
def wrapper(function, *args, **kwargs): | ||
with tmp(path, teardown=teardown): | ||
return function(*args, **kwargs) | ||
return decorator.decorator(wrapper, function) | ||
return real_decorator | ||
|
||
|
||
@contextlib.contextmanager | ||
def tmp(path=None, teardown=True): | ||
"""Create and go into tmp directory, returns the path. | ||
This function creates a temporary directory and enters that directory. The | ||
returned object is the path to the created directory. | ||
If @path is not specified, we create an empty directory, otherwise, it must | ||
be a path to an existing directory. In that case, the directory will be | ||
copied into the temporary directory. | ||
If @teardown is True (defaults to True), the directory is (attempted) | ||
deleted after context, otherwise it is kept as is. | ||
""" | ||
cwd = os.getcwd() | ||
fname = tempfile.NamedTemporaryFile().name | ||
|
||
if path: | ||
if not os.path.isdir(path): | ||
logging.debug('tmp:raise no such path') | ||
raise IOError('No such directory: %s' % path) | ||
shutil.copytree(path, fname) | ||
else: | ||
# no path to copy, create empty dir | ||
os.mkdir(fname) | ||
|
||
os.chdir(fname) | ||
|
||
yield fname # give control to caller scope | ||
|
||
os.chdir(cwd) | ||
|
||
if teardown: | ||
try: | ||
shutil.rmtree(fname) | ||
except OSError as oserr: | ||
logging.debug('tmp:rmtree failed %s (%s)' % (fname, oserr)) | ||
shutil.rmtree(fname, ignore_errors=True) |