def _get_last_backup(self): """ Get the last backup path Return None if not available :returns: string """ path = os.path.join(self.destination.path, self.name) self.destination.check_availability() if self.destination.is_local(): if not os.path.isdir(path): return None #filenames = [os.path.join(path, el) for el in os.listdir(path)] filenames = os.listdir(path) elif self.destination.is_ssh(): #First, create at least the target if does not exists command = ['ssh', '-t', self.destination.login, 'mkdir', '-p', path] self.logger.debug('SSH mkdir command: ' + str(command)) process = subprocess.Popen(command, bufsize=4096, stdout=subprocess.PIPE) stdout, stderr = process.communicate() self.logger.debug('SSH mkdir result: ' + stdout.decode()) command = ['ssh', '-t', self.destination.login, 'ls', '-1', path] self.logger.debug('SSH ls command: ' + str(command)) process = subprocess.Popen(command, bufsize=4096, stdout=subprocess.PIPE) stdout, stderr = process.communicate() filenames = stdout.decode() filenames = filenames.split('\n') filenames = [x.strip('\r') for x in filenames if x != ''] last = utils.get_last_file(filenames) if last is not None: last = os.path.join(path, last) self.logger.debug('_get_last_backup returns: %s', last) return last
def test_list_date(self): file_list = [] now = datetime.datetime.now() for day in range(0,20): date = now - datetime.timedelta(days=day) filename = date.strftime("%Y-%m-%d_%Hh%Mm%Ss") file_list.append(filename) expected = now.strftime("%Y-%m-%d_%Hh%Mm%Ss") result = get_last_file(file_list) self.assertEqual(result, expected)
def test_list_poison(self): file_list = ['poison'] expected = None result = get_last_file(file_list) self.assertEqual(result, expected)
def test_list_empty(self): file_list = [] expected = None result = get_last_file(file_list) self.assertEqual(result, expected)