소스 검색

archiver: define_exclusion_group to avoid repetition

Marian Beermann 8 년 전
부모
커밋
0fabefdb59
1개의 변경된 파일39개의 추가작업 그리고 115개의 파일을 삭제
  1. 39 115
      src/borg/archiver.py

+ 39 - 115
src/borg/archiver.py

@@ -2333,6 +2333,39 @@ class Archiver:
                               help='Write execution profile in Borg format into FILE. For local use a Python-'
                                    'compatible file can be generated by suffixing FILE with ".pyprof".')
 
+        def define_exclude_and_patterns(add_option, *, tag_files=False, strip_components=False):
+            add_option('-e', '--exclude', metavar='PATTERN', dest='patterns',
+                       type=parse_exclude_pattern, action='append',
+                       help='exclude paths matching PATTERN')
+            add_option('--exclude-from', metavar='EXCLUDEFILE', action=ArgparseExcludeFileAction,
+                       help='read exclude patterns from EXCLUDEFILE, one per line')
+            add_option('--pattern', metavar='PATTERN', action=ArgparsePatternAction,
+                       help='experimental: include/exclude paths matching PATTERN')
+            add_option('--patterns-from', metavar='PATTERNFILE', action=ArgparsePatternFileAction,
+                       help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
+
+            if tag_files:
+                add_option('--exclude-caches', dest='exclude_caches', action='store_true',
+                           help='exclude directories that contain a CACHEDIR.TAG file '
+                                '(http://www.brynosaurus.com/cachedir/spec.html)')
+                add_option('--exclude-if-present', metavar='NAME', dest='exclude_if_present',
+                           action='append', type=str,
+                           help='exclude directories that are tagged by containing a filesystem object with '
+                                'the given NAME')
+                add_option('--keep-exclude-tags', '--keep-tag-files', dest='keep_exclude_tags',
+                           action='store_true',
+                           help='if tag objects are specified with ``--exclude-if-present``, '
+                                'don\'t omit the tag objects themselves from the backup archive')
+
+            if strip_components:
+                add_option('--strip-components', metavar='NUMBER', dest='strip_components', type=int, default=0,
+                           help='Remove the specified number of leading path elements. '
+                                'Paths with fewer elements will be silently skipped.')
+
+        def define_exclusion_group(subparser, **kwargs):
+            exclude_group = subparser.add_argument_group('Exclusion options')
+            define_exclude_and_patterns(exclude_group.add_argument, **kwargs)
+
         parser = argparse.ArgumentParser(prog=self.prog, description='Borg - Deduplicated Backups',
                                          add_help=False)
         parser.common_options = self.CommonOptions(define_common_options,
@@ -2795,26 +2828,7 @@ class Archiver:
         subparser.add_argument('--no-cache-sync', dest='no_cache_sync', action='store_true',
                                help='experimental: do not synchronize the cache. Implies ``--no-files-cache``.')
 
-        exclude_group = subparser.add_argument_group('Exclusion options')
-        exclude_group.add_argument('-e', '--exclude', metavar='PATTERN', dest='patterns',
-                                   type=parse_exclude_pattern, action='append', help='exclude paths matching PATTERN')
-        exclude_group.add_argument('--exclude-from', metavar='EXCLUDEFILE', action=ArgparseExcludeFileAction,
-                                   help='read exclude patterns from EXCLUDEFILE, one per line')
-        exclude_group.add_argument('--exclude-caches', dest='exclude_caches', action='store_true',
-                                   help='exclude directories that contain a CACHEDIR.TAG file ('
-                                        'http://www.brynosaurus.com/cachedir/spec.html)')
-        exclude_group.add_argument('--exclude-if-present', dest='exclude_if_present', metavar='NAME',
-                                   action='append', type=str,
-                                   help='exclude directories that are tagged by containing a filesystem object with '
-                                        'the given NAME')
-        exclude_group.add_argument('--keep-exclude-tags', '--keep-tag-files', dest='keep_exclude_tags',
-                                   action='store_true',
-                                   help='if tag objects are specified with ``--exclude-if-present``, '
-                                        'don\'t omit the tag objects themselves from the backup archive')
-        exclude_group.add_argument('--pattern', metavar='PATTERN', action=ArgparsePatternAction,
-                                   help='experimental: include/exclude paths matching PATTERN')
-        exclude_group.add_argument('--patterns-from', metavar='PATTERNFILE', action=ArgparsePatternFileAction,
-                                   help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
+        define_exclusion_group(subparser, tag_files=True)
 
         fs_group = subparser.add_argument_group('Filesystem options')
         fs_group.add_argument('-x', '--one-file-system', dest='one_file_system', action='store_true',
@@ -2887,21 +2901,8 @@ class Archiver:
                                help='output verbose list of items (files, dirs, ...)')
         subparser.add_argument('-n', '--dry-run', dest='dry_run', action='store_true',
                                help='do not actually change any files')
-        subparser.add_argument('-e', '--exclude', metavar='PATTERN', dest='patterns',
-                               type=parse_exclude_pattern, action='append',
-                               help='exclude paths matching PATTERN')
-        subparser.add_argument('--exclude-from', metavar='EXCLUDEFILE', action=ArgparseExcludeFileAction,
-                               help='read exclude patterns from EXCLUDEFILE, one per line')
-        subparser.add_argument('--pattern', metavar='PATTERN', action=ArgparsePatternAction,
-                               help='experimental: include/exclude paths matching PATTERN')
-        subparser.add_argument('--patterns-from', metavar='PATTERNFILE', action=ArgparsePatternFileAction,
-                               help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
         subparser.add_argument('--numeric-owner', dest='numeric_owner', action='store_true',
                                help='only obey numeric user and group identifiers')
-        subparser.add_argument('--strip-components', metavar='NUMBER', dest='strip_components',
-                               type=int, default=0,
-                               help='Remove the specified number of leading path elements. Pathnames with fewer '
-                                    'elements will be silently skipped.')
         subparser.add_argument('--stdout', dest='stdout', action='store_true',
                                help='write all extracted data to stdout')
         subparser.add_argument('--sparse', dest='sparse', action='store_true',
@@ -2911,6 +2912,7 @@ class Archiver:
                                help='archive to extract')
         subparser.add_argument('paths', metavar='PATH', nargs='*', type=str,
                                help='paths to extract; patterns are supported')
+        define_exclusion_group(subparser, strip_components=True)
 
         export_tar_epilog = process_epilog("""
         This command creates a tarball from an archive.
@@ -2957,18 +2959,6 @@ class Archiver:
                                help='filter program to pipe data through')
         subparser.add_argument('--list', dest='output_list', action='store_true',
                                help='output verbose list of items (files, dirs, ...)')
-        subparser.add_argument('-e', '--exclude', dest='patterns',
-                               type=parse_exclude_pattern, action='append',
-                               metavar="PATTERN", help='exclude paths matching PATTERN')
-        subparser.add_argument('--exclude-from', action=ArgparseExcludeFileAction,
-                               metavar='EXCLUDEFILE', help='read exclude patterns from EXCLUDEFILE, one per line')
-        subparser.add_argument('--pattern', action=ArgparsePatternAction,
-                               metavar="PATTERN", help='experimental: include/exclude paths matching PATTERN')
-        subparser.add_argument('--patterns-from', action=ArgparsePatternFileAction,
-                               metavar='PATTERNFILE', help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
-        subparser.add_argument('--strip-components', dest='strip_components',
-                               type=int, default=0, metavar='NUMBER',
-                               help='Remove the specified number of leading path elements. Pathnames with fewer elements will be silently skipped.')
         subparser.add_argument('location', metavar='ARCHIVE',
                                type=location_validator(archive=True),
                                help='archive to export')
@@ -2976,6 +2966,7 @@ class Archiver:
                                help='output tar file. "-" to write to stdout instead.')
         subparser.add_argument('paths', metavar='PATH', nargs='*', type=str,
                                help='paths to extract; patterns are supported')
+        define_exclusion_group(subparser, strip_components=True)
 
         diff_epilog = process_epilog("""
             This command finds differences (file contents, user/group/mode) between archives.
@@ -3015,30 +3006,7 @@ class Archiver:
                                help='ARCHIVE2 name (no repository location allowed)')
         subparser.add_argument('paths', metavar='PATH', nargs='*', type=str,
                                help='paths of items inside the archives to compare; patterns are supported')
-
-        exclude_group = subparser.add_argument_group('Exclusion options')
-        exclude_group.add_argument('-e', '--exclude', dest='patterns',
-                                   type=parse_exclude_pattern, action='append',
-                                   metavar="PATTERN", help='exclude paths matching PATTERN')
-        exclude_group.add_argument('--exclude-from', action=ArgparseExcludeFileAction,
-                                   metavar='EXCLUDEFILE', help='read exclude patterns from EXCLUDEFILE, one per line')
-        exclude_group.add_argument('--exclude-caches', dest='exclude_caches',
-                                   action='store_true',
-                                   help='exclude directories that contain a CACHEDIR.TAG file ('
-                                        'http://www.brynosaurus.com/cachedir/spec.html)')
-        exclude_group.add_argument('--exclude-if-present', dest='exclude_if_present',
-                                   metavar='NAME', action='append', type=str,
-                                   help='exclude directories that are tagged by containing a filesystem object with '
-                                        'the given NAME')
-        exclude_group.add_argument('--keep-exclude-tags', '--keep-tag-files', dest='keep_exclude_tags',
-                                   action='store_true',
-                                   help='if tag objects are specified with ``--exclude-if-present``, '
-                                        'don\'t omit the tag objects themselves from the backup archive')
-        exclude_group.add_argument('--pattern',
-                                   action=ArgparsePatternAction,
-                                   metavar="PATTERN", help='experimental: include/exclude paths matching PATTERN')
-        exclude_group.add_argument('--patterns-from', action=ArgparsePatternFileAction,
-                                   metavar='PATTERNFILE', help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
+        define_exclusion_group(subparser, tag_files=True)
 
         rename_epilog = process_epilog("""
         This command renames an archive in the repository.
@@ -3132,29 +3100,7 @@ class Archiver:
         subparser.add_argument('paths', metavar='PATH', nargs='*', type=str,
                                help='paths to list; patterns are supported')
         self.add_archives_filters_args(subparser)
-
-        exclude_group = subparser.add_argument_group('Exclusion options')
-        exclude_group.add_argument('-e', '--exclude', dest='patterns',
-                                   type=parse_exclude_pattern, action='append',
-                                   metavar="PATTERN", help='exclude paths matching PATTERN')
-        exclude_group.add_argument('--exclude-from', action=ArgparseExcludeFileAction,
-                                   metavar='EXCLUDEFILE', help='read exclude patterns from EXCLUDEFILE, one per line')
-        exclude_group.add_argument('--exclude-caches', dest='exclude_caches', action='store_true',
-                                   help='exclude directories that contain a CACHEDIR.TAG file ('
-                                        'http://www.brynosaurus.com/cachedir/spec.html)')
-        exclude_group.add_argument('--exclude-if-present', dest='exclude_if_present',
-                                   metavar='NAME', action='append', type=str,
-                                   help='exclude directories that are tagged by containing a filesystem object with '
-                                        'the given NAME')
-        exclude_group.add_argument('--keep-exclude-tags', '--keep-tag-files', dest='keep_exclude_tags',
-                                   action='store_true',
-                                   help='if tag objects are specified with ``--exclude-if-present``, don\'t omit the tag '
-                                        'objects themselves from the backup archive')
-        exclude_group.add_argument('--pattern',
-                                   action=ArgparsePatternAction,
-                                   metavar="PATTERN", help='experimental: include/exclude paths matching PATTERN')
-        exclude_group.add_argument('--patterns-from', action=ArgparsePatternFileAction,
-                                   metavar='PATTERNFILE', help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
+        define_exclusion_group(subparser, tag_files=True)
 
         mount_epilog = process_epilog("""
         This command mounts an archive as a FUSE filesystem. This can be useful for
@@ -3495,29 +3441,7 @@ class Archiver:
         subparser.add_argument('-s', '--stats', dest='stats', action='store_true',
                                help='print statistics at end')
 
-        exclude_group = subparser.add_argument_group('Exclusion options')
-        exclude_group.add_argument('-e', '--exclude', dest='patterns',
-                                   type=parse_exclude_pattern, action='append',
-                                   metavar="PATTERN", help='exclude paths matching PATTERN')
-        exclude_group.add_argument('--exclude-from', action=ArgparseExcludeFileAction,
-                                   metavar='EXCLUDEFILE', help='read exclude patterns from EXCLUDEFILE, one per line')
-        exclude_group.add_argument('--exclude-caches', dest='exclude_caches',
-                                   action='store_true',
-                                   help='exclude directories that contain a CACHEDIR.TAG file ('
-                                        'http://www.brynosaurus.com/cachedir/spec.html)')
-        exclude_group.add_argument('--exclude-if-present', dest='exclude_if_present',
-                                   metavar='NAME', action='append', type=str,
-                                   help='exclude directories that are tagged by containing a filesystem object with '
-                                        'the given NAME')
-        exclude_group.add_argument('--keep-exclude-tags', '--keep-tag-files', dest='keep_exclude_tags',
-                                   action='store_true',
-                                   help='if tag objects are specified with ``--exclude-if-present``, don\'t omit the tag '
-                                        'objects themselves from the backup archive')
-        exclude_group.add_argument('--pattern',
-                                   action=ArgparsePatternAction,
-                                   metavar="PATTERN", help='experimental: include/exclude paths matching PATTERN')
-        exclude_group.add_argument('--patterns-from', action=ArgparsePatternFileAction,
-                                   metavar='PATTERNFILE', help='experimental: read include/exclude patterns from PATTERNFILE, one per line')
+        define_exclusion_group(subparser, tag_files=True)
 
         archive_group = subparser.add_argument_group('Archive options')
         archive_group.add_argument('--target', dest='target', metavar='TARGET', default=None,