diff options
author | Dmitry Kopylov <kopylovd@gmail.com> | 2022-02-10 16:48:18 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:18 +0300 |
commit | b2f5101486cc0de2e979c8ba9ada2109785bf5fd (patch) | |
tree | affe28b840816b505db0467f2285b01c89c04247 /library/python/testing/yatest_common/yatest/common/canonical.py | |
parent | e9b28b5aad71453a4637b70dde02e801e4147a2a (diff) | |
download | ydb-b2f5101486cc0de2e979c8ba9ada2109785bf5fd.tar.gz |
Restoring authorship annotation for Dmitry Kopylov <kopylovd@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/python/testing/yatest_common/yatest/common/canonical.py')
-rw-r--r-- | library/python/testing/yatest_common/yatest/common/canonical.py | 268 |
1 files changed, 134 insertions, 134 deletions
diff --git a/library/python/testing/yatest_common/yatest/common/canonical.py b/library/python/testing/yatest_common/yatest/common/canonical.py index b6a136d3e9..9c02fe1c11 100644 --- a/library/python/testing/yatest_common/yatest/common/canonical.py +++ b/library/python/testing/yatest_common/yatest/common/canonical.py @@ -1,17 +1,17 @@ -import os -import logging -import shutil -import tempfile - +import os +import logging +import shutil +import tempfile + import six from . import process from . import runtime from . import path - -yatest_logger = logging.getLogger("ya.test") - - + +yatest_logger = logging.getLogger("ya.test") + + def _copy(src, dst, universal_lines=False): if universal_lines: with open(dst, "wb") as f: @@ -21,21 +21,21 @@ def _copy(src, dst, universal_lines=False): shutil.copy(src, dst) -def canonical_file(path, diff_tool=None, local=False, universal_lines=False, diff_file_name=None, diff_tool_timeout=None): - """ - Create canonical file that can be returned from a test - :param path: path to the file - :param diff_tool: custom diff tool to use for comparison with the canonical one, if None - default will be used - :param local: save file locally, otherwise move to sandbox +def canonical_file(path, diff_tool=None, local=False, universal_lines=False, diff_file_name=None, diff_tool_timeout=None): + """ + Create canonical file that can be returned from a test + :param path: path to the file + :param diff_tool: custom diff tool to use for comparison with the canonical one, if None - default will be used + :param local: save file locally, otherwise move to sandbox :param universal_lines: normalize EOL - :param diff_tool_timeout: timeout for running diff tool - :return: object that can be canonized - """ - abs_path = os.path.abspath(path) - assert os.path.exists(abs_path), "Canonical path {} does not exist".format(path) - tempdir = tempfile.mkdtemp(prefix="canon_tmp", dir=runtime.build_path()) - safe_path = os.path.join(tempdir, os.path.basename(abs_path)) - # if the created file is in output_path, we copy it, so that it will be available when the tests finishes + :param diff_tool_timeout: timeout for running diff tool + :return: object that can be canonized + """ + abs_path = os.path.abspath(path) + assert os.path.exists(abs_path), "Canonical path {} does not exist".format(path) + tempdir = tempfile.mkdtemp(prefix="canon_tmp", dir=runtime.build_path()) + safe_path = os.path.join(tempdir, os.path.basename(abs_path)) + # if the created file is in output_path, we copy it, so that it will be available when the tests finishes _copy(path, safe_path, universal_lines=universal_lines) if diff_tool: if not isinstance(diff_tool, six.string_types): @@ -44,133 +44,133 @@ def canonical_file(path, diff_tool=None, local=False, universal_lines=False, dif raise Exception("Invalid custom diff-tool: not cmd") except: raise Exception("Invalid custom diff-tool: not binary path") - return runtime._get_ya_plugin_instance().file(safe_path, diff_tool=diff_tool, local=local, diff_file_name=diff_file_name, diff_tool_timeout=diff_tool_timeout) - - -def canonical_dir(path, diff_tool=None, diff_file_name=None, diff_tool_timeout=None): - abs_path = os.path.abspath(path) - assert os.path.exists(abs_path), "Canonical path {} does not exist".format(path) - assert os.path.isdir(abs_path), "Path {} is not a directory".format(path) - if diff_file_name and not diff_tool: - raise Exception("diff_file_name can be only be used with diff_tool for canonical_dir") - tempdir = tempfile.mkdtemp() - safe_path = os.path.join(tempdir, os.path.basename(abs_path)) - shutil.copytree(abs_path, safe_path) - return runtime._get_ya_plugin_instance().file(safe_path, diff_tool=diff_tool, diff_file_name=diff_file_name, diff_tool_timeout=diff_tool_timeout) - - -def canonical_execute( - binary, args=None, check_exit_code=True, - shell=False, timeout=None, cwd=None, - env=None, stdin=None, stderr=None, creationflags=0, + return runtime._get_ya_plugin_instance().file(safe_path, diff_tool=diff_tool, local=local, diff_file_name=diff_file_name, diff_tool_timeout=diff_tool_timeout) + + +def canonical_dir(path, diff_tool=None, diff_file_name=None, diff_tool_timeout=None): + abs_path = os.path.abspath(path) + assert os.path.exists(abs_path), "Canonical path {} does not exist".format(path) + assert os.path.isdir(abs_path), "Path {} is not a directory".format(path) + if diff_file_name and not diff_tool: + raise Exception("diff_file_name can be only be used with diff_tool for canonical_dir") + tempdir = tempfile.mkdtemp() + safe_path = os.path.join(tempdir, os.path.basename(abs_path)) + shutil.copytree(abs_path, safe_path) + return runtime._get_ya_plugin_instance().file(safe_path, diff_tool=diff_tool, diff_file_name=diff_file_name, diff_tool_timeout=diff_tool_timeout) + + +def canonical_execute( + binary, args=None, check_exit_code=True, + shell=False, timeout=None, cwd=None, + env=None, stdin=None, stderr=None, creationflags=0, file_name=None, save_locally=False, close_fds=False, diff_tool=None, diff_file_name=None, diff_tool_timeout=None, -): - """ - Shortcut to execute a binary and canonize its stdout +): + """ + Shortcut to execute a binary and canonize its stdout :param binary: absolute path to the binary - :param args: binary arguments - :param check_exit_code: will raise ExecutionError if the command exits with non zero code - :param shell: use shell to run the command - :param timeout: execution timeout - :param cwd: working directory - :param env: command environment - :param stdin: command stdin - :param stderr: command stderr - :param creationflags: command creation flags + :param args: binary arguments + :param check_exit_code: will raise ExecutionError if the command exits with non zero code + :param shell: use shell to run the command + :param timeout: execution timeout + :param cwd: working directory + :param env: command environment + :param stdin: command stdin + :param stderr: command stderr + :param creationflags: command creation flags :param file_name: output file name. if not specified program name will be used - :param diff_tool: path to custome diff tool - :param diff_file_name: custom diff file name to create when diff is found + :param diff_tool: path to custome diff tool + :param diff_file_name: custom diff file name to create when diff is found :param diff_tool_timeout: timeout for running diff tool - :return: object that can be canonized - """ - if type(binary) == list: - command = binary - else: - command = [binary] - command += _prepare_args(args) - if shell: - command = " ".join(command) - execute_args = locals() - del execute_args["binary"] - del execute_args["args"] + :return: object that can be canonized + """ + if type(binary) == list: + command = binary + else: + command = [binary] + command += _prepare_args(args) + if shell: + command = " ".join(command) + execute_args = locals() + del execute_args["binary"] + del execute_args["args"] del execute_args["file_name"] - del execute_args["save_locally"] - del execute_args["diff_tool"] - del execute_args["diff_file_name"] + del execute_args["save_locally"] + del execute_args["diff_tool"] + del execute_args["diff_file_name"] del execute_args["diff_tool_timeout"] - if not file_name and stdin: - file_name = os.path.basename(stdin.name) + if not file_name and stdin: + file_name = os.path.basename(stdin.name) return _canonical_execute(process.execute, execute_args, file_name, save_locally, diff_tool, diff_file_name, diff_tool_timeout) - - -def canonical_py_execute( - script_path, args=None, check_exit_code=True, - shell=False, timeout=None, cwd=None, env=None, - stdin=None, stderr=None, creationflags=0, - file_name=None, save_locally=False, close_fds=False, + + +def canonical_py_execute( + script_path, args=None, check_exit_code=True, + shell=False, timeout=None, cwd=None, env=None, + stdin=None, stderr=None, creationflags=0, + file_name=None, save_locally=False, close_fds=False, diff_tool=None, diff_file_name=None, diff_tool_timeout=None, -): - """ - Shortcut to execute a python script and canonize its stdout - :param script_path: path to the script arcadia relative - :param args: script arguments - :param check_exit_code: will raise ExecutionError if the command exits with non zero code - :param shell: use shell to run the command - :param timeout: execution timeout - :param cwd: working directory - :param env: command environment - :param stdin: command stdin - :param stderr: command stderr - :param creationflags: command creation flags +): + """ + Shortcut to execute a python script and canonize its stdout + :param script_path: path to the script arcadia relative + :param args: script arguments + :param check_exit_code: will raise ExecutionError if the command exits with non zero code + :param shell: use shell to run the command + :param timeout: execution timeout + :param cwd: working directory + :param env: command environment + :param stdin: command stdin + :param stderr: command stderr + :param creationflags: command creation flags :param file_name: output file name. if not specified program name will be used - :param diff_tool: path to custome diff tool - :param diff_file_name: custom diff file name to create when diff is found + :param diff_tool: path to custome diff tool + :param diff_file_name: custom diff file name to create when diff is found :param diff_tool_timeout: timeout for running diff tool - :return: object that can be canonized - """ - command = [runtime.source_path(script_path)] + _prepare_args(args) - if shell: - command = " ".join(command) - execute_args = locals() - del execute_args["script_path"] - del execute_args["args"] + :return: object that can be canonized + """ + command = [runtime.source_path(script_path)] + _prepare_args(args) + if shell: + command = " ".join(command) + execute_args = locals() + del execute_args["script_path"] + del execute_args["args"] del execute_args["file_name"] - del execute_args["save_locally"] - del execute_args["diff_tool"] - del execute_args["diff_file_name"] + del execute_args["save_locally"] + del execute_args["diff_tool"] + del execute_args["diff_file_name"] del execute_args["diff_tool_timeout"] return _canonical_execute(process.py_execute, execute_args, file_name, save_locally, diff_tool, diff_file_name, diff_tool_timeout) - - -def _prepare_args(args): - if args is None: - args = [] + + +def _prepare_args(args): + if args is None: + args = [] if isinstance(args, six.string_types): - args = map(lambda a: a.strip(), args.split()) - return args - - + args = map(lambda a: a.strip(), args.split()) + return args + + def _canonical_execute(excutor, kwargs, file_name, save_locally, diff_tool, diff_file_name, diff_tool_timeout): - res = excutor(**kwargs) - command = kwargs["command"] - file_name = file_name or process.get_command_name(command) - if file_name.endswith(".exe"): - file_name = os.path.splitext(file_name)[0] # don't want to bring windows stuff in file names - out_file_path = path.get_unique_file_path(runtime.output_path(), "{}.out.txt".format(file_name)) - err_file_path = path.get_unique_file_path(runtime.output_path(), "{}.err.txt".format(file_name)) - - try: - os.makedirs(os.path.dirname(out_file_path)) - except OSError: - pass - + res = excutor(**kwargs) + command = kwargs["command"] + file_name = file_name or process.get_command_name(command) + if file_name.endswith(".exe"): + file_name = os.path.splitext(file_name)[0] # don't want to bring windows stuff in file names + out_file_path = path.get_unique_file_path(runtime.output_path(), "{}.out.txt".format(file_name)) + err_file_path = path.get_unique_file_path(runtime.output_path(), "{}.err.txt".format(file_name)) + + try: + os.makedirs(os.path.dirname(out_file_path)) + except OSError: + pass + with open(out_file_path, "wb") as out_file: - yatest_logger.debug("Will store file in %s", out_file_path) - out_file.write(res.std_out) - - if res.std_err: + yatest_logger.debug("Will store file in %s", out_file_path) + out_file.write(res.std_out) + + if res.std_err: with open(err_file_path, "wb") as err_file: - err_file.write(res.std_err) - + err_file.write(res.std_err) + return canonical_file(out_file_path, local=save_locally, diff_tool=diff_tool, diff_file_name=diff_file_name, diff_tool_timeout=diff_tool_timeout) |