diff options
author | Calum Lind <calumlind@gmail.com> | 2018-11-01 09:06:41 +0000 |
---|---|---|
committer | Calum Lind <calumlind@gmail.com> | 2018-11-02 08:47:57 +0000 |
commit | f24e9d152c90d503b86529dc1e61ad5ce147a23d (patch) | |
tree | 81a36b5ec164a4de83ef40bdcae82264ddf60ef2 | |
parent | f47089ae7dea1b75a1a624d37bd7518355934949 (diff) | |
download | deluge-f24e9d152c90d503b86529dc1e61ad5ce147a23d.tar.gz deluge-f24e9d152c90d503b86529dc1e61ad5ce147a23d.tar.bz2 deluge-f24e9d152c90d503b86529dc1e61ad5ce147a23d.zip |
[Common] Remove oldest archive if too many exist
Prevents the archive folder bloating.
-rw-r--r-- | deluge/common.py | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/deluge/common.py b/deluge/common.py index 05636800f..a0ffebaf3 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -12,7 +12,6 @@ from __future__ import division, print_function, unicode_literals import base64 import binascii -import datetime import functools import glob import locale @@ -26,6 +25,7 @@ import sys import tarfile import time from contextlib import closing +from datetime import datetime from io import BytesIO import pkg_resources @@ -162,7 +162,7 @@ def get_default_download_dir(): return download_dir -def archive_files(arc_name, filepaths, message=None): +def archive_files(arc_name, filepaths, message=None, rotate=10): """Compress a list of filepaths into timestamped tarball in config dir. The archiving config directory is 'archive'. @@ -182,21 +182,20 @@ def archive_files(arc_name, filepaths, message=None): arc_comp = 'xz' if not PY2 else 'bz2' archive_dir = os.path.join(get_config_dir(), 'archive') - timestamp = ( - datetime.datetime.now().replace(microsecond=0).isoformat().replace(':', '-') - ) + timestamp = datetime.now().replace(microsecond=0).isoformat().replace(':', '-') arc_filepath = os.path.join( archive_dir, arc_name + '-' + timestamp + '.tar.' + arc_comp ) - max_num_arcs = 20 if not os.path.exists(archive_dir): os.makedirs(archive_dir) else: - old_arcs = glob.glob(os.path.join(archive_dir, arc_name) + '*') - if len(old_arcs) > max_num_arcs: - # TODO: Remove oldest timestamped archives. - log.warning('More than %s tarballs in config archive', max_num_arcs) + all_arcs = glob.glob(os.path.join(archive_dir, arc_name) + '*') + if len(all_arcs) >= rotate: + log.warning( + 'Too many existing archives for %s. Deleting oldest archive.', arc_name + ) + os.remove(sorted(all_arcs)[0]) try: with tarfile.open(arc_filepath, 'w:' + arc_comp) as tar: |