Skip to content

Commit

Permalink
Add return code functions (borgbackup#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abogical authored and enkore committed Mar 5, 2017
1 parent 89114d4 commit 4b33c3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/borg/archiver.py
Expand Up @@ -36,7 +36,7 @@
from .constants import * # NOQA
from .crc32 import crc32
from .helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR
from .helpers import Error, NoManifestError
from .helpers import Error, NoManifestError, set_ec
from .helpers import location_validator, archivename_validator, ChunkerParams, CompressionSpec
from .helpers import PrefixSpec, SortBySpec, HUMAN_SORT_KEYS
from .helpers import BaseFormatter, ItemFormatter, ArchiveFormatter
Expand Down Expand Up @@ -3372,7 +3372,7 @@ def run(self, args):
self.prerun_checks(logger)
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
Expand Down
20 changes: 20 additions & 0 deletions src/borg/helpers.py
Expand Up @@ -52,6 +52,26 @@ def Chunk(data, **meta):
return _Chunk(meta, data)


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

Expand Down
1 change: 1 addition & 0 deletions src/borg/testsuite/archiver.py
Expand Up @@ -79,6 +79,7 @@ def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw):
archiver = Archiver()
archiver.prerun_checks = lambda *args: None
archiver.exit_code = EXIT_SUCCESS
helpers.exit_code = EXIT_SUCCESS
try:
args = archiver.parse_args(list(args))
# argparse parsing may raise SystemExit when the command line is bad or
Expand Down

0 comments on commit 4b33c3f

Please sign in to comment.