Skip to content

Commit 5dd2ab5

Browse files
authored
Merge pull request #21 from nexB/16-simplify-deltacode-output
16 simplify DeltaCode output
2 parents 61af30e + 5a42628 commit 5dd2ab5

8 files changed

Lines changed: 118 additions & 200 deletions

File tree

src/deltacode/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def determine_delta(self):
9090
for path, old_files in old_index.items():
9191
for old_file in old_files:
9292
old_files_to_visit -= 1
93-
93+
9494
if old_file.type != 'file':
9595
continue
9696

@@ -100,7 +100,7 @@ def determine_delta(self):
100100
except KeyError:
101101
deltas['removed'].append(Delta(None, old_file, 'removed'))
102102
continue
103-
103+
104104
# make sure everything is accounted for
105105
assert new_files_to_visit == 0
106106
assert old_files_to_visit == 0
@@ -130,7 +130,7 @@ def to_dict(self):
130130
"""
131131
if self.deltas == None:
132132
return
133-
133+
134134
return OrderedDict([
135135
('added', [d.to_dict() for d in self.deltas.get('added')]),
136136
('removed', [d.to_dict() for d in self.deltas.get('removed')]),
@@ -155,18 +155,23 @@ def to_dict(self):
155155
if self.new_file == None and self.old_file == None:
156156
return
157157

158-
if self.new_file == None:
158+
if self.category == 'added':
159+
return OrderedDict([
160+
('category', 'added'),
161+
('path', self.new_file.path)
162+
])
163+
elif self.category == 'removed':
159164
return OrderedDict([
160-
('new', None),
161-
('old', self.old_file.to_dict()),
165+
('category', 'removed'),
166+
('path', self.old_file.path)
162167
])
163-
elif self.old_file == None:
168+
elif self.category == 'modified':
164169
return OrderedDict([
165-
('new', self.new_file.to_dict()),
166-
('old', None),
170+
('category', 'modified'),
171+
('path', self.old_file.path)
167172
])
168173
else:
169174
return OrderedDict([
170-
('new', self.new_file.to_dict()),
171-
('old', self.old_file.to_dict()),
175+
('category', 'unmodified'),
176+
('path', self.old_file.path)
172177
])

src/deltacode/cli.py

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,42 @@
33
#
44
from __future__ import absolute_import
55

6+
from collections import OrderedDict
7+
68
import csv
79
import json
810

911
import click
1012

1113
from deltacode import DeltaCode
14+
from deltacode import __version__
1215

1316

14-
def generate_csv(data, result_file):
17+
def generate_csv(delta, result_file):
1518
"""
16-
Using the OrderedDict generated by DeltaCode.to_dict(), create a .csv file
19+
Using the DeltaCode object, create a .csv file
1720
containing the primary information from the Delta objects.
1821
"""
19-
category, new, new_filename, new_sha1, new_size, new_type, new_orig, old,\
20-
old_filename, old_sha1, old_size, old_type, old_orig = '', '', '', '',\
21-
'', '', '', '', '', '', '', '', ''
22-
tuple = ()
23-
tuple_list = []
24-
deltas = data
25-
26-
for delta in deltas:
27-
category = delta
28-
for f in deltas[delta]:
29-
new = '' if delta == 'removed' else f['new']['path']
30-
new_filename = '' if delta == 'removed' else f['new']['name']
31-
new_sha1 = '' if delta == 'removed' else f['new']['sha1']
32-
new_size = '' if delta == 'removed' else f['new']['size']
33-
new_type = '' if delta == 'removed' else f['new']['type']
34-
new_orig = '' if delta == 'removed' else f['new']['original_path']
35-
old = '' if delta == 'added' else f['old']['path']
36-
old_filename = '' if delta == 'added' else f['old']['name']
37-
old_sha1 = '' if delta == 'added' else f['old']['sha1']
38-
old_size = '' if delta == 'added' else f['old']['size']
39-
old_type = '' if delta == 'added' else f['old']['type']
40-
old_orig = '' if delta == 'added' else f['old']['original_path']
41-
42-
tuple = (category, new, old, new_filename, old_filename, new_sha1,
43-
old_sha1, new_size, old_size, new_type, old_type, new_orig, old_orig)
44-
tuple_list.append(tuple)
45-
4622
with open(result_file, 'wb') as out:
4723
csv_out = csv.writer(out)
48-
csv_out.writerow(['Type of delta', 'New scan path', 'Old scan path',
49-
'new_filename', 'old_filename', 'new_sha1', 'old_sha1', 'new_size',
50-
'old_size', 'new_type', 'old_type', 'new_original_path', 'old_original_path'])
51-
52-
for row in tuple_list:
24+
csv_out.writerow(['Type of delta', 'Path'])
25+
for row in [(f.category, f.old_file.path if f.category == 'removed' else f.new_file.path) for d in delta.deltas for f in delta.deltas.get(d)]:
5326
csv_out.writerow(row)
5427

5528

56-
def generate_json(data, result_file):
29+
def generate_json(delta, result_file):
5730
"""
58-
Using the OrderedDict generated by DeltaCode.to_dict(), create a .json file
31+
Using the DeltaCode object, create a .json file
5932
containing the primary information from the Delta objects.
6033
"""
61-
# TODO: Add json file headers here
34+
output = OrderedDict([
35+
('deltacode_version', __version__),
36+
('deltacode_stats', delta.get_stats()),
37+
('deltas', delta.to_dict())
38+
])
39+
6240
with open(result_file, 'w') as outfile:
63-
json.dump(data, outfile, indent=4)
41+
json.dump(output, outfile, indent=4)
6442

6543

6644
@click.command()
@@ -77,18 +55,15 @@ def cli(new, old, csv_file, json_file):
7755
.json file (-j or -json-file) at a user-designated location. If no file
7856
option is selected, the JSON results are printed to the console.
7957
"""
80-
8158
# do the delta
8259
delta = DeltaCode(new, old)
83-
data = delta.to_dict()
84-
8560

8661
# output to csv
8762
if csv_file:
88-
generate_csv(data, csv_file)
63+
generate_csv(delta, csv_file)
8964
# generate JSON output
9065
elif json_file:
91-
generate_json(data, json_file)
66+
generate_json(delta, json_file)
9267
# print to stdout
9368
else:
94-
print(json.dumps(data, indent=4))
69+
print(json.dumps(delta.to_dict()))

tests/data/cli/added1.csv

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path
2-
added,a/a5.py,,a5.py,,a0c7dab0c0f07cc8ef8fac7729359b6997ed50cd,,200,,file,,codebase_01_1_file_added/a/a5.py,
3-
unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_added/a/a3.py,codebase_01/a/a3.py
4-
unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_added/b/b4.py,codebase_01/b/b4.py
5-
unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_added/a/a2.py,codebase_01/a/a2.py
6-
unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_added/b/b2.py,codebase_01/b/b2.py
7-
unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_added/b/b1.py,codebase_01/b/b1.py
8-
unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_added/b/b3.py,codebase_01/b/b3.py
9-
unmodified,a/a4.py,a/a4.py,a4.py,a4.py,6f71666c46446c29d3f45feef5419ae76fb86a5b,6f71666c46446c29d3f45feef5419ae76fb86a5b,200,200,file,file,codebase_01_1_file_added/a/a4.py,codebase_01/a/a4.py
10-
unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_added/a/a1.py,codebase_01/a/a1.py
1+
Type of delta,Path
2+
added,a/a5.py
3+
unmodified,a/a3.py
4+
unmodified,b/b4.py
5+
unmodified,a/a2.py
6+
unmodified,b/b2.py
7+
unmodified,b/b1.py
8+
unmodified,b/b3.py
9+
unmodified,a/a4.py
10+
unmodified,a/a1.py

tests/data/cli/modified1.csv

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path
2-
modified,a/a4.py,a/a4.py,a4.py,a4.py,56aacf4e8ebcd7d8365e68cf11df933a237d33eb,6f71666c46446c29d3f45feef5419ae76fb86a5b,246,200,file,file,codebase_01_1_file_modified_not_renamed/a/a4.py,codebase_01/a/a4.py
3-
unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_modified_not_renamed/a/a3.py,codebase_01/a/a3.py
4-
unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b4.py,codebase_01/b/b4.py
5-
unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b1.py,codebase_01/b/b1.py
6-
unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_modified_not_renamed/a/a2.py,codebase_01/a/a2.py
7-
unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b2.py,codebase_01/b/b2.py
8-
unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b3.py,codebase_01/b/b3.py
9-
unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_modified_not_renamed/a/a1.py,codebase_01/a/a1.py
1+
Type of delta,Path
2+
modified,a/a4.py
3+
unmodified,a/a3.py
4+
unmodified,b/b4.py
5+
unmodified,b/b1.py
6+
unmodified,a/a2.py
7+
unmodified,b/b2.py
8+
unmodified,b/b3.py
9+
unmodified,a/a1.py

tests/data/cli/removed1.csv

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path
2-
removed,,a/a4.py,,a4.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01/a/a4.py
3-
unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_removed/a/a3.py,codebase_01/a/a3.py
4-
unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_removed/b/b4.py,codebase_01/b/b4.py
5-
unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_removed/b/b1.py,codebase_01/b/b1.py
6-
unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_removed/a/a2.py,codebase_01/a/a2.py
7-
unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_removed/b/b2.py,codebase_01/b/b2.py
8-
unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_removed/b/b3.py,codebase_01/b/b3.py
9-
unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_removed/a/a1.py,codebase_01/a/a1.py
1+
Type of delta,Path
2+
removed,a/a4.py
3+
unmodified,a/a3.py
4+
unmodified,b/b4.py
5+
unmodified,b/b1.py
6+
unmodified,a/a2.py
7+
unmodified,b/b2.py
8+
unmodified,b/b3.py
9+
unmodified,a/a1.py

tests/data/cli/renamed1.csv

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path
2-
added,a/a4_renamed_not_modified.py,,a4_renamed_not_modified.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01_1_file_renamed_not_modified/a/a4_renamed_not_modified.py,
3-
removed,,a/a4.py,,a4.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01/a/a4.py
4-
unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_renamed_not_modified/a/a3.py,codebase_01/a/a3.py
5-
unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b4.py,codebase_01/b/b4.py
6-
unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b1.py,codebase_01/b/b1.py
7-
unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b2.py,codebase_01/b/b2.py
8-
unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b3.py,codebase_01/b/b3.py
9-
unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_renamed_not_modified/a/a1.py,codebase_01/a/a1.py
10-
unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_renamed_not_modified/a/a2.py,codebase_01/a/a2.py
1+
Type of delta,Path
2+
added,a/a4_renamed_not_modified.py
3+
removed,a/a4.py
4+
unmodified,a/a3.py
5+
unmodified,b/b4.py
6+
unmodified,b/b1.py
7+
unmodified,b/b2.py
8+
unmodified,b/b3.py
9+
unmodified,a/a1.py
10+
unmodified,a/a2.py

tests/test_cli.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ def test_generate_csv_added(self):
5858
old_scan = self.get_test_loc('cli/old_added1.json')
5959

6060
delta = DeltaCode(new_scan, old_scan)
61-
data = delta.to_dict()
62-
6361
result_file = self.get_temp_file('.csv')
64-
cli.generate_csv(data, result_file)
65-
62+
cli.generate_csv(delta, result_file)
6663
expected_file = self.get_test_loc('cli/added1.csv')
6764
check_csvs(result_file, expected_file)
6865

@@ -71,9 +68,8 @@ def test_generate_csv_modified(self):
7168
old_scan = self.get_test_loc('cli/old_modified1.json')
7269

7370
delta = DeltaCode(new_scan, old_scan)
74-
data = delta.to_dict()
7571
result_file = self.get_temp_file('.csv')
76-
cli.generate_csv(data, result_file)
72+
cli.generate_csv(delta, result_file)
7773
expected_file = self.get_test_loc('cli/modified1.csv')
7874
check_csvs(result_file, expected_file)
7975

@@ -82,9 +78,8 @@ def test_generate_csv_removed(self):
8278
old_scan = self.get_test_loc('cli/old_removed1.json')
8379

8480
delta = DeltaCode(new_scan, old_scan)
85-
data = delta.to_dict()
8681
result_file = self.get_temp_file('.csv')
87-
cli.generate_csv(data, result_file)
82+
cli.generate_csv(delta, result_file)
8883
expected_file = self.get_test_loc('cli/removed1.csv')
8984
check_csvs(result_file, expected_file)
9085

@@ -93,8 +88,7 @@ def test_generate_csv_renamed(self):
9388
old_scan = self.get_test_loc('cli/old_renamed1.json')
9489

9590
delta = DeltaCode(new_scan, old_scan)
96-
data = delta.to_dict()
9791
result_file = self.get_temp_file('.csv')
98-
cli.generate_csv(data, result_file)
92+
cli.generate_csv(delta, result_file)
9993
expected_file = self.get_test_loc('cli/renamed1.csv')
10094
check_csvs(result_file, expected_file)

0 commit comments

Comments
 (0)