Skip to content

Commit

Permalink
refactor (re-)init of exit_code and warnings_list globals
Browse files Browse the repository at this point in the history
stop directly accessing the variables from other modules.

prefix with underscore to indicate that these shall
only be used within this module and every other user
shall call the respective functions.
  • Loading branch information
ThomasWaldmann committed Dec 15, 2023
1 parent 330b350 commit 38cb364
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
41 changes: 24 additions & 17 deletions src/borg/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,24 @@

"""
The global warnings_list variable is used to collect warning_info elements while borg is running.
Note: keep this in helpers/__init__.py as the code expects to be able to assign to helpers.warnings_list.
"""
warnings_list = []
_warnings_list = []


def add_warning(msg, *args, **kwargs):
global warnings_list
global _warnings_list
warning_code = kwargs.get("wc", EXIT_WARNING)
assert isinstance(warning_code, int)
warning_type = kwargs.get("wt", "percent")
assert warning_type in ("percent", "curly")
warnings_list.append(warning_info(warning_code, msg, args, warning_type))
_warnings_list.append(warning_info(warning_code, msg, args, warning_type))


"""
The global exit_code variable is used so that modules other than archiver can increase the program exit code if a
warning or error occurred during their operation.
Note: keep this in helpers/__init__.py as the code expects to be able to assign to helpers.exit_code.
"""
exit_code = EXIT_SUCCESS
_exit_code = EXIT_SUCCESS


def classify_ec(ec):
Expand Down Expand Up @@ -96,8 +92,19 @@ def set_ec(ec):
"""
Sets the exit code of the program to ec IF ec is more severe than the current exit code.
"""
global exit_code
exit_code = max_ec(exit_code, ec)
global _exit_code
_exit_code = max_ec(_exit_code, ec)


def init_ec_warnings(ec=EXIT_SUCCESS, warnings=None):
"""
(Re-)Init the globals for the exit code and the warnings list.
"""
global _exit_code, _warnings_list
_exit_code = ec
warnings = [] if warnings is None else warnings
assert isinstance(warnings, list)
_warnings_list = warnings


def get_ec(ec=None):
Expand All @@ -107,18 +114,18 @@ def get_ec(ec=None):
if ec is not None:
set_ec(ec)

global exit_code
exit_code_class = classify_ec(exit_code)
global _exit_code
exit_code_class = classify_ec(_exit_code)
if exit_code_class in ("signal", "error", "warning"):
# there was a signal/error/warning, return its exit code
return exit_code
return _exit_code
assert exit_code_class == "success"
global warnings_list
if not warnings_list:
global _warnings_list
if not _warnings_list:
# we do not have any warnings in warnings list, return success exit code
return exit_code
return _exit_code
# looks like we have some warning(s)
rcs = sorted(set(w_info.wc for w_info in warnings_list))
rcs = sorted(set(w_info.wc for w_info in _warnings_list))
logger.debug(f"rcs: {rcs!r}")
if len(rcs) == 1:
# easy: there was only one kind of warning, so we can be specific
Expand Down
4 changes: 2 additions & 2 deletions src/borg/testsuite/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from ..crypto.file_integrity import FileIntegrityError
from ..helpers import Location, get_security_dir
from ..helpers import Manifest, MandatoryFeatureUnsupported, ArchiveInfo
from ..helpers import init_ec_warnings
from ..helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR, Error, CancelledByUser, RTError, CommandError
from ..helpers import bin_to_hex
from ..helpers import MAX_S
Expand Down Expand Up @@ -97,8 +98,7 @@ def exec_cmd(*args, archiver=None, fork=False, exe=None, input=b'', binary_outpu
if archiver is None:
archiver = Archiver()
archiver.prerun_checks = lambda *args: None
helpers.exit_code = EXIT_SUCCESS
helpers.warnings_list = []
init_ec_warnings()
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 38cb364

Please sign in to comment.