diff options
author | dvshkurko <dvshkurko@yandex-team.ru> | 2022-02-10 16:45:52 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:52 +0300 |
commit | c768a99151e47c3a4bb7b92c514d256abd301c4d (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /library/python/testing/yatest_common/yatest/common/path.py | |
parent | 321ee9bce31ec6e238be26dbcbe539cffa2c3309 (diff) | |
download | ydb-c768a99151e47c3a4bb7b92c514d256abd301c4d.tar.gz |
Restoring authorship annotation for <dvshkurko@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/python/testing/yatest_common/yatest/common/path.py')
-rw-r--r-- | library/python/testing/yatest_common/yatest/common/path.py | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/library/python/testing/yatest_common/yatest/common/path.py b/library/python/testing/yatest_common/yatest/common/path.py index 1d08f72e62..6fed7dda8a 100644 --- a/library/python/testing/yatest_common/yatest/common/path.py +++ b/library/python/testing/yatest_common/yatest/common/path.py @@ -1,13 +1,13 @@ # coding=utf-8 -import errno +import errno import os import shutil import contextlib -import library.python.fs as lpf +import library.python.fs as lpf + - def replace_in_file(path, old, new): """ Replace text occurrences in a file @@ -18,7 +18,7 @@ def replace_in_file(path, old, new): with open(path) as fp: content = fp.read() - lpf.ensure_removed(path) + lpf.ensure_removed(path) with open(path, 'w') as fp: fp.write(content.replace(old, new)) @@ -56,35 +56,35 @@ def copytree(src, dst, symlinks=False, ignore=None, postprocessing=None): postprocessing(os.path.join(root, path), True) -def get_unique_file_path(dir_path, file_pattern, create_file=True, max_suffix=10000): - def atomic_file_create(path): - try: +def get_unique_file_path(dir_path, file_pattern, create_file=True, max_suffix=10000): + def atomic_file_create(path): + try: fd = os.open(path, os.O_CREAT | os.O_EXCL, 0o644) - os.close(fd) - return True - except OSError as e: - if e.errno in [errno.EEXIST, errno.EISDIR, errno.ETXTBSY]: - return False - # Access issue with file itself, not parent directory. - if e.errno == errno.EACCES and os.path.exists(path): - return False - raise e - - def atomic_dir_create(path): - try: - os.mkdir(path) - return True - except OSError as e: - if e.errno == errno.EEXIST: - return False - raise e - + os.close(fd) + return True + except OSError as e: + if e.errno in [errno.EEXIST, errno.EISDIR, errno.ETXTBSY]: + return False + # Access issue with file itself, not parent directory. + if e.errno == errno.EACCES and os.path.exists(path): + return False + raise e + + def atomic_dir_create(path): + try: + os.mkdir(path) + return True + except OSError as e: + if e.errno == errno.EEXIST: + return False + raise e + file_path = os.path.join(dir_path, file_pattern) - lpf.ensure_dir(os.path.dirname(file_path)) + lpf.ensure_dir(os.path.dirname(file_path)) file_counter = 0 - handler = atomic_file_create if create_file else atomic_dir_create - while os.path.exists(file_path) or not handler(file_path): + handler = atomic_file_create if create_file else atomic_dir_create + while os.path.exists(file_path) or not handler(file_path): file_path = os.path.join(dir_path, file_pattern + ".{}".format(file_counter)) file_counter += 1 - assert file_counter < max_suffix + assert file_counter < max_suffix return file_path |