From b4c0dcfbdfdf15d29cb4391002ce889f96252b61 Mon Sep 17 00:00:00 2001 From: Abdel-Rahman Date: Sun, 5 Mar 2017 14:33:42 +0200 Subject: [PATCH] Add return code functions (#2199) (cherry picked from commit 4b33c3fe143a1cc52ec8e566232ae03e33440a2b) --- borg/archiver.py | 4 ++-- borg/helpers.py | 20 ++++++++++++++++++++ borg/testsuite/archiver.py | 3 ++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/borg/archiver.py b/borg/archiver.py index 420cfb32f..5abf923d9 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -23,7 +23,7 @@ from .helpers import Error, location_validator, archivename_validator, format_li PathPrefixPattern, to_localtime, timestamp, safe_timestamp, bin_to_hex, get_cache_dir, prune_within, prune_split, \ Manifest, NoManifestError, remove_surrogates, format_archive, check_extension_modules, Statistics, \ dir_is_tagged, bigint_to_int, ChunkerParams, CompressionSpec, PrefixSpec, is_slow_msgpack, yes, sysinfo, \ - EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR, log_multi, PatternMatcher, ErrorIgnoringTextIOWrapper + EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR, log_multi, PatternMatcher, ErrorIgnoringTextIOWrapper, set_ec from .helpers import signal_handler, raising_signal_handler, SigHup, SigTerm from .logger import create_logger, setup_logging logger = create_logger() @@ -2075,7 +2075,7 @@ class Archiver: check_extension_modules() if is_slow_msgpack(): logger.warning("Using a pure-python msgpack! This will result in lower performance.") - return func(args) + return set_ec(func(args)) def sig_info_handler(sig_no, stack): # pragma: no cover diff --git a/borg/helpers.py b/borg/helpers.py index 388a1c536..299563945 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -45,6 +45,26 @@ EXIT_WARNING = 1 # reached normal end of operation, but there were issues EXIT_ERROR = 2 # terminated abruptly, did not reach end of operation +''' +The global exit_code variable is used so that modules other than archiver can increase the program exit code if a +warning or error occured during their operation. This is different from archiver.exit_code, which is only accessible +from the archiver object. +''' +exit_code = EXIT_SUCCESS + + +def set_ec(ec): + ''' + Sets the exit code of the program, if an exit code higher or equal than this is set, this does nothing. This + makes EXIT_ERROR override EXIT_WARNING, etc.. + + ec: exit code to set + ''' + global exit_code + exit_code = max(exit_code, ec) + return exit_code + + class Error(Exception): """Error base class""" diff --git a/borg/testsuite/archiver.py b/borg/testsuite/archiver.py index f688ba182..5e8a6a0f8 100644 --- a/borg/testsuite/archiver.py +++ b/borg/testsuite/archiver.py @@ -19,7 +19,7 @@ from hashlib import sha256 import msgpack import pytest -from .. import xattr +from .. import xattr, helpers from ..archive import Archive, ChunkBuffer, CHUNK_MAX_EXP, flags_noatime, flags_normal from ..archiver import Archiver from ..cache import Cache @@ -68,6 +68,7 @@ def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw): if archiver is None: archiver = Archiver() archiver.exit_code = EXIT_SUCCESS + helpers.exit_code = EXIT_SUCCESS args = archiver.parse_args(list(args)) ret = archiver.run(args) return ret, output.getvalue()