~gpanders/pushbroom

ref: f36b82a79e05427ad05cf34bdf041ea730f6ab79 pushbroom/src/janitor/sweep.py -rw-r--r-- 1.3 KiB
f36b82a7Greg Anders Start conversion of janitor into a python package 1 year, 8 months 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(path, num_days, ignored, 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: Regex pattern of paths to ignore
    :trash: If set, move files to this directory instead of deleting them

    """
    now = time.time()
    logging.info("Starting janitor")
    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)]

        files = [f for f in files if not re.match(ignored, f)]
        for file in files:
            fpath = os.path.join(root, file)
            if os.stat(fpath).st_mtime < thresh:
                if trash:
                    if dry_run:
                        print("Moving {} to {}".format(file, trash))
                    else:
                        logging.info("Moving %s to %s", file, trash)
                        os.rename(fpath, os.path.join(trash, file))
                else:
                    if dry_run:
                        print("Deleting {}".format(file))
                    else:
                        logging.info("Deleting %s", file)
                        os.remove(fpath)