Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 54 additions & 0 deletions src/deltacode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ def __init__(self, new_path, old_path, options):
self.options = options
self.deltas = []
self.errors = []
self.stats = Stat(self.new.files_count, self.old.files_count)

if self.new.path != '' and self.old.path != '':
self.determine_delta()
self.determine_moved()
self.license_diff()
self.copyright_diff()
self.stats.calculate_stats()
# Sort deltas by score, descending, i.e., high > low, and then by
# factors, alphabetically. Run the least significant sort first.
self.deltas.sort(key=lambda Delta: Delta.factors, reverse=False)
Expand Down Expand Up @@ -106,6 +108,7 @@ def determine_delta(self):
except KeyError:
delta = Delta(100, new_file, None)
delta.status = 'added'
self.stats.num_added += 1
self.deltas.append(delta)
continue

Expand All @@ -117,11 +120,13 @@ def determine_delta(self):
if new_file.sha1 == f.sha1:
delta = Delta(0, new_file, f)
delta.status = 'unmodified'
self.stats.num_unmodified += 1
self.deltas.append(delta)
continue
else:
delta = Delta(20, new_file, f)
delta.status = 'modified'
self.stats.num_modified += 1
self.deltas.append(delta)

# now time to find the added.
Expand All @@ -138,6 +143,7 @@ def determine_delta(self):
except KeyError:
delta = Delta(0, None, old_file)
delta.status = 'removed'
self.stats.num_removed += 1
self.deltas.append(delta)
continue

