Przeglądaj źródła

move common options definition to archiver.common

Thomas Waldmann 2 lat temu
rodzic
commit
ea6b373f85
2 zmienionych plików z 139 dodań i 141 usunięć
  1. 1 140
      src/borg/archiver/__init__.py
  2. 138 1
      src/borg/archiver/common.py

+ 1 - 140
src/borg/archiver/__init__.py

@@ -24,7 +24,6 @@ try:
     from ..constants import *  # NOQA
     from ..helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR, EXIT_SIGNAL_BASE
     from ..helpers import Error, set_ec
-    from ..helpers import location_validator, Location
     from ..helpers import format_file_size
     from ..helpers import remove_surrogates
     from ..helpers import check_python, check_extension_modules
@@ -283,145 +282,7 @@ class Archiver(
                 setattr(args, dest, option_value)
 
     def build_parser(self):
-        def define_common_options(add_common_option):
-            add_common_option("-h", "--help", action="help", help="show this help message and exit")
-            add_common_option(
-                "--critical",
-                dest="log_level",
-                action="store_const",
-                const="critical",
-                default="warning",
-                help="work on log level CRITICAL",
-            )
-            add_common_option(
-                "--error",
-                dest="log_level",
-                action="store_const",
-                const="error",
-                default="warning",
-                help="work on log level ERROR",
-            )
-            add_common_option(
-                "--warning",
-                dest="log_level",
-                action="store_const",
-                const="warning",
-                default="warning",
-                help="work on log level WARNING (default)",
-            )
-            add_common_option(
-                "--info",
-                "-v",
-                "--verbose",
-                dest="log_level",
-                action="store_const",
-                const="info",
-                default="warning",
-                help="work on log level INFO",
-            )
-            add_common_option(
-                "--debug",
-                dest="log_level",
-                action="store_const",
-                const="debug",
-                default="warning",
-                help="enable debug output, work on log level DEBUG",
-            )
-            add_common_option(
-                "--debug-topic",
-                metavar="TOPIC",
-                dest="debug_topics",
-                action="append",
-                default=[],
-                help="enable TOPIC debugging (can be specified multiple times). "
-                "The logger path is borg.debug.<TOPIC> if TOPIC is not fully qualified.",
-            )
-            add_common_option(
-                "-p", "--progress", dest="progress", action="store_true", help="show progress information"
-            )
-            add_common_option("--iec", dest="iec", action="store_true", help="format using IEC units (1KiB = 1024B)")
-            add_common_option(
-                "--log-json",
-                dest="log_json",
-                action="store_true",
-                help="Output one JSON object per log line instead of formatted text.",
-            )
-            add_common_option(
-                "--lock-wait",
-                metavar="SECONDS",
-                dest="lock_wait",
-                type=int,
-                default=1,
-                help="wait at most SECONDS for acquiring a repository/cache lock (default: %(default)d).",
-            )
-            add_common_option(
-                "--bypass-lock",
-                dest="lock",
-                action="store_false",
-                default=argparse.SUPPRESS,  # only create args attribute if option is specified
-                help="Bypass locking mechanism",
-            )
-            add_common_option(
-                "--show-version", dest="show_version", action="store_true", help="show/log the borg version"
-            )
-            add_common_option("--show-rc", dest="show_rc", action="store_true", help="show/log the return code (rc)")
-            add_common_option(
-                "--umask",
-                metavar="M",
-                dest="umask",
-                type=lambda s: int(s, 8),
-                default=UMASK_DEFAULT,
-                help="set umask to M (local only, default: %(default)04o)",
-            )
-            add_common_option(
-                "--remote-path",
-                metavar="PATH",
-                dest="remote_path",
-                help='use PATH as borg executable on the remote (default: "borg")',
-            )
-            add_common_option(
-                "--upload-ratelimit",
-                metavar="RATE",
-                dest="upload_ratelimit",
-                type=int,
-                help="set network upload rate limit in kiByte/s (default: 0=unlimited)",
-            )
-            add_common_option(
-                "--upload-buffer",
-                metavar="UPLOAD_BUFFER",
-                dest="upload_buffer",
-                type=int,
-                help="set network upload buffer size in MiB. (default: 0=no buffer)",
-            )
-            add_common_option(
-                "--consider-part-files",
-                dest="consider_part_files",
-                action="store_true",
-                help="treat part files like normal files (e.g. to list/extract them)",
-            )
-            add_common_option(
-                "--debug-profile",
-                metavar="FILE",
-                dest="debug_profile",
-                default=None,
-                help="Write execution profile in Borg format into FILE. For local use a Python-"
-                'compatible file can be generated by suffixing FILE with ".pyprof".',
-            )
-            add_common_option(
-                "--rsh",
-                metavar="RSH",
-                dest="rsh",
-                help="Use this command to connect to the 'borg serve' process (default: 'ssh')",
-            )
-            add_common_option(
-                "-r",
-                "--repo",
-                metavar="REPO",
-                dest="location",
-                type=location_validator(other=False),
-                default=Location(other=False),
-                help="repository to use",
-            )
+        from .common import define_common_options
 
         parser = argparse.ArgumentParser(prog=self.prog, description="Borg - Deduplicated Backups", add_help=False)
         # paths and patterns must have an empty list as default everywhere

+ 138 - 1
src/borg/archiver/common.py

@@ -9,7 +9,7 @@ from ..constants import *  # NOQA
 from ..cache import Cache, assert_secure
 from ..helpers import Error
 from ..helpers import Manifest, AI_HUMAN_SORT_KEYS
-from ..helpers import GlobSpec, SortBySpec, positive_int_validator
+from ..helpers import GlobSpec, SortBySpec, positive_int_validator, location_validator, Location
 from ..patterns import PatternMatcher
 from ..remote import RemoteRepository
 from ..repository import Repository
@@ -424,6 +424,143 @@ def define_archive_filters_group(subparser, *, sort_by=True, first_last=True):
     return filters_group
 
 
+def define_common_options(add_common_option):
+    add_common_option("-h", "--help", action="help", help="show this help message and exit")
+    add_common_option(
+        "--critical",
+        dest="log_level",
+        action="store_const",
+        const="critical",
+        default="warning",
+        help="work on log level CRITICAL",
+    )
+    add_common_option(
+        "--error",
+        dest="log_level",
+        action="store_const",
+        const="error",
+        default="warning",
+        help="work on log level ERROR",
+    )
+    add_common_option(
+        "--warning",
+        dest="log_level",
+        action="store_const",
+        const="warning",
+        default="warning",
+        help="work on log level WARNING (default)",
+    )
+    add_common_option(
+        "--info",
+        "-v",
+        "--verbose",
+        dest="log_level",
+        action="store_const",
+        const="info",
+        default="warning",
+        help="work on log level INFO",
+    )
+    add_common_option(
+        "--debug",
+        dest="log_level",
+        action="store_const",
+        const="debug",
+        default="warning",
+        help="enable debug output, work on log level DEBUG",
+    )
+    add_common_option(
+        "--debug-topic",
+        metavar="TOPIC",
+        dest="debug_topics",
+        action="append",
+        default=[],
+        help="enable TOPIC debugging (can be specified multiple times). "
+        "The logger path is borg.debug.<TOPIC> if TOPIC is not fully qualified.",
+    )
+    add_common_option("-p", "--progress", dest="progress", action="store_true", help="show progress information")
+    add_common_option("--iec", dest="iec", action="store_true", help="format using IEC units (1KiB = 1024B)")
+    add_common_option(
+        "--log-json",
+        dest="log_json",
+        action="store_true",
+        help="Output one JSON object per log line instead of formatted text.",
+    )
+    add_common_option(
+        "--lock-wait",
+        metavar="SECONDS",
+        dest="lock_wait",
+        type=int,
+        default=1,
+        help="wait at most SECONDS for acquiring a repository/cache lock (default: %(default)d).",
+    )
+    add_common_option(
+        "--bypass-lock",
+        dest="lock",
+        action="store_false",
+        default=argparse.SUPPRESS,  # only create args attribute if option is specified
+        help="Bypass locking mechanism",
+    )
+    add_common_option("--show-version", dest="show_version", action="store_true", help="show/log the borg version")
+    add_common_option("--show-rc", dest="show_rc", action="store_true", help="show/log the return code (rc)")
+    add_common_option(
+        "--umask",
+        metavar="M",
+        dest="umask",
+        type=lambda s: int(s, 8),
+        default=UMASK_DEFAULT,
+        help="set umask to M (local only, default: %(default)04o)",
+    )
+    add_common_option(
+        "--remote-path",
+        metavar="PATH",
+        dest="remote_path",
+        help='use PATH as borg executable on the remote (default: "borg")',
+    )
+    add_common_option(
+        "--upload-ratelimit",
+        metavar="RATE",
+        dest="upload_ratelimit",
+        type=int,
+        help="set network upload rate limit in kiByte/s (default: 0=unlimited)",
+    )
+    add_common_option(
+        "--upload-buffer",
+        metavar="UPLOAD_BUFFER",
+        dest="upload_buffer",
+        type=int,
+        help="set network upload buffer size in MiB. (default: 0=no buffer)",
+    )
+    add_common_option(
+        "--consider-part-files",
+        dest="consider_part_files",
+        action="store_true",
+        help="treat part files like normal files (e.g. to list/extract them)",
+    )
+    add_common_option(
+        "--debug-profile",
+        metavar="FILE",
+        dest="debug_profile",
+        default=None,
+        help="Write execution profile in Borg format into FILE. For local use a Python-"
+        'compatible file can be generated by suffixing FILE with ".pyprof".',
+    )
+    add_common_option(
+        "--rsh",
+        metavar="RSH",
+        dest="rsh",
+        help="Use this command to connect to the 'borg serve' process (default: 'ssh')",
+    )
+    add_common_option(
+        "-r",
+        "--repo",
+        metavar="REPO",
+        dest="location",
+        type=location_validator(other=False),
+        default=Location(other=False),
+        help="repository to use",
+    )
+
+
 def build_matcher(inclexcl_patterns, include_paths):
     matcher = PatternMatcher()
     matcher.add_inclexcl(inclexcl_patterns)