~gpanders/pushbroom

b08aad872e7bf2ce5d3fc9bc022d1548d1fbae73 — Greg Anders 2 years ago 20f82e9
Add `Match` configuration option
3 files changed, 40 insertions(+), 13 deletions(-)

M README.md
M src/pushbroom/console.py
M src/pushbroom/sweep.py
M README.md => README.md +34 -9
@@ 85,26 85,51 @@ files or directories that match the given glob:
    [Downloads]
    Path = ~/Downloads
    NumDays = 30
    Ignore = folder_to_keep/**/*
    Ignore = folder_to_keep

Similarly, you can specify `Match` to have Pushbroom only remove files that
match one of the given patterns:

    [Vim Backup Directory]
    Path = ~/.cache/vim/backup
    NumDays = 90
    Match = *~

Both `Ignore` and `Match` can be a list of patterns separated by commas.

    [Home Directory]
    Path = ~
    NumDays = 365
    Match = .*
    Ignore = .local, .config, .cache, .vim

The following configuration items are recognized in `pushbroom.conf`:

**Path**
### Path
**Required**

Specify which directory to monitor
Specify which directory to monitor.

**Trash**
### Trash

Specify where to move files after deletion. If this option is not provided,
files will simply be deleted.
Specify where to move files after deletion. If omitted, files will simply be
deleted.

**NumDays**
### NumDays
**Required**

Number of days to keep files in `Path` before they are removed.

**Ignore**
### Ignore
**Default**: None

List of glob expression patterns of files or directories to ignore.

### Match
**Default**: `*`

Glob expression pattern of files or directories to ignore.
List of glob expression patterns of files or directories to remove. If omitted,
everything is removed.

## Automating


M src/pushbroom/console.py => src/pushbroom/console.py +3 -1
@@ 83,10 83,12 @@ def run():
            trash = config.get(section, "trash", fallback=None)
            ignore = config.get(section, "ignore", fallback="").split(",")
            ignored = r"|".join([fnmatch.translate(x) for x in ignore])
            match = config.get(section, "match", fallback="*").split(",")
            matched = r"|".join([fnmatch.translate(x) for x in match])

            if trash:
                trash = os.path.abspath(os.path.expanduser(trash))
                if not os.path.isdir(trash):
                    logging.error("No such directory %s", trash)

            pushbroom.sweep(section, fullpath, num_days, ignored, trash, args.dry_run)
            pushbroom.sweep(section, fullpath, num_days, ignored, matched, trash, args.dry_run)

M src/pushbroom/sweep.py => src/pushbroom/sweep.py +3 -3
@@ 6,7 6,7 @@ import time
SECONDS_PER_DAY = 24 * 60 * 60


def sweep(name, path, num_days, ignored, trash=None, dry_run=False):
def sweep(name, path, num_days, ignored, match=None, trash=None, dry_run=False):
    """Remove old files from a directory

    :path: Path to remove files from


@@ 21,9 21,9 @@ def sweep(name, path, num_days, ignored, trash=None, dry_run=False):
    num_seconds = num_days * SECONDS_PER_DAY
    thresh = now - num_seconds
    for root, dirs, files in os.walk(path):
        dirs[:] = [d for d in dirs if not re.match(ignored, d)]
        dirs[:] = [d for d in dirs if re.match(match, d) and not re.match(ignored, d)]

        files = [f for f in files if not re.match(ignored, f)]
        files = [f for f in files if re.match(match, f) and not re.match(ignored, f)]
        for file in files:
            fpath = os.path.join(root, file)
            try: