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
57 changes: 49 additions & 8 deletions src/deltacode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ def determine_delta(self):
deltas['unmodified'].append(Delta(new_file, f, 'unmodified'))
continue
else:
deltas['modified'].append(Delta(new_file, f, 'modified'))
delta = Delta(new_file, f, 'modified')
# Change the Delta object's 'category' attribute to
# 'license change' if a substantial license change has been detected.
delta.license_diff()
deltas['modified'].append(delta)

# now time to find the added.
for path, old_files in old_index.items():
Expand Down Expand Up @@ -124,11 +128,11 @@ def get_stats(self):

def to_dict(self):
"""
Given an OrderedDict of Delta objects, return an OrderedDict with a
'deltacode_version' field, a 'deltacode_stats' field, a 'deltas_count'
field, and a 'deltas' field containing a list of our Delta objects.
Given an OrderedDict of Delta objects, return an OrderedDict of Delta
objects grouping the objects under the keys 'added', 'removed',
'modified' or 'unmodified'.
"""
if self.deltas == None:
if self.deltas is None:
return

return OrderedDict([
Expand All @@ -151,8 +155,40 @@ def __init__(self, new_file=None, old_file=None, delta_type=None):
self.old_file = old_file
self.category = delta_type

def license_diff(self):
"""
Compare the license details for a pair of 'new' and 'old' File objects
in a Delta object and change the Delta object's 'category' attribute to
'license change' if those details differ and the cutoff score test is
satisfied.
"""
if self.new_file is None or self.old_file is None:
return

cutoff_score = 50

if self.new_file.licenses:
new_keys = [l.key for l in self.new_file.licenses if l.score >= cutoff_score]
else:
new_keys = []

if self.old_file.licenses:
old_keys = [l.key for l in self.old_file.licenses if l.score >= cutoff_score]
else:
old_keys = []

new_keys = list(set(new_keys))
old_keys = list(set(old_keys))

if new_keys != old_keys:
self.category = 'license change'

def to_dict(self):
if self.new_file == None and self.old_file == None:
"""
Check the 'category' attribute of the Delta object and return an
OrderedDict comprising the 'category' and 'path' of the object.
"""
if self.new_file is None and self.old_file is None:
return

if self.category == 'added':
Expand All @@ -168,10 +204,15 @@ def to_dict(self):
elif self.category == 'modified':
return OrderedDict([
('category', 'modified'),
('path', self.old_file.path)
('path', self.new_file.path)
])
elif self.category == 'license change':
return OrderedDict([
('category', 'license change'),
('path', self.new_file.path)
])
else:
return OrderedDict([
('category', 'unmodified'),
('path', self.old_file.path)
('path', self.new_file.path)
])
4 changes: 4 additions & 0 deletions tests/data/cli/modified_new_license_added.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Type of delta,Path
modified,some/path/c/c1.py
license change,some/path/a/a1.py
license change,some/path/b/b1.py
3 changes: 3 additions & 0 deletions tests/data/cli/modified_new_license_added_low_score.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Type of delta,Path
modified,some/path/a/a1.py
modified,some/path/b/b1.py
92 changes: 92 additions & 0 deletions tests/data/cli/scan_modified_new_license_added.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"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.1.0",
"scancode_options": {
"--license": true,
"--info": true
},
"files_count": 3,
"files": [
{
"path": "some/path/a/a1.py",
"type": "file",
"name": "a1.py",
"size": 300,
"sha1": "000647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "apache-2.0",
"score": 80.0,
"short_name": "Apache 2.0",
"category": "Permissive"
},
{
"key": "agpl-2.0",
"score": 70.0,
"short_name": "AGPL 2.0",
"category": "Copyleft"
},
{
"key": "bsd-simplified",
"score": 100.0,
"short_name": "BSD-Simplified",
"category": "Permissive"
},
{
"key": "mit",
"score": 100.0,
"short_name": "MIT License",
"category": "Permissive"
}
]
},
{
"path": "some/path/b/b1.py",
"type": "file",
"name": "b1.py",
"size": 300,
"sha1": "111647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "apache-2.0",
"score": 80.0,
"short_name": "Apache 2.0",
"category": "Permissive"
},
{
"key": "gpl-2.0",
"score": 60.0,
"short_name": "GPL 2.0",
"category": "Copyleft"
},
{
"key": "bsd-simplified",
"score": 100.0,
"short_name": "BSD-Simplified",
"category": "Permissive"
},
{
"key": "mit",
"score": 100.0,
"short_name": "MIT License",
"category": "Permissive"
}
]
},
{
"path": "some/path/c/c1.py",
"type": "file",
"name": "c1.py",
"size": 300,
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "mit",
"score": 100.0,
"short_name": "MIT License",
"category": "Permissive"
}
]
}
]
}
59 changes: 59 additions & 0 deletions tests/data/cli/scan_modified_new_license_added_low_score.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"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.1.0",
"scancode_options": {
"--license": true,
"--info": true
},
"files_count": 2,
"files": [
{
"path": "some/path/a/a1.py",
"type": "file",
"name": "a1.py",
"size": 350,
"sha1": "222647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "apache-2.0",
"score": 80.0,
"short_name": "Apache 2.0",
"category": "Permissive"
},
{
"key": "agpl-2.0",
"score": 70.0,
"short_name": "AGPL 2.0",
"category": "Copyleft"
},
{
"key": "bsd-simplified",
"score": 40.0,
"short_name": "BSD-Simplified",
"category": "Permissive"
}
]
},
{
"path": "some/path/b/b1.py",
"type": "file",
"name": "b1.py",
"size": 290,
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "mit",
"score": 100.0,
"short_name": "MIT License",
"category": "Permissive"
},
{
"key": "bsd-simplified",
"score": 30.0,
"short_name": "BSD-Simplified",
"category": "Permissive"
}
]
}
]
}
80 changes: 80 additions & 0 deletions tests/data/cli/scan_modified_old_license_added.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"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.1.0",
"scancode_options": {
"--license": true,
"--info": true
},
"files_count": 3,
"files": [
{
"path": "some/path/a/a1.py",
"type": "file",
"name": "a1.py",
"size": 200,
"sha1": "84b647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "apache-2.0",
"score": 80.0,
"short_name": "Apache 2.0",
"category": "Permissive"
},
{
"key": "public-domain",
"score": 10.0,
"short_name": "Public Domain",
"category": "Public Domain"
},
{
"key": "bsd-simplified",
"score": 100.0,
"short_name": "BSD-Simplified",
"category": "Permissive"
}
]
},
{
"path": "some/path/b/b1.py",
"type": "file",
"name": "b2.py",
"size": 200,
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "mpl-2.0",
"score": 90.0,
"short_name": "MPL 2.0",
"category": "Copyleft Limited"
},
{
"key": "public-domain",
"score": 10.0,
"short_name": "Public Domain",
"category": "Public Domain"
},
{
"key": "bsd-simplified",
"score": 100.0,
"short_name": "BSD-Simplified",
"category": "Permissive"
}
]
},
{
"path": "some/path/c/c1.py",
"type": "file",
"name": "c1.py",
"size": 300,
"sha1": "222647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "mit",
"score": 100.0,
"short_name": "MIT License",
"category": "Permissive"
}
]
}
]
}
47 changes: 47 additions & 0 deletions tests/data/cli/scan_modified_old_license_added_low_score.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"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.1.0",
"scancode_options": {
"--license": true,
"--info": true
},
"files_count": 2,
"files": [
{
"path": "some/path/a/a1.py",
"type": "file",
"name": "a1.py",
"size": 300,
"sha1": "000647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "apache-2.0",
"score": 80.0,
"short_name": "Apache 2.0",
"category": "Permissive"
},
{
"key": "agpl-2.0",
"score": 70.0,
"short_name": "AGPL 2.0",
"category": "Copyleft"
}
]
},
{
"path": "some/path/b/b1.py",
"type": "file",
"name": "b1.py",
"size": 300,
"sha1": "111647771481d39dd3a53f6dc210c26abac37748",
"licenses": [
{
"key": "mit",
"score": 100.0,
"short_name": "MIT License",
"category": "Permissive"
}
]
}
]
}
Loading