Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/deltacode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def determine_delta(self):
for path, old_files in old_index.items():
for old_file in old_files:
old_files_to_visit -= 1

if old_file.type != 'file':
continue

Expand All @@ -100,7 +100,7 @@ def determine_delta(self):
except KeyError:
deltas['removed'].append(Delta(None, old_file, 'removed'))
continue

# make sure everything is accounted for
assert new_files_to_visit == 0
assert old_files_to_visit == 0
Expand Down Expand Up @@ -130,7 +130,7 @@ def to_dict(self):
"""
if self.deltas == None:
return

return OrderedDict([
('added', [d.to_dict() for d in self.deltas.get('added')]),
('removed', [d.to_dict() for d in self.deltas.get('removed')]),
Expand All @@ -155,18 +155,23 @@ def to_dict(self):
if self.new_file == None and self.old_file == None:
return

if self.new_file == None:
if self.category == 'added':
return OrderedDict([
('category', 'added'),
('path', self.new_file.path)
])
elif self.category == 'removed':
return OrderedDict([
('new', None),
('old', self.old_file.to_dict()),
('category', 'removed'),
('path', self.old_file.path)
])
elif self.old_file == None:
elif self.category == 'modified':
return OrderedDict([
('new', self.new_file.to_dict()),
('old', None),
('category', 'modified'),
('path', self.old_file.path)
])
else:
return OrderedDict([
('new', self.new_file.to_dict()),
('old', self.old_file.to_dict()),
('category', 'unmodified'),
('path', self.old_file.path)
])
51 changes: 15 additions & 36 deletions src/deltacode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,45 @@
#
from __future__ import absolute_import

from collections import OrderedDict

import csv
import json

import click

from deltacode import DeltaCode
from deltacode import __version__


def generate_csv(data, result_file):
"""
Using the OrderedDict generated by DeltaCode.to_dict(), create a .csv file
containing the primary information from the Delta objects.
"""
category, new, new_filename, new_sha1, new_size, new_type, new_orig, old,\
old_filename, old_sha1, old_size, old_type, old_orig = '', '', '', '',\
'', '', '', '', '', '', '', '', ''
tuple = ()
tuple_list = []
deltas = data

for delta in deltas:
category = delta
for f in deltas[delta]:
new = '' if delta == 'removed' else f['new']['path']
new_filename = '' if delta == 'removed' else f['new']['name']
new_sha1 = '' if delta == 'removed' else f['new']['sha1']
new_size = '' if delta == 'removed' else f['new']['size']
new_type = '' if delta == 'removed' else f['new']['type']
new_orig = '' if delta == 'removed' else f['new']['original_path']
old = '' if delta == 'added' else f['old']['path']
old_filename = '' if delta == 'added' else f['old']['name']
old_sha1 = '' if delta == 'added' else f['old']['sha1']
old_size = '' if delta == 'added' else f['old']['size']
old_type = '' if delta == 'added' else f['old']['type']
old_orig = '' if delta == 'added' else f['old']['original_path']

tuple = (category, new, old, new_filename, old_filename, new_sha1,
old_sha1, new_size, old_size, new_type, old_type, new_orig, old_orig)
tuple_list.append(tuple)
tuple_list = [(f['category'], f['path']) for delta in deltas for f in deltas[delta]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tuple_list is a poor variable choice. Reading the list comprehension, we know that it is a list of tuples; no need to be redundant here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, we can skip the varibles all together and just do the list comprehension on line 28

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and you should really use .get() syntax when accessing dictionaries.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. 👍


with open(result_file, 'wb') as out:
csv_out = csv.writer(out)
csv_out.writerow(['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'])

csv_out.writerow(['Type of delta', 'Path'])
for row in tuple_list:
csv_out.writerow(row)


def generate_json(data, result_file):
def generate_json(delta, result_file):
"""
Using the OrderedDict generated by DeltaCode.to_dict(), create a .json file
Using the DeltaCode object, create a .json file
containing the primary information from the Delta objects.
"""
# TODO: Add json file headers here
json_dict = OrderedDict([

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, json_dict is a poor variable choice.

Something like output is all that is needed.

('deltacode_version', __version__),
('deltacode_stats', delta.get_stats()),
('deltas', delta.to_dict())
])

with open(result_file, 'w') as outfile:
json.dump(data, outfile, indent=4)
json.dump(json_dict, outfile, indent=4)


@click.command()
Expand All @@ -77,18 +58,16 @@ def cli(new, old, csv_file, json_file):
.json file (-j or -json-file) at a user-designated location. If no file
option is selected, the JSON results are printed to the console.
"""

# do the delta
delta = DeltaCode(new, old)
data = delta.to_dict()


# output to csv
if csv_file:
generate_csv(data, csv_file)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We pass the whole delta object to generate_csv(), like we did for generate_json() to keep things similar.

# generate JSON output
elif json_file:
generate_json(data, json_file)
generate_json(delta, json_file)
# print to stdout
else:
print(json.dumps(data, indent=4))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to change this to print(json.dumps(delta.to_dict()), like the rest of our output functions.

20 changes: 10 additions & 10 deletions tests/data/cli/added1.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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
added,a/a5.py,,a5.py,,a0c7dab0c0f07cc8ef8fac7729359b6997ed50cd,,200,,file,,codebase_01_1_file_added/a/a5.py,
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
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
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
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
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
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
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
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
Type of delta,Path
added,a/a5.py
unmodified,a/a3.py
unmodified,b/b4.py
unmodified,a/a2.py
unmodified,b/b2.py
unmodified,b/b1.py
unmodified,b/b3.py
unmodified,a/a4.py
unmodified,a/a1.py
18 changes: 9 additions & 9 deletions tests/data/cli/modified1.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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
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
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
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
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
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
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
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
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
Type of delta,Path
modified,a/a4.py
unmodified,a/a3.py
unmodified,b/b4.py
unmodified,b/b1.py
unmodified,a/a2.py
unmodified,b/b2.py
unmodified,b/b3.py
unmodified,a/a1.py
18 changes: 9 additions & 9 deletions tests/data/cli/removed1.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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
removed,,a/a4.py,,a4.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01/a/a4.py
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
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
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
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
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
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
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
Type of delta,Path
removed,a/a4.py
unmodified,a/a3.py
unmodified,b/b4.py
unmodified,b/b1.py
unmodified,a/a2.py
unmodified,b/b2.py
unmodified,b/b3.py
unmodified,a/a1.py
20 changes: 10 additions & 10 deletions tests/data/cli/renamed1.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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
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,
removed,,a/a4.py,,a4.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01/a/a4.py
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
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
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
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
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
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
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
Type of delta,Path
added,a/a4_renamed_not_modified.py
removed,a/a4.py
unmodified,a/a3.py
unmodified,b/b4.py
unmodified,b/b1.py
unmodified,b/b2.py
unmodified,b/b3.py
unmodified,a/a1.py
unmodified,a/a2.py
Loading