Expand Down Expand Up @@ -181,6 +187,9 @@ def update_deltas(self, added, removed):
"""
delta = Delta(0, added.new_file, removed.old_file)
delta.status = 'moved'
self.stats.num_moved += 1
self.stats.num_added -= 1
self.stats.num_removed -= 1
self.deltas.append(delta)
self.deltas.remove(added)
self.deltas.remove(removed)
Expand Down Expand Up @@ -312,3 +321,48 @@ def to_dict(self):
('new', new_file),
('old', old_file),
])

class Stat(object):
"""
Contains all the stats for the file changes in the new directory
with respect to the old directory.
"""
def __init__(self, new_files_count, old_files_count):
self.new_files_count = new_files_count
self.old_files_count = old_files_count
self.num_added = 0
self.num_removed = 0
self.num_moved = 0
self.num_modified = 0
self.num_unmodified = 0
self.percent_added = 0
self.percent_removed = 0
self.percent_moved = 0
self.percent_modified = 0
self.percent_unmodified = 0

def calculate_stats(self):
"""
Calculates the percentage change of new directory with respect to
the old directory.
"""
self.percent_added = utils.calculate_percent(self.num_added, self.old_files_count)
self.percent_removed = utils.calculate_percent(self.num_removed, self.old_files_count)
self.percent_moved = utils.calculate_percent(self.num_moved, self.old_files_count)
self.percent_modified = utils.calculate_percent(self.num_modified, self.old_files_count)
self.percent_unmodified = utils.calculate_percent(self.num_unmodified, self.old_files_count)

def to_dict(self):
"""
Return an OrderedDict comprising all the percent attributes of the object.
"""
return OrderedDict([
('old_files_count', self.old_files_count),
('new_files_count', self.new_files_count),
('percent_added', self.percent_added),
('percent_removed', self.percent_removed),
('percent_moved', self.percent_moved),
('percent_modified', self.percent_modified),
('percent_unmodified', self.percent_unmodified),
])

1 change: 1 addition & 0 deletions src/deltacode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def write_json(deltacode, outfile, all_delta_types=False):
('deltacode_version', __version__),
('deltacode_errors', collect_errors(deltacode)),
('deltas_count', len([d for d in deltas(deltacode, all_delta_types)])),
('delta_stats', deltacode.stats.to_dict()),
('deltas', deltas(deltacode, all_delta_types))
])

Expand Down
9 changes: 7 additions & 2 deletions src/deltacode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Visit https://github.com/nexB/deltacode/ for support and download.
#

from __future__ import absolute_import
from __future__ import absolute_import, division

from collections import defaultdict

Expand Down Expand Up @@ -182,14 +182,19 @@ def deltas(deltacode, all_delta_types=False):
elif not delta.is_unmodified():
yield delta.to_dict()

def calculate_percent(value, total):
"""
Return the rounded value percentage of total.
"""
ratio = (value / total) * 100
return round(ratio, 2)

class AlignmentException(Exception):
"""
Named exception for alignment errors.
"""
pass


def align_trees(a_files, b_files):
"""
Given two sequences of File objects 'a' and 'b', return a tuple of
Expand Down
69 changes: 69 additions & 0 deletions tests/data/deltacode/all_stat_order_check_new.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"scancode_notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
"scancode_version": "2.9.0b1.post8.2a658c314",
"scancode_options": {
"input": "C:\\code\\nexb\\dev\\codebase01\\sorting\\sorted01_new",
"--copyright": true,
"--info": true,
"--json-pp": "C:\\code\\nexb\\dev\\deltacode\\tests\\data\\deltacode\\scan_sorted01_new.json",
"--license": true,
"--package": true
},
"files_count": 6,
"files": [
{
"path": "new/a.txt",
"type": "file",
"name": "a.txt",
"size": 100,
"sha1": "abtrbt",
"licenses": [
]
},
{
"path": "new/b.txt",
"type": "file",
"name": "b.txt",
"size": 100,
"sha1": "dasdas",
"licenses": [
]
},
{
"path": "new/c.txt",
"type": "file",
"name": "c.txt",
"size": 110,
"sha1": "avfvf",
"licenses": [
]
},
{
"path": "new/d.txt",
"type": "file",
"name": "d.txt",
"size": 110,
"sha1": "avfvf",
"licenses": [
]
},
{
"path": "new/e2.txt",
"type": "file",
"name": "e2.txt",
"size": 210,
"sha1": "avfvvrvf",
"licenses": [
]
},
{
"path": "new/f2.txt",
"type": "file",
"name": "f2.txt",
"size": 110,
"sha1": "ivfvvrvf",
"licenses": [
]
}
]
}
78 changes: 78 additions & 0 deletions tests/data/deltacode/all_stat_order_check_old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"scancode_notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
"scancode_version": "2.9.0b1.post8.2a658c314",
"scancode_options": {
"input": "C:\\code\\nexb\\dev\\codebase01\\sorting\\sorted01_old",
"--copyright": true,
"--info": true,
"--json-pp": "C:\\code\\nexb\\dev\\deltacode\\tests\\data\\deltacode\\scan_sorted01_old.json",
"--license": true,
"--package": true
},
"files_count": 7,
"files": [
{
"path": "old/a.txt",
"type": "file",
"name": "a.txt",
"size": 100,
"sha1": "abtrbt",
"licenses": [
]
},
{
"path": "old/b.txt",
"type": "file",
"name": "b.txt",
"size": 100,
"sha1": "dasdas",
"licenses": [
]
},
{
"path": "old/c.txt",
"type": "file",
"name": "c.txt",
"size": 100,
"sha1": "awd",
"licenses": [
]
},
{
"path": "old/a/d.txt",
"type": "file",
"name": "d.txt",
"size": 110,
"sha1": "avfvf",
"licenses": [
]
},
{
"path": "old/e1.txt",
"type": "file",
"name": "e1.txt",
"size": 110,
"sha1": "cegr",
"licenses": [
]
},
{
"path": "old/f1.txt",
"type": "file",
"name": "f1.txt",
"size": 310,
"sha1": "gkbme",
"licenses": [
]
},
{
"path": "old/g.txt",
"type": "file",
"name": "g.txt",
"size": 410,
"sha1": "btbtg",
"licenses": [
]
}
]
}
2 changes: 1 addition & 1 deletion tests/data/deltacode/scan_sorted01_new.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"--license": true,
"--package": true
},
"files_count": 5,
"files_count": 4,
"files": [
{
"path": "sorted01_new",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_deltacode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,3 +1592,27 @@ def test_DeltaCode_permissive_add_public_domain(self):
assert [d.status for d in deltas_object if d.new_file.path == 'a1.py'] == ['modified']
assert [d.factors for d in deltas_object if d.new_file.path == 'a2.py'].pop() == []
assert [d.status for d in deltas_object if d.new_file.path == 'a2.py'] == ['unmodified']

def test_Stat_calculation_and_ordering(self):
new_scan = self.get_test_loc('deltacode/all_stat_order_check_new.json')
old_scan = self.get_test_loc('deltacode/all_stat_order_check_old.json')

options = OrderedDict([
('--all-delta-types', True)
])

expected = OrderedDict([
('old_files_count', 7),
('new_files_count', 6),
('percent_added', 28.57),
('percent_removed', 42.86),
('percent_moved', 14.29),
('percent_modified', 14.29),
('percent_unmodified', 28.57)
])

deltacode_object = DeltaCode(new_scan, old_scan, options)

stats_object = deltacode_object.stats.to_dict()

assert stats_object == expected