From 848375e2fe35ecddfcdfd7bb30811fff87b549da Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Mon, 18 Jan 2016 13:09:08 +0100 Subject: [PATCH] Add and document path prefix as pattern style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The “extract” command supports extracting all files underneath a given set of prefix paths. The forthcoming support for extracting files using a pattern (i.e. only files ending in “.zip”) requires the introduction of path prefixes as a third pattern style, making it also available for exclusions. --- borg/archiver.py | 16 ++++++++++------ borg/helpers.py | 1 + borg/testsuite/helpers.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/borg/archiver.py b/borg/archiver.py index 57d311bcf..66298fe07 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -611,12 +611,12 @@ class Archiver: helptext = {} helptext['patterns'] = textwrap.dedent(''' - Exclusion patterns support two separate styles, fnmatch and regular - expressions. If followed by a colon (':') the first two characters of - a pattern are used as a style selector. Explicit style selection is necessary - when regular expressions are desired or when the desired fnmatch pattern - starts with two alphanumeric characters followed by a colon (i.e. - `aa:something/*`). + Exclusion patterns support three separate styles, fnmatch, regular + expressions and path prefixes. If followed by a colon (':') the first two + characters of a pattern are used as a style selector. Explicit style + selection is necessary when a non-default style is desired or when the + desired pattern starts with two alphanumeric characters followed by a colon + (i.e. `aa:something/*`). `Fnmatch `_ patterns use a variant of shell pattern syntax, with '*' matching any number of @@ -640,6 +640,10 @@ class Archiver: documentation for the re module `_. + Prefix path patterns can be selected with the prefix `pp:`. This pattern + style is useful to match whole sub-directories. The pattern `pp:/data/bar` + matches `/data/bar` and everything therein. + Exclusions can be passed via the command line option `--exclude`. When used from within a shell the patterns should be quoted to protect them from expansion. diff --git a/borg/helpers.py b/borg/helpers.py index adb75fb4b..8c1bb594b 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -412,6 +412,7 @@ class RegexPattern(PatternBase): _PATTERN_STYLES = set([ FnmatchPattern, + PathPrefixPattern, RegexPattern, ]) diff --git a/borg/testsuite/helpers.py b/borg/testsuite/helpers.py index 22bfe903e..f31bd9840 100644 --- a/borg/testsuite/helpers.py +++ b/borg/testsuite/helpers.py @@ -324,6 +324,10 @@ def test_invalid_unicode_pattern(pattern): ["/more/data"]), ([r" re:^\s "], ["/data/something00.txt", "/more/data", "/home", "/whitespace/end\t"]), ([r" re:\s$ "], ["/data/something00.txt", "/more/data", "/home", " #/wsfoobar", "\tstart/whitespace"]), + (["pp:./"], None), + (["pp:/"], [" #/wsfoobar", "\tstart/whitespace"]), + (["pp:aaabbb"], None), + (["pp:/data", "pp: #/", "pp:\tstart", "pp:/whitespace"], ["/more/data", "/home"]), ]) def test_patterns_from_file(tmpdir, lines, expected): files = [ @@ -364,6 +368,12 @@ def test_patterns_from_file(tmpdir, lines, expected): ("re:.*", RegexPattern), ("re:^/something/", RegexPattern), ("re:re:^/something/", RegexPattern), + + # Path prefix + ("pp:", PathPrefixPattern), + ("pp:/", PathPrefixPattern), + ("pp:/data/", PathPrefixPattern), + ("pp:pp:/data/", PathPrefixPattern), ]) def test_parse_pattern(pattern, cls): assert isinstance(parse_pattern(pattern), cls)