diff options
Diffstat (limited to 'main.py')
| -rwxr-xr-x | main.py | 33 |
1 files changed, 31 insertions, 2 deletions
| @@ -25,6 +25,7 @@ import netrc | |||
| 25 | import optparse | 25 | import optparse |
| 26 | import os | 26 | import os |
| 27 | import shlex | 27 | import shlex |
| 28 | import signal | ||
| 28 | import sys | 29 | import sys |
| 29 | import textwrap | 30 | import textwrap |
| 30 | import time | 31 | import time |
| @@ -95,6 +96,7 @@ else: | |||
| 95 | file=sys.stderr, | 96 | file=sys.stderr, |
| 96 | ) | 97 | ) |
| 97 | 98 | ||
| 99 | KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT | ||
| 98 | 100 | ||
| 99 | global_options = optparse.OptionParser( | 101 | global_options = optparse.OptionParser( |
| 100 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]", | 102 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]", |
| @@ -374,7 +376,11 @@ class _Repo(object): | |||
| 374 | git_trace2_event_log.StartEvent() | 376 | git_trace2_event_log.StartEvent() |
| 375 | git_trace2_event_log.CommandEvent(name="repo", subcommands=[name]) | 377 | git_trace2_event_log.CommandEvent(name="repo", subcommands=[name]) |
| 376 | 378 | ||
| 377 | try: | 379 | def execute_command_helper(): |
| 380 | """ | ||
| 381 | Execute the subcommand. | ||
| 382 | """ | ||
| 383 | nonlocal result | ||
| 378 | cmd.CommonValidateOptions(copts, cargs) | 384 | cmd.CommonValidateOptions(copts, cargs) |
| 379 | cmd.ValidateOptions(copts, cargs) | 385 | cmd.ValidateOptions(copts, cargs) |
| 380 | 386 | ||
| @@ -409,6 +415,23 @@ class _Repo(object): | |||
| 409 | if hasattr(copts, "manifest_branch"): | 415 | if hasattr(copts, "manifest_branch"): |
| 410 | child_argv.extend(["--manifest-branch", spec.revision]) | 416 | child_argv.extend(["--manifest-branch", spec.revision]) |
| 411 | result = self._Run(name, gopts, child_argv) or result | 417 | result = self._Run(name, gopts, child_argv) or result |
| 418 | |||
| 419 | def execute_command(): | ||
| 420 | """ | ||
| 421 | Execute the command and log uncaught exceptions. | ||
| 422 | """ | ||
| 423 | try: | ||
| 424 | execute_command_helper() | ||
| 425 | except (KeyboardInterrupt, SystemExit, Exception) as e: | ||
| 426 | ok = isinstance(e, SystemExit) and not e.code | ||
| 427 | if not ok: | ||
| 428 | exception_name = type(e).__name__ | ||
| 429 | git_trace2_event_log.ErrorEvent( | ||
| 430 | f"RepoExitError:{exception_name}") | ||
| 431 | raise | ||
| 432 | |||
| 433 | try: | ||
| 434 | execute_command() | ||
| 412 | except ( | 435 | except ( |
| 413 | DownloadError, | 436 | DownloadError, |
| 414 | ManifestInvalidRevisionError, | 437 | ManifestInvalidRevisionError, |
| @@ -448,6 +471,12 @@ class _Repo(object): | |||
| 448 | if e.code: | 471 | if e.code: |
| 449 | result = e.code | 472 | result = e.code |
| 450 | raise | 473 | raise |
| 474 | except KeyboardInterrupt: | ||
| 475 | result = KEYBOARD_INTERRUPT_EXIT | ||
| 476 | raise | ||
| 477 | except Exception: | ||
| 478 | result = 1 | ||
| 479 | raise | ||
| 451 | finally: | 480 | finally: |
| 452 | finish = time.time() | 481 | finish = time.time() |
| 453 | elapsed = finish - start | 482 | elapsed = finish - start |
| @@ -813,7 +842,7 @@ def _Main(argv): | |||
| 813 | result = repo._Run(name, gopts, argv) or 0 | 842 | result = repo._Run(name, gopts, argv) or 0 |
| 814 | except KeyboardInterrupt: | 843 | except KeyboardInterrupt: |
| 815 | print("aborted by user", file=sys.stderr) | 844 | print("aborted by user", file=sys.stderr) |
| 816 | result = 1 | 845 | result = KEYBOARD_INTERRUPT_EXIT |
| 817 | except ManifestParseError as mpe: | 846 | except ManifestParseError as mpe: |
| 818 | print("fatal: %s" % mpe, file=sys.stderr) | 847 | print("fatal: %s" % mpe, file=sys.stderr) |
| 819 | result = 1 | 848 | result = 1 |
