Change factor out cleanup_cache() into cleanup_cache() + delete_not_changed_in() for multiple use cases.

Change make it simpler to add cache paths to cleanup_cache() using a list of tuples.
Change PEP8 .. cleanup_cache > cleanup_Cache.
This commit is contained in:
JackDandy 2016-10-03 19:31:54 +01:00
parent 6b418ddea9
commit 116e0c64db
2 changed files with 29 additions and 12 deletions

View file

@ -1466,21 +1466,39 @@ def scantree(path):
yield entry yield entry
def cleanup_Cache(): def cleanup_cache():
"""Delete cached Images older then 30 days""" """
basepath = ek.ek(os.path.join, sickbeard.CACHE_DIR, 'images') Delete old cached files
cachepaths = [ek.ek(os.path.join, basepath, 'trakt')] """
del_time = time.mktime((datetime.datetime.now() - datetime.timedelta(days=30)).timetuple()) delete_not_changed_in([ek.ek(os.path.join, sickbeard.CACHE_DIR, *x) for x in [
for c in cachepaths: ('images', 'trakt')]])
def delete_not_changed_in(paths, days=30, minutes=0):
"""
Delete files under paths not changed in n days and/or n minutes.
If a file was modified later than days/and or minutes, then don't delete it.
:param paths: List of paths to scan for files to delete
:param days: Purge files not modified in this number of days (default: 30 days)
:param minutes: Purge files not modified in this number of minutes (default: 0 minutes)
:return: tuple; number of files that qualify for deletion, number of qualifying files that failed to be deleted
"""
del_time = time.mktime((datetime.datetime.now() - datetime.timedelta(days=days, minutes=minutes)).timetuple())
errors = 0
qualified = 0
for c in paths:
try: try:
for f in scantree(c): for f in scantree(c):
if f.is_file(follow_symlinks=False) and f.stat(follow_symlinks=False).st_mtime < del_time: if f.is_file(follow_symlinks=False) and del_time > f.stat(follow_symlinks=False).st_mtime:
try: try:
ek.ek(os.remove, f.path) ek.ek(os.remove, f.path)
except: except (StandardError, Exception):
errors += 1
qualified += 1
except (StandardError, Exception):
pass pass
except: return qualified, errors
pass
def set_file_timestamp(filename, min_age=3, new_time=None): def set_file_timestamp(filename, min_age=3, new_time=None):
@ -1490,4 +1508,3 @@ def set_file_timestamp(filename, min_age=3, new_time=None):
ek.ek(os.utime, filename, new_time) ek.ek(os.utime, filename, new_time)
except (StandardError, Exception): except (StandardError, Exception):
pass pass

View file

@ -60,7 +60,7 @@ class ShowUpdater:
sickbeard.helpers.clear_unused_providers() sickbeard.helpers.clear_unused_providers()
# cleanup image cache # cleanup image cache
sickbeard.helpers.cleanup_Cache() sickbeard.helpers.cleanup_cache()
# add missing mapped ids # add missing mapped ids
if not sickbeard.background_mapping_task.is_alive(): if not sickbeard.background_mapping_task.is_alive():