Fix parsing "changed link" lines (#510)

* Fix parsing "changed link" lines

* Move borg diff parser test to test_archives.py
This commit is contained in:
Tomasz Kontusz 2020-06-16 18:21:24 +02:00 committed by GitHub
parent 936d3c10d0
commit c16a7509bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 52 deletions

View File

@ -19,58 +19,7 @@ class DiffResult(DiffResultBase, DiffResultUI):
super().__init__() super().__init__()
self.setupUi(self) self.setupUi(self)
files_with_attributes = [] files_with_attributes, nested_file_list = parse_diff_lines(fs_data.split('\n'))
nested_file_list = nested_dict()
def parse_line(line):
if line:
line_split = line.split()
else:
return 0, "", "", ""
if line_split[0] == 'added' or line_split[0] == 'removed':
change_type = line_split[0]
if line_split[1] in ['directory', 'link']:
size = 0
full_path = re.search(r'^\w+ \w+ +(.*)', line).group(1)
else:
significand = line_split[1]
unit = line_split[2]
size = calc_size(significand, unit)
full_path = re.search(r'^\w+ +\S+ \w?B (.*)', line).group(1)
else:
size_change = re.search(r' *[\+-]?(\d+\.*\d*) (\w?B) +[\+-]?.+\w?B ', line)
if size_change:
significand = size_change.group(1)
unit = size_change.group(2)
size = calc_size(significand, unit)
full_path_index = size_change.end(0)
else:
size = 0
permission_change = re.search(r' *(\[.{24}\]) ', line)
if permission_change:
change_type = permission_change.group(1)
full_path_index = permission_change.end(0)
else:
change_type = "modified"
if size_change and permission_change:
full_path_index = max(size_change.end(0), permission_change.end(0))
full_path = line[full_path_index:]
dir, name = os.path.split(full_path)
# add to nested dict of folders to find nested dirs.
d = get_dict_from_list(nested_file_list, dir.split('/'))
if name not in d:
d[name] = {}
return size, change_type, name, dir
for line in fs_data.split('\n'):
files_with_attributes.append(parse_line(line))
model = DiffTree(files_with_attributes, nested_file_list) model = DiffTree(files_with_attributes, nested_file_list)
view = self.treeView view = self.treeView
@ -88,6 +37,62 @@ class DiffResult(DiffResultBase, DiffResultUI):
self.okButton.clicked.connect(self.accept) self.okButton.clicked.connect(self.accept)
def parse_diff_lines(diff_lines):
files_with_attributes = []
nested_file_list = nested_dict()
def parse_line(line):
if line:
line_split = line.split()
else:
return 0, "", "", ""
if line_split[0] in {'added', 'removed', 'changed'}:
change_type = line_split[0]
if line_split[1] in ['directory', 'link']:
size = 0
full_path = re.search(r'^\w+ \w+ +(.*)', line).group(1)
else:
significand = line_split[1]
unit = line_split[2]
size = calc_size(significand, unit)
full_path = re.search(r'^\w+ +\S+ \w?B (.*)', line).group(1)
else:
size_change = re.search(r' *[\+-]?(\d+\.*\d*) (\w?B) +[\+-]?.+\w?B ', line)
if size_change:
significand = size_change.group(1)
unit = size_change.group(2)
size = calc_size(significand, unit)
full_path_index = size_change.end(0)
else:
size = 0
permission_change = re.search(r' *(\[.{24}\]) ', line)
if permission_change:
change_type = permission_change.group(1)
full_path_index = permission_change.end(0)
else:
change_type = "modified"
if size_change and permission_change:
full_path_index = max(size_change.end(0), permission_change.end(0))
full_path = line[full_path_index:]
dir, name = os.path.split(full_path)
# add to nested dict of folders to find nested dirs.
d = get_dict_from_list(nested_file_list, dir.split('/'))
if name not in d:
d[name] = {}
return size, change_type, name, dir
for line in diff_lines:
files_with_attributes.append(parse_line(line))
return (files_with_attributes, nested_file_list)
def calc_size(significand, unit): def calc_size(significand, unit):
if unit == 'B': if unit == 'B':
return int(significand) return int(significand)

View File

@ -1,5 +1,6 @@
import psutil import psutil
from collections import namedtuple from collections import namedtuple
import pytest
from PyQt5 import QtCore from PyQt5 import QtCore
from vorta.models import BackupProfileModel, ArchiveModel from vorta.models import BackupProfileModel, ArchiveModel
import vorta.borg import vorta.borg
@ -165,3 +166,14 @@ def test_archive_diff(qapp, qtbot, mocker, borg_json_output, monkeypatch):
assert tab._resultwindow.archiveNameLabel_1.text() == 'test-archive' assert tab._resultwindow.archiveNameLabel_1.text() == 'test-archive'
tab._resultwindow.accept() tab._resultwindow.accept()
@pytest.mark.parametrize('line, expected', [
('changed link some/changed/link',
(0, 'changed', 'link', 'some/changed')),
(' +77.8 kB -77.8 kB some/changed/file',
(77800, 'modified', 'file', 'some/changed')),
])
def test_archive_diff_parser(line, expected):
files_with_attributes, nested_file_list = vorta.views.diff_result.parse_diff_lines([line])
assert files_with_attributes == [expected]