From 118e04013ab35fd5b6607ca239a44ab80fb064ed Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 11 Jun 2019 15:13:59 +0200 Subject: Improve special case handler to work on Windows There were some issues with not finding the git executable and trying to remove read-only files / files that are still held by a process. Change-Id: I7f587c4e96cff763cc0b3438d9ed2249da8f122f Reviewed-by: Simon Hausmann --- util/cmake/special_case_helper.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'util') diff --git a/util/cmake/special_case_helper.py b/util/cmake/special_case_helper.py index 1cb25ac1e2..b9cb93dce0 100644 --- a/util/cmake/special_case_helper.py +++ b/util/cmake/special_case_helper.py @@ -88,6 +88,8 @@ import os import subprocess import filecmp import time +import typing +import stat from shutil import copyfile from shutil import rmtree @@ -136,8 +138,11 @@ def copyfile_log(src: str, dst: str, debug=False): def check_if_git_in_path() -> bool: + is_win = os.name == 'nt' for path in os.environ['PATH'].split(os.pathsep): git_path = os.path.join(path, 'git') + if is_win: + git_path += '.exe' if os.path.isfile(git_path) and os.access(git_path, os.X_OK): return True return False @@ -187,6 +192,17 @@ def create_file_with_no_special_cases(original_file_path: str, no_special_cases_ write_content_to_file(no_special_cases_file_path, content_no_special_cases) +def rm_tree_on_error_handler(func: typing.Callable[..., None], + path: str, exception_info: tuple): + # If the path is read only, try to make it writable, and try + # to remove the path again. + if not os.access(path, os.W_OK): + os.chmod(path, stat.S_IWRITE) + func(path) + else: + print('Error while trying to remove path: {}. Exception: {}'.format(path, exception_info)) + + class SpecialCaseHandler(object): def __init__(self, @@ -275,14 +291,14 @@ class SpecialCaseHandler(object): print('Git merge conflict resolution process failed. Exception: {}'.format(e)) raise e finally: + os.chdir(current_dir) + # Remove the temporary repo. try: if not self.keep_temporary_files: - rmtree(repo_absolute_path) + rmtree(repo_absolute_path, onerror=rm_tree_on_error_handler) except Exception as e: - print(e) - - os.chdir(current_dir) + print('Error removing temporary repo. Exception: {}'.format(e)) def save_next_clean_file(self): files_are_equivalent = filecmp.cmp(self.generated_file_path, self.post_merge_file_path) -- cgit v1.2.3