Fix special_case_handler to handle git add prev_CMakeLists.txt better

When using run_pro2cmake.py, it spawns multiple instances of
pro2cmake.py.
If more than 1 of the instances need to git add prev_CMakeLists.txt at
the same time, git add might fail due to the acquired index.lock.

The cleaner solution would be to use a file lock as a mutex, but that
requires an external pypi package.

Use a poor man solution of retrying the git add with a time delay for a
finite amount of times.

Change-Id: I2031f6e29ae499526cb4f1753e4387e7f4fab0ab
Reviewed-by: Qt CMake Build Bot
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Alexandru Croitor 2019-06-04 15:17:33 +02:00
parent 63f2b8c7a8
commit b64e8e721f

View File

@ -87,6 +87,7 @@ import re
import os import os
import subprocess import subprocess
import filecmp import filecmp
import time
from shutil import copyfile from shutil import copyfile
from shutil import rmtree from shutil import rmtree
@ -142,7 +143,7 @@ def check_if_git_in_path() -> bool:
return False return False
def run_process_quiet(args_string: str, debug=False) -> None: def run_process_quiet(args_string: str, debug=False) -> bool:
if debug: if debug:
print('Running command: "{}\"'.format(args_string)) print('Running command: "{}\"'.format(args_string))
args_list = args_string.split() args_list = args_string.split()
@ -153,6 +154,8 @@ def run_process_quiet(args_string: str, debug=False) -> None:
# an error for us. # an error for us.
if 'git merge' not in args_string: if 'git merge' not in args_string:
print('Error while running: "{}"\n{}'.format(args_string, e.stdout)) print('Error while running: "{}"\n{}'.format(args_string, e.stdout))
return False
return True
def does_file_have_conflict_markers(file_path: str, debug=False) -> bool: def does_file_have_conflict_markers(file_path: str, debug=False) -> bool:
@ -289,7 +292,28 @@ class SpecialCaseHandler(object):
# merge result, save the new "clean" file for future # merge result, save the new "clean" file for future
# regenerations. # regenerations.
copyfile_log(self.generated_file_path, self.prev_file_path, debug=self.debug) copyfile_log(self.generated_file_path, self.prev_file_path, debug=self.debug)
run_process_quiet("git add {}".format(self.prev_file_path), debug=self.debug)
# Attempt to git add until we succeed. It can fail when
# run_pro2cmake executes pro2cmake in multiple threads, and git
# has acquired the index lock.
success = False
failed_once = False
i = 0
while not success and i < 20:
success = run_process_quiet("git add {}".format(self.prev_file_path),
debug=self.debug)
if not success:
failed_once = True
i += 1
time.sleep(0.1)
if failed_once and not success:
print('Retrying git add, the index.lock was probably acquired.')
if failed_once and success:
print('git add succeeded.')
elif failed_once and not success:
print('git add failed. Make sure to git add {} yourself.'.format(
self.prev_file_path))
def handle_special_cases_helper(self) -> bool: def handle_special_cases_helper(self) -> bool:
""" """