commit 1b7a24dfc132e0455ca43133f907dd51452ccfa0 Author: juga0 juga@riseup.net Date: Tue Jul 17 16:57:11 2018 +0000
Add function to find old v3bw files --- sbws/core/cleanup.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
diff --git a/sbws/core/cleanup.py b/sbws/core/cleanup.py index 3c4c8f8..bd3300c 100644 --- a/sbws/core/cleanup.py +++ b/sbws/core/cleanup.py @@ -1,5 +1,6 @@ from sbws.util.filelock import DirectoryLock from sbws.globals import (fail_hard, is_initted) +from sbws.util.timestamp import unixts_to_dt_obj from argparse import ArgumentDefaultsHelpFormatter from datetime import datetime from datetime import timedelta @@ -32,6 +33,33 @@ def gen_parser(sub): help='Do not clean v3bw files')
+def _get_files_mtime_older_than(dname, days_delta, extensions): + """Return files which modification time is older than days_delta + and which extension is one of the extensions.""" + assert os.path.isdir(dname) + assert isinstance(days_delta, int) + assert isinstance(extensions, list) + for ext in extensions: + assert isinstance(ext, str) + assert ext[0] == '.' + # Determine oldest allowed date + today = datetime.utcfromtimestamp(time.time()) + oldest_day = today - timedelta(days=days_delta) + for root, dirs, files in os.walk(dname): + for f in files: + fname = os.path.join(root, f) + _, ext = os.path.splitext(fname) + if ext not in extensions: + log.debug('Ignoring %s because it doesn't have extension ' + '%s', fname, ext) + continue + # using file modification time instead of parsing the name + # of the file. + filedt = unixts_to_dt_obj( + os.stat(fname, follow_symlinks=False).st_mtime) + if filedt < oldest_day and os.path.splitext: + yield fname +
def _get_older_files_than(dname, num_days_ago, extensions): assert os.path.isdir(dname)
tor-commits@lists.torproject.org