~gpanders/pushbroom

ref: 18b9819999dfd82820b4c6c803d668ea76693276 pushbroom/src/pushbroom/sweep.py -rw-r--r-- 1.4 KiB
18b98199Greg Anders Install pytest and create unit test outline 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
import logging
import os
import re
import time

SECONDS_PER_DAY = 24 * 60 * 60


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
    :num_days: Remove files older than this many days
    :ignored: Glob pattern of paths to ignore
    :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)
    now = time.time()
    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)]

        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:
                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)
            except FileNotFoundError:
                pass