def test_no_auto_confirm_when_something_else(self, mock_getchar): possibilities = ("x", "0", "1", "") mock_getchar.side_effect = possibilities confirm = Confirm() for user_input in possibilities: with self.subTest(): self.assertFalse(confirm.ask(), user_input)
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: click.echo("==> 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: click.echo("==> No backup to be deleted.") return None # print the list of files to be kept click.echo(f"\n==> {len(white_list)} backups will be kept:") for date_id in white_list: date_formated = backup.target.parse_timestamp(date_id) click.echo(f"\n ID: {date_id} (from {date_formated})") for f in backup.by_timestamp(date_id): click.echo(f" {backup.target.path}{f}") # print the list of files to be deleted delete_list = list() click.echo(f"\n==> {len(black_list)} backups will be deleted:") for date_id in black_list: date_formated = backup.target.parse_timestamp(date_id) click.echo(f"\n ID: {date_id} (from {date_formated})") for f in backup.by_timestamp(date_id): click.echo(f" {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) click.echo(f" {name} deleted.") backup.close_ftp()
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 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)) click.echo("==> Do you want to delete the following files?") for name in delete_list: click.echo(f" {backup.target.path}{name}") # delete confirm = Confirm(assume_yes) if confirm.ask(): for name in delete_list: backup.target.delete_file(name) click.echo(f" {name} deleted.") backup.close_ftp()
def test_no_auto_confirm_upper(self, mock_getchar): mock_getchar.return_value = "Y" confirm = Confirm() self.assertTrue(confirm.ask())
def test_auto_confirm_without_assume_yes(self, _mock_getchar): cancel_possibilities = (False, "", 0) for value in cancel_possibilities: confirm = Confirm(value) 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_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_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())