1
0
Fork 0

Raise repo exit errors in place of sys.exit

Bug: b/293344017
Change-Id: I92d81c78eba8ff31b5252415f4c9a515a6c76411
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/381774
Tested-by: Jason Chang <jasonnc@google.com>
Reviewed-by: Joanna Wang <jojwang@google.com>
Commit-Queue: Jason Chang <jasonnc@google.com>
This commit is contained in:
Jason Chang 2023-08-03 14:38:00 -07:00 committed by LUCI
parent b8a7b4a629
commit f9aacd4087
9 changed files with 80 additions and 34 deletions

View file

@ -18,6 +18,11 @@ import subprocess
import sys
from urllib.parse import urlparse
from urllib.request import urlopen
from error import RepoExitError
class FetchFileError(RepoExitError):
"""Exit error when fetch_file fails."""
def fetch_file(url, verbose=False):
@ -29,6 +34,7 @@ def fetch_file(url, verbose=False):
scheme = urlparse(url).scheme
if scheme == "gs":
cmd = ["gsutil", "cat", url]
errors = []
try:
result = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True
@ -41,9 +47,10 @@ def fetch_file(url, verbose=False):
)
return result.stdout
except subprocess.CalledProcessError as e:
errors.append(e)
print(
'fatal: error running "gsutil": %s' % e.stderr, file=sys.stderr
)
sys.exit(1)
raise FetchFileError(aggregate_errors=errors)
with urlopen(url) as f:
return f.read()