diff options
| author | Mike Frysinger <vapier@google.com> | 2020-02-14 23:38:28 -0500 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2020-02-15 19:32:28 +0000 |
| commit | 6a784ff9a6ea025021a33703152c7b2b1becb7fc (patch) | |
| tree | c3e6cd849a2545f5b06e3ea03dd8c9446bfb070f | |
| parent | a46bf7dc2af111ae4a663d61ed06dc90ddfb8068 (diff) | |
| download | git-repo-6a784ff9a6ea025021a33703152c7b2b1becb7fc.tar.gz | |
repo: handle bad programs a bit better
If programs emit non-UTF-8 output, we currently throw a fatal error.
We largely only care about the exit status of programs, and even the
output we do parse is a bit minimal. Lets make it into a warning and
mangle the invalid bytes into U+FFFD. This should complain enough to
annoy but not to break when it's not necessary.
Bug: https://crbug.com/gerrit/12337#c2
Change-Id: Idbc94f19ff4d84d2e47e01960dd17d5b492d4a8a
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/255272
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
| -rwxr-xr-x | repo | 18 |
1 files changed, 13 insertions, 5 deletions
| @@ -363,15 +363,23 @@ def run_command(cmd, **kwargs): | |||
| 363 | kwargs.setdefault('stderr', subprocess.PIPE) | 363 | kwargs.setdefault('stderr', subprocess.PIPE) |
| 364 | cmd_input = kwargs.pop('input', None) | 364 | cmd_input = kwargs.pop('input', None) |
| 365 | 365 | ||
| 366 | def decode(output): | ||
| 367 | """Decode |output| to text.""" | ||
| 368 | if output is None: | ||
| 369 | return output | ||
| 370 | try: | ||
| 371 | return output.decode('utf-8') | ||
| 372 | except UnicodeError: | ||
| 373 | print('repo: warning: Invalid UTF-8 output:\ncmd: %r\n%r' % (cmd, output), | ||
| 374 | file=sys.stderr) | ||
| 375 | # TODO(vapier): Once we require Python 3, use 'backslashreplace'. | ||
| 376 | return output.decode('utf-8', 'replace') | ||
| 377 | |||
| 366 | # Run & package the results. | 378 | # Run & package the results. |
| 367 | proc = subprocess.Popen(cmd, **kwargs) | 379 | proc = subprocess.Popen(cmd, **kwargs) |
| 368 | (stdout, stderr) = proc.communicate(input=cmd_input) | 380 | (stdout, stderr) = proc.communicate(input=cmd_input) |
| 369 | if stdout is not None: | ||
| 370 | stdout = stdout.decode('utf-8') | ||
| 371 | if stderr is not None: | ||
| 372 | stderr = stderr.decode('utf-8') | ||
| 373 | trace.print(':', ' '.join(cmd)) | 381 | trace.print(':', ' '.join(cmd)) |
| 374 | ret = RunResult(proc.returncode, stdout, stderr) | 382 | ret = RunResult(proc.returncode, decode(stdout), decode(stderr)) |
| 375 | 383 | ||
| 376 | # If things failed, print useful debugging output. | 384 | # If things failed, print useful debugging output. |
| 377 | if check and ret.returncode: | 385 | if check and ret.returncode: |
