~gpanders/pushbroom

ref: 93b0d2d2a44a729c86a563c169d747b94d6f86df pushbroom/src/pushbroom/sweep.py -rw-r--r-- 1.5 KiB
93b0d2d2Greg Anders Mild refactoring to improve pylint score 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import logging
import os
import re
import time

SECONDS_PER_DAY = 24 * 60 * 60


def sweep(name, path, num_days, ignore, match, trash, dry_run):
    # pylint: disable = too-many-arguments
    """Remove old files from a directory

    :name:     Name of the section being cleaned
    :path:     Path to remove files from
    :num_days: Remove files older than this many days
    :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

    """
    logging.info("Sweeping %s", name)
    num_seconds = num_days * SECONDS_PER_DAY
    thresh = time.time() - 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(ignore, d)]

        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)
            if not os.path.exists(fpath):
                continue

            if os.stat(fpath).st_mtime < thresh:
                if trash:
                    logging.info("Moving %s to %s", fpath, trash)
                    if not dry_run:
                        os.rename(fpath, os.path.join(trash, file))
                else:
                    logging.info("Deleting %s", fpath)
                    if not dry_run:
                        os.remove(fpath)