浏览代码

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 年之前
父节点
当前提交
b53c86cf4c
共有 2 个文件被更改,包括 27 次插入20 次删除
  1. 24 17
      src/borg/helpers/__init__.py
  2. 3 3
      src/borg/testsuite/archiver/__init__.py

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

@@ -64,28 +64,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):
@@ -128,8 +124,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):
@@ -139,18 +146,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

+ 3 - 3
src/borg/testsuite/archiver/__init__.py

@@ -15,7 +15,7 @@ from io import BytesIO, StringIO
 
 import pytest
 
-from ... import xattr, helpers, platform
+from ... import xattr, platform
 from ...archive import Archive
 from ...archiver import Archiver, PURE_PYTHON_MSGPACK_WARNING
 from ...cache import Cache
@@ -23,6 +23,7 @@ from ...constants import *  # NOQA
 from ...helpers import Location, umount
 from ...helpers import EXIT_SUCCESS
 from ...helpers import bin_to_hex
+from ...helpers import init_ec_warnings
 from ...logger import flush_logging
 from ...manifest import Manifest
 from ...platform import get_flags
@@ -76,8 +77,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