Add return code functions (#2199)

(cherry picked from commit 4b33c3fe14)
This commit is contained in:
Abdel-Rahman 2017-03-05 14:33:42 +02:00 committed by Marian Beermann
parent 008c236aab
commit b4c0dcfbdf
3 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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"""

View File

@ -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()