[Common] Remove oldest archive if too many exist

Prevents the archive folder bloating.
This commit is contained in:
Calum Lind 2018-11-01 09:06:41 +00:00
commit f24e9d152c

View file

@ -12,7 +12,6 @@ from __future__ import division, print_function, unicode_literals
import base64 import base64
import binascii import binascii
import datetime
import functools import functools
import glob import glob
import locale import locale
@ -26,6 +25,7 @@ import sys
import tarfile import tarfile
import time import time
from contextlib import closing from contextlib import closing
from datetime import datetime
from io import BytesIO from io import BytesIO
import pkg_resources import pkg_resources
@ -162,7 +162,7 @@ def get_default_download_dir():
return 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. """Compress a list of filepaths into timestamped tarball in config dir.
The archiving config directory is 'archive'. 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' arc_comp = 'xz' if not PY2 else 'bz2'
archive_dir = os.path.join(get_config_dir(), 'archive') archive_dir = os.path.join(get_config_dir(), 'archive')
timestamp = ( timestamp = datetime.now().replace(microsecond=0).isoformat().replace(':', '-')
datetime.datetime.now().replace(microsecond=0).isoformat().replace(':', '-')
)
arc_filepath = os.path.join( arc_filepath = os.path.join(
archive_dir, arc_name + '-' + timestamp + '.tar.' + arc_comp archive_dir, arc_name + '-' + timestamp + '.tar.' + arc_comp
) )
max_num_arcs = 20
if not os.path.exists(archive_dir): if not os.path.exists(archive_dir):
os.makedirs(archive_dir) os.makedirs(archive_dir)
else: else:
old_arcs = glob.glob(os.path.join(archive_dir, arc_name) + '*') all_arcs = glob.glob(os.path.join(archive_dir, arc_name) + '*')
if len(old_arcs) > max_num_arcs: if len(all_arcs) >= rotate:
# TODO: Remove oldest timestamped archives. log.warning(
log.warning('More than %s tarballs in config archive', max_num_arcs) 'Too many existing archives for %s. Deleting oldest archive.', arc_name
)
os.remove(sorted(all_arcs)[0])
try: try:
with tarfile.open(arc_filepath, 'w:' + arc_comp) as tar: with tarfile.open(arc_filepath, 'w:' + arc_comp) as tar: