Skip to content

Commit f0ba69c

Browse files
committed
Determine whether license info added/removed #29
* Refactor Delta._license_diff() and Delta.to_dict(). * Refactor failing tests, add six new tests. Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 0c5fe02 commit f0ba69c

15 files changed

Lines changed: 412 additions & 9 deletions

src/deltacode/__init__.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,40 @@ def __init__(self, new_file=None, old_file=None, delta_type=None):
168168
self.old_file = old_file if old_file else File()
169169
self.category = delta_type if delta_type else ''
170170

171-
# Change the Delta object's 'category' attribute to
172-
# 'license change' if a substantial license change has been detected.
171+
# If a license change is detected, and depending on the nature of that change,
172+
# change the Delta object's 'category' attribute from 'modified' to
173+
# 'license change', 'license info removed' or 'license info added'.
173174
if self.category == 'modified':
174175
self._license_diff()
175176

176177
def _license_diff(self, cutoff_score=50):
177178
"""
178179
Compare the license details for a pair of 'new' and 'old' File objects
179180
in a Delta object and change the Delta object's 'category' attribute to
180-
'license change' if those details differ and the cutoff score test is
181-
satisfied.
181+
'license info removed', 'license info added' or 'license change' if
182+
there has been a license change and depending on the nature of that change.
182183
"""
183184
new_licenses = self.new_file.licenses or []
184185
new_keys = set(l.key for l in new_licenses if l.score >= cutoff_score)
186+
new_keys_all = set(l.key for l in new_licenses)
185187

186188
old_licenses = self.old_file.licenses or []
187189
old_keys = set(l.key for l in old_licenses if l.score >= cutoff_score)
190+
old_keys_all = set(l.key for l in old_licenses)
188191

189192
if new_keys != old_keys:
190-
self.category = 'license change'
193+
if len(new_keys) == 0:
194+
self.category = 'license info removed'
195+
elif len(old_keys) == 0:
196+
self.category = 'license info added'
197+
else:
198+
self.category = 'license change'
199+
200+
if new_keys_all != old_keys_all:
201+
if len(new_keys_all) == 0:
202+
self.category = 'license info removed'
203+
elif len(old_keys_all) == 0:
204+
self.category = 'license info added'
191205

192206
def to_dict(self):
193207
"""
@@ -226,6 +240,22 @@ def to_dict(self):
226240
('type', self.new_file.type),
227241
('size', self.new_file.size)
228242
])
243+
elif self.category == 'license info added':
244+
return OrderedDict([
245+
('category', 'license info added'),
246+
('path', self.new_file.path),
247+
('name', self.new_file.name),
248+
('type', self.new_file.type),
249+
('size', self.new_file.size)
250+
])
251+
elif self.category == 'license info removed':
252+
return OrderedDict([
253+
('category', 'license info removed'),
254+
('path', self.new_file.path),
255+
('name', self.new_file.name),
256+
('type', self.new_file.type),
257+
('size', self.new_file.size)
258+
])
229259
else:
230260
return OrderedDict([
231261
('category', 'unmodified'),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Type of delta,Path,Name,Type,Size
2+
license info added,some/path/a/a1.py,a1.py,file,350
3+
unmodified,some/path/b/b1.py,b1.py,file,290
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Type of delta,Path,Name,Type,Size
2+
license info added,some/path/a/a1.py,a1.py,file,350
3+
unmodified,some/path/b/b1.py,b1.py,file,290
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Type of delta,Path,Name,Type,Size
2+
license info removed,some/path/a/a1.py,a1.py,file,350
3+
unmodified,some/path/b/b1.py,b1.py,file,290
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Type of delta,Path,Name,Type,Size
2+
license info removed,some/path/a/a1.py,a1.py,file,350
3+
unmodified,some/path/b/b1.py,b1.py,file,290
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"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.",
3+
"scancode_version": "2.1.0",
4+
"scancode_options": {
5+
"--license": true,
6+
"--info": true
7+
},
8+
"files_count": 2,
9+
"files": [
10+
{
11+
"path": "some/path/a/a1.py",
12+
"type": "file",
13+
"name": "a1.py",
14+
"size": 350,
15+
"sha1": "222647771481d39dd3a53f6dc210c26abac37748",
16+
"licenses": [
17+
{
18+
"key": "apache-2.0",
19+
"score": 80.0,
20+
"short_name": "Apache 2.0",
21+
"category": "Permissive"
22+
}
23+
]
24+
},
25+
{
26+
"path": "some/path/b/b1.py",
27+
"type": "file",
28+
"name": "b1.py",
29+
"size": 290,
30+
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
31+
"licenses": [
32+
{
33+
"key": "mit",
34+
"score": 100.0,
35+
"short_name": "MIT License",
36+
"category": "Permissive"
37+
}
38+
]
39+
}
40+
]
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"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.",
3+
"scancode_version": "2.1.0",
4+
"scancode_options": {
5+
"--license": true,
6+
"--info": true
7+
},
8+
"files_count": 2,
9+
"files": [
10+
{
11+
"path": "some/path/a/a1.py",
12+
"type": "file",
13+
"name": "a1.py",
14+
"size": 350,
15+
"sha1": "222647771481d39dd3a53f6dc210c26abac37748",
16+
"licenses": [
17+
{
18+
"key": "apache-2.0",
19+
"score": 49.0,
20+
"short_name": "Apache 2.0",
21+
"category": "Permissive"
22+
}
23+
]
24+
},
25+
{
26+
"path": "some/path/b/b1.py",
27+
"type": "file",
28+
"name": "b1.py",
29+
"size": 290,
30+
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
31+
"licenses": [
32+
{
33+
"key": "mit",
34+
"score": 100.0,
35+
"short_name": "MIT License",
36+
"category": "Permissive"
37+
}
38+
]
39+
}
40+
]
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"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.",
3+
"scancode_version": "2.1.0",
4+
"scancode_options": {
5+
"--license": true,
6+
"--info": true
7+
},
8+
"files_count": 2,
9+
"files": [
10+
{
11+
"path": "some/path/a/a1.py",
12+
"type": "file",
13+
"name": "a1.py",
14+
"size": 350,
15+
"sha1": "222647771481d39dd3a53f6dc210c26abac37748",
16+
"licenses": []
17+
},
18+
{
19+
"path": "some/path/b/b1.py",
20+
"type": "file",
21+
"name": "b1.py",
22+
"size": 290,
23+
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
24+
"licenses": [
25+
{
26+
"key": "mit",
27+
"score": 100.0,
28+
"short_name": "MIT License",
29+
"category": "Permissive"
30+
}
31+
]
32+
}
33+
]
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"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.",
3+
"scancode_version": "2.1.0",
4+
"scancode_options": {
5+
"--license": true,
6+
"--info": true
7+
},
8+
"files_count": 2,
9+
"files": [
10+
{
11+
"path": "some/path/a/a1.py",
12+
"type": "file",
13+
"name": "a1.py",
14+
"size": 350,
15+
"sha1": "222647771481d39dd3a53f6dc210c26abac37748",
16+
"licenses": []
17+
},
18+
{
19+
"path": "some/path/b/b1.py",
20+
"type": "file",
21+
"name": "b1.py",
22+
"size": 290,
23+
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
24+
"licenses": [
25+
{
26+
"key": "mit",
27+
"score": 100.0,
28+
"short_name": "MIT License",
29+
"category": "Permissive"
30+
}
31+
]
32+
}
33+
]
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"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.",
3+
"scancode_version": "2.1.0",
4+
"scancode_options": {
5+
"--license": true,
6+
"--info": true
7+
},
8+
"files_count": 2,
9+
"files": [
10+
{
11+
"path": "some/path/a/a1.py",
12+
"type": "file",
13+
"name": "a1.py",
14+
"size": 300,
15+
"sha1": "000647771481d39dd3a53f6dc210c26abac37748",
16+
"licenses": []
17+
},
18+
{
19+
"path": "some/path/b/b1.py",
20+
"type": "file",
21+
"name": "b1.py",
22+
"size": 290,
23+
"sha1": "333647771481d39dd3a53f6dc210c26abac37748",
24+
"licenses": [
25+
{
26+
"key": "mit",
27+
"score": 100.0,
28+
"short_name": "MIT License",
29+
"category": "Permissive"
30+
}
31+
]
32+
}
33+
]
34+
}

0 commit comments

Comments
 (0)