2
0
Эх сурвалжийг харах

refactor (re-)init of exit_code and warnings_list globals

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.
Thomas Waldmann 1 жил өмнө
parent
commit
242b8f9f51

+ 24 - 17
src/borg/helpers/__init__.py

@@ -32,28 +32,24 @@ warning_info = namedtuple("warning_info", "wc,msg,args,wt")
 
 """
 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):
@@ -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):
@@ -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

+ 2 - 2
src/borg/testsuite/archiver.py

@@ -40,6 +40,7 @@ from ..crypto.keymanager import RepoIdMismatch, NotABorgKeyFile
 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
@@ -96,8 +97,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