From a5cf6979925e1d1730bc37884e2d4d70e9b94047 Mon Sep 17 00:00:00 2001 From: Michael Sbarra Date: Mon, 7 Oct 2024 13:55:27 -0700 Subject: [PATCH] [21] Allow retries for subprocess commands --- repos/config/detail/lock_repos.py | 59 +++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/repos/config/detail/lock_repos.py b/repos/config/detail/lock_repos.py index 08eebc4..2ec2c74 100755 --- a/repos/config/detail/lock_repos.py +++ b/repos/config/detail/lock_repos.py @@ -104,25 +104,46 @@ def extract_archive_root_folder(path, origin): def fetch_git_details(url, version, **kwargs): cwd = os.getcwd() - with tempfile.TemporaryDirectory() as tempdir: - result = subprocess.run( - ["git", "clone", url, "--no-checkout", tempdir, "--depth", "1", - "--branch", version, "--bare", "-q"], - capture_output = True, - encoding='utf8' - ) - if result.returncode != 0: - raise ValueError(result.stderr) - os.chdir(tempdir) - result = subprocess.run( - ["git", "log", "--date=raw", "--format=format:%H/%cd"], - stdout=subprocess.PIPE, - encoding='utf8' - ) - if result.returncode != 0: - raise ValueError(result.stderr) - commit_hash, time = result.stdout.split("/") - os.chdir(cwd) + max_retries = 2 + for i in range(max_retries + 1): + with tempfile.TemporaryDirectory() as tempdir: + try: + result = subprocess.run( + ["git", "clone", url, "--no-checkout", tempdir, "--depth", "1", + "--branch", version, "--bare", "-q"], + capture_output = True, + encoding='utf8', + timeout=20 + ) + if result.returncode != 0: + if max_retries == i: + raise ValueError(result.stderr) + else: + print(f"git clone returncode failure. Retrying attempt {i+1}/{max_retries}") + continue + os.chdir(tempdir) + result = subprocess.run( + ["git", "log", "--date=raw", "--format=format:%H/%cd"], + stdout=subprocess.PIPE, + encoding='utf8', + timeout=5 + ) + if result.returncode != 0: + if max_retries == i: + raise ValueError(result.stderr) + else: + print(f"git log returncode failure. Retrying attempt {i+1}/{max_retries}") + continue + except subprocess.TimeoutExpired as e: + if max_retries == i: + raise e + else: + print(f"subprocess.TimeoutExpired failure. Retrying attempt {i+1}/{max_retries}") + continue + + commit_hash, time = result.stdout.split("/") + os.chdir(cwd) + break return { "hash":commit_hash, "shallow_since": time,