~gpanders/pushbroom

3aa09f9e15fe64d43b305e9c47f76497ec02ef37 — Greg Anders 2 years ago 18b9819
Remove optional arguments from sweep()
2 files changed, 12 insertions(+), 9 deletions(-)

M src/pushbroom/console.py
M src/pushbroom/sweep.py
M src/pushbroom/console.py => src/pushbroom/console.py +7 -5
@@ 3,9 3,10 @@ import configparser
import fnmatch
import logging
import os
import re
import sys

import pushbroom
from pushbroom import sweep, __version__


def run():


@@ 17,7 18,7 @@ def run():
        "-V",
        "--version",
        action="version",
        version="%(prog)s {version}".format(version=pushbroom.__version__),
        version="%(prog)s {version}".format(version=__version__),
    )
    parser.add_argument(
        "-n",


@@ 82,13 83,14 @@ def run():
            num_days = config.getint(section, "numdays")
            trash = config.get(section, "trash", fallback=None)
            ignore = config.get(section, "ignore", fallback="").split(",")
            ignored = r"|".join([fnmatch.translate(x) for x in ignore])
            ignore_re = re.compile("|".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])
            match_re = re.compile("|".join([fnmatch.translate(x) for x in match]))
            dry_run = args.dry_run

            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, matched, trash, args.dry_run)
            sweep(section, fullpath, num_days, ignore_re, match_re, trash, dry_run)

M src/pushbroom/sweep.py => src/pushbroom/sweep.py +5 -4
@@ 6,12 6,13 @@ import time
SECONDS_PER_DAY = 24 * 60 * 60


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

    :path: Path to remove files from
    :num_days: Remove files older than this many days
    :ignored: Glob pattern of paths to ignore
    :ignore: Regular expression pattern of paths to ignore
    :match: Regular expression pattern of paths to remove
    :trash: If set, move files to this directory instead of deleting them
    :dry_run: Only show what would happen without actually doing anything



@@ 21,9 22,9 @@ def sweep(name, path, num_days, ignored, match=None, 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 re.match(match, d) and not re.match(ignored, d)]
        dirs[:] = [d for d in dirs if re.match(match, d) and not re.match(ignore, d)]

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