diff options
Diffstat (limited to 'vnftest/common/utils.py')
-rw-r--r-- | vnftest/common/utils.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/vnftest/common/utils.py b/vnftest/common/utils.py index e62b5db..fbc8391 100644 --- a/vnftest/common/utils.py +++ b/vnftest/common/utils.py @@ -397,3 +397,30 @@ class Timer(object): def __getattr__(self, item): return getattr(self.delta, item) + +def find_relative_file(path, task_path): + """ + Find file in one of places: in abs of path or relative to a directory path, + in this order. + + :param path: + :param task_path: + :return str: full path to file + """ + # fixme: create schema to validate all fields have been provided + for lookup in [os.path.abspath(path), os.path.join(task_path, path)]: + try: + with open(lookup): + return lookup + except IOError: + pass + raise IOError(errno.ENOENT, 'Unable to find {} file'.format(path)) + + +def open_relative_file(path, task_path): + try: + return open(path) + except IOError as e: + if e.errno == errno.ENOENT: + return open(os.path.join(task_path, path)) + raise |