def autoclean(assume_yes=False): """ Remove a series of backup files based on the following rules: * Keeps all the backups from the last 7 days * Keeps the most recent backup from each week of the last month * Keeps the most recent backup from each month of the last year * Keeps the most recent backup from each year of the remaining years """ # check if there are backups backup = Backup() backup.files = tuple(backup.target.get_files()) if not backup.files: print('==> No backups found.') return None # get black and white list cleaning = BackupAutoClean(backup.get_timestamps()) white_list = cleaning.white_list black_list = cleaning.black_list if not black_list: print('==> No backup to be deleted.') return None # print the list of files to be kept print('\n==> {} backups will be kept:'.format(len(white_list))) for date_id in white_list: date_formated = backup.target.parse_timestamp(date_id) print('\n ID: {} (from {})'.format(date_id, date_formated)) for f in backup.by_timestamp(date_id): print(' {}{}'.format(backup.target.path, f)) # print the list of files to be deleted delete_list = list() print('\n==> {} backups will be deleted:'.format(len(black_list))) for date_id in black_list: date_formated = backup.target.parse_timestamp(date_id) print('\n ID: {} (from {})'.format(date_id, date_formated)) for f in backup.by_timestamp(date_id): print(' {}{}'.format(backup.target.path, f)) delete_list.append(f) # delete confirm = Confirm(assume_yes) if confirm.ask(): for name in delete_list: backup.target.delete_file(name) print(' {} deleted.'.format(name)) backup.close_ftp()
def remove(date_id, assume_yes=False): """Remove a series of backup files based on the date part of the files""" # check if date/id is valid backup = Backup() if backup.valid(date_id): # List files to be deleted delete_list = tuple(backup.by_timestamp(date_id)) print('==> Do you want to delete the following files?') for name in delete_list: print(' {}{}'.format(backup.target.path, name)) # delete confirm = Confirm(assume_yes) if confirm.ask(): for name in delete_list: backup.target.delete_file(name) print(' {} deleted.'.format(name)) backup.close_ftp()
def test_auto_confirm_without_assume_yes(self): cancel_possibilities = (False, '', 0) for value in cancel_possibilities: confirm = Confirm(value) confirm.input = MagicMock() self.assertFalse(confirm.ask(), value)
def test_auto_confirm_with_assume_yes(self): ok_possibilities = (True, 'Y', 1) for value in ok_possibilities: confirm = Confirm(value) self.assertTrue(confirm.ask(), value)
def test_no_auto_confirm_when_something_else(self): possibilities = ('x', 0, 1, '', None, False) confirm = Confirm() confirm.input = MagicMock(side_effect=possibilities) for user_input in possibilities: self.assertFalse(confirm.ask(), user_input)
def test_no_auto_confirm_lower(self): confirm = Confirm() confirm.input = MagicMock(return_value='y') self.assertTrue(confirm.ask())