From 90d70e4138a3e9f8cfdcaa2c82f6150218e2b0ba Mon Sep 17 00:00:00 2001 From: XiaGuochao Date: Fri, 20 Mar 2020 10:26:16 +0800 Subject: [PATCH] modify rules for creating temporary path and copying dependent rules --- test/maple_test/main.py | 2 +- test/maple_test/task.py | 94 +++++++++++++++++++++------------------- test/maple_test/test.py | 17 +++++++- test/maple_test/utils.py | 1 + 4 files changed, 67 insertions(+), 47 deletions(-) diff --git a/test/maple_test/main.py b/test/maple_test/main.py index 6f3d35f550..446ef65cb3 100644 --- a/test/maple_test/main.py +++ b/test/maple_test/main.py @@ -66,7 +66,7 @@ def main(): temp_dir = running_config.get("temp_dir") if configs.get_val("debug"): logger.debug("Keep temp file at %s", temp_dir) - else: + elif temp_dir.exists(): logger.debug("remove temp_dir %s", temp_dir) shutil.rmtree(str(temp_dir)) diff --git a/test/maple_test/task.py b/test/maple_test/task.py index 5011bd8d2d..98818e420d 100644 --- a/test/maple_test/task.py +++ b/test/maple_test/task.py @@ -336,20 +336,45 @@ class SingleTask: "env": env, } self.case_path = case.relative_path - self.prepare_temp(case.path, self.work_dir) + self.prepare(case, self.work_dir, config) log_dir = (running_config.get("log_config").get("dir") / self.name).parent self.prepare_dir(log_dir) self.result = (NOT_RUN, None) - def prepare_temp(self, src, dest): + def prepare(self, case, dest, config): + dependence = case.dependence + src_path = case.path + src_dir = src_path.parent logger = configs.LOGGER - if not src.exists(): - logger.debug("Source: {} is not existing.\n".format(src)) + if not src_path.exists(): + logger.debug("Source: {} is not existing.\n".format(src_path)) return self.prepare_dir(dest) - shutil.copy(str(src), str(dest)) - for command in self.commands: - self.move_needed_files(command, src, dest) + shutil.copy(str(src_path), str(dest)) + logger.debug("Copy {} => {}".format(src_path, dest)) + self.prepare_dependence(src_dir, dependence, dest, config) + + @staticmethod + def prepare_dependence(src_dir, dependence, dest, config): + logger = configs.LOGGER + src_files = [] + for file in dependence: + file = SingleTask._form_line(file, config) + src_path = src_dir / file + if src_path.exists(): + src_files.append(src_path) + src_files = set(src_files) + for file in src_files: + if file.is_file(): + shutil.copy(str(file), str(dest)) + else: + name = file.name + try: + shutil.copytree(str(file), str(dest / name)) + except: + pass + if src_files: + logger.debug("Copy {} => {}".format(src_files, dest)) @staticmethod def prepare_dir(directory): @@ -366,50 +391,29 @@ class SingleTask: def _form_commands(self, case, config): for command in case.commands: - for key, value in config.get("internal_var").items(): - end = 0 - while end < len(command): - start = command.find("%{}".format(key), end) - if start == -1: - break - end = len(key) + start + 1 - if end == len(command): - command = command[:start] + value + command[end:] - continue - if command[end].isalnum() or command[end] == "_": - continue - else: - command = command[:start] + value + command[end:] + command = self._form_line(command, config) compare_cmd = " {} {} --comment={} ".format( EXECUTABLE, COMPARE, shlex.quote(case.comment) ) self.commands.append(format_compare_command(command, compare_cmd)) @staticmethod - def move_needed_files(command, src, dest): - """Move required files to temporary directory""" - logger = configs.LOGGER - copy_file = "" - for part in set(shlex.split(command)): - if not part: - continue - if part[0] in ["/", "|", "%", "$"] or part in [".", "..", "~"]: - pass - else: - temp_src = src.parent / part - if temp_src.exists(): - dest_path = (dest / part).parent - if not dest_path.exists(): - dest_path.mkdir() - logger.debug("Mkdir: {}".format(dest_path)) - if temp_src.is_file(): - shutil.copy(str(temp_src), str(dest_path)) - copy_file += "{}, ".format(temp_src) - if temp_src.is_dir(): - shutil.copytree(str(temp_src), str(dest / part)) - copy_file += "{}, ".format(temp_src) - if copy_file: - logger.debug("copy file: {}to {}".format(copy_file, dest)) + def _form_line(line, config): + for key, value in config.get("internal_var").items(): + end = 0 + while end < len(line): + start = line.find("%{}".format(key), end) + if start == -1: + break + end = len(key) + start + 1 + if end == len(line): + line = line[:start] + value + line[end:] + continue + if line[end].isalnum() or line[end] == "_": + continue + else: + line = line[:start] + value + line[end:] + return line def format_compare_command(raw_command, compare_cmd): diff --git a/test/maple_test/test.py b/test/maple_test/test.py index cdae5e8554..441c33ad0e 100644 --- a/test/maple_test/test.py +++ b/test/maple_test/test.py @@ -14,8 +14,9 @@ # FIT FOR A PARTICULAR PURPOSE. # See the Mulan PSL v1 for more details. # +import shlex -from maple_test.utils import PASS, EXEC_FLAG, EXPECT_FLAG +from maple_test.utils import PASS, EXEC_FLAG, EXPECT_FLAG, DEPENDENCE_FLAG from maple_test.utils import read_file_with_multi_encoding from maple_test.utils import split_comment, filter_line @@ -38,6 +39,7 @@ class Case: command for command in self.extract_commands(comment_lines) if command ] self.expect = self.extract_expect(comment_lines) + self.dependence = self.extract_dependence(comment_lines) @staticmethod def extract_expect(comment_lines): @@ -49,6 +51,19 @@ class Case: expect = expect_line[-1] return expect + @staticmethod + def extract_dependence(comment_lines): + support_separartor = ",; " + dependence_line = [filter_line(line, DEPENDENCE_FLAG) for line in comment_lines] + dependence_line = [line for line in dependence_line if line] + dependence = [] + for line in dependence_line: + parser = shlex.shlex(line) + parser.whitespace += support_separartor + parser.whitespace_split = True + dependence += list(parser) + return set(dependence) + @staticmethod def extract_commands(comment_lines): commands = [] diff --git a/test/maple_test/utils.py b/test/maple_test/utils.py index 5c92a22e3d..d00fd74d55 100644 --- a/test/maple_test/utils.py +++ b/test/maple_test/utils.py @@ -26,6 +26,7 @@ from pathlib import Path EXEC_FLAG = "EXEC" ASSERT_FLAG = "ASSERT" EXPECT_FLAG = "EXPECT" +DEPENDENCE_FLAG = "DEPENDENCE" PASS = "PASS" FAIL = "FAIL" XFAIL = "XFAIL" -- Gitee