Skip to content

Commit 76b88dc

Browse files
committed
Add license/copyright score for 'added' files #84
Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent bcfd9be commit 76b88dc

8 files changed

Lines changed: 223 additions & 14 deletions

File tree

src/deltacode/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ def is_unmodified(self):
282282
self.old_file.path == self.new_file.path):
283283
return True
284284

285+
def is_added(self):
286+
"""
287+
Identify a Delta object reflecting the addition of a File.
288+
"""
289+
if not self.old_file and self.new_file:
290+
return True
291+
285292
def to_dict(self):
286293
"""
287294
Return an OrderedDict comprising the 'factors', 'score' and new and old

src/deltacode/utils.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@
3535

3636
def determine_license_diff(delta, unique_categories):
3737
"""
38-
Increase the Delta object's 'score' attribute and add one or more
39-
appropriate categories to its 'factors' attribute if there has been a
40-
license change and depending on the nature of that change.
41-
"""
38+
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
39+
one or more appropriate categories to its 'factors' attribute if there has
40+
been a license change and depending on the nature of that change.
41+
"""
42+
if delta.is_added():
43+
new_licenses = delta.new_file.licenses or []
44+
new_categories = set(license.category for license in new_licenses)
45+
46+
if delta.new_file.has_licenses():
47+
delta.update(20, 'license info added')
48+
# no license ==> 'Copyleft Limited'or higher
49+
for category in new_categories:
50+
if category in unique_categories:
51+
delta.update(20, category.lower() + ' added')
52+
return
53+
4254
if not delta.is_modified():
4355
return
4456

@@ -73,10 +85,15 @@ def determine_license_diff(delta, unique_categories):
7385

7486
def determine_copyright_diff(delta):
7587
"""
76-
Increase the Delta object's 'score' attribute and add one or more
77-
appropriate categories to its 'factors' attribute if there has been a
78-
copyright change and depending on the nature of that change.
88+
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
89+
one or more appropriate categories to its 'factors' attribute if there has
90+
been a copyright change and depending on the nature of that change.
7991
"""
92+
if delta.is_added():
93+
if delta.new_file.has_copyrights():
94+
delta.update(10, 'copyright info added')
95+
return
96+
8097
if not delta.is_modified():
8198
return
8299

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Factors,Score,Path,Name,Type,Size,Old Path
2-
added,100,b/a4.py,a4.py,file,200,
3-
added,100,b/a4_copy.py,a4_copy.py,file,200,
2+
added license info added copyright info added,130,b/a4.py,a4.py,file,200,
3+
added license info added copyright info added,130,b/a4_copy.py,a4_copy.py,file,200,
44
removed,0,a/a4.py,a4.py,file,200,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Factors,Score,Path,Name,Type,Size,Old Path
2-
added,100,b/a4.py,a4.py,file,200,
3-
added,100,c/a4.py,a4.py,file,200,
2+
added license info added copyright info added,130,b/a4.py,a4.py,file,200,
3+
added license info added copyright info added,130,c/a4.py,a4.py,file,200,
44
removed,0,a/a4.py,a4.py,file,200,

tests/data/cli/added1.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Factors,Score,Path,Name,Type,Size,Old Path
2-
added,100,a/a5.py,a5.py,file,200,
2+
added license info added copyright info added,130,a/a5.py,a5.py,file,200,

tests/data/cli/renamed1.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Factors,Score,Path,Name,Type,Size,Old Path
2-
added,100,a/a4_renamed_not_modified.py,a4_renamed_not_modified.py,file,200,
2+
added license info added copyright info added,130,a/a4_renamed_not_modified.py,a4_renamed_not_modified.py,file,200,
33
removed,0,a/a4.py,a4.py,file,200,

tests/test_deltacode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ def test_DeltaCode_sort_order(self):
14631463
deltas_object = deltacode_object.deltas
14641464

14651465
expected = [
1466-
['added'],
1466+
['added', 'license info added', 'copyright info added'],
14671467
['modified'],
14681468
['moved'],
14691469
['removed'],

tests/test_utils.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,73 @@ def test_determine_license_diff_copyleft_to_copyleft_limited(self):
654654
assert len(test_delta.factors) == 1
655655
assert 'license change' in test_delta.factors
656656

657+
def test_determine_license_diff_file_added_permissive_license(self):
658+
test_file_new = models.File({
659+
'path':'/test/path.txt',
660+
'name': 'path.txt',
661+
'sha1': 'a',
662+
'original_path': '',
663+
"licenses": [
664+
{
665+
"key": "mit",
666+
"score": 80.0,
667+
"short_name": "MIT License",
668+
"category": "Permissive"
669+
}
670+
]
671+
})
672+
673+
test_delta = deltacode.Delta(100, test_file_new, None)
674+
675+
utils.determine_license_diff(test_delta, unique_categories)
676+
677+
assert test_delta.score == 120
678+
assert len(test_delta.factors) == 1
679+
680+
assert 'license info added' in test_delta.factors
681+
682+
def test_determine_license_diff_file_added_commercial_and_copyleft_licenses(self):
683+
test_file_new = models.File({
684+
'path':'/test/path.txt',
685+
'name': 'path.txt',
686+
'sha1': 'a',
687+
'original_path': '',
688+
"licenses": [
689+
{
690+
"key": "commercial-license",
691+
"score": 55.0,
692+
"short_name": "Commercial License",
693+
"category": "Commercial",
694+
"owner": "Unspecified"
695+
},
696+
{
697+
"key": "adapt-1.0",
698+
"score": 15.0,
699+
"short_name": "APL 1.0",
700+
"category": "Copyleft",
701+
"owner": "OSI - Open Source Initiative"
702+
}
703+
]
704+
})
705+
706+
test_delta = deltacode.Delta(100, test_file_new, None)
707+
708+
utils.determine_license_diff(test_delta, unique_categories)
709+
710+
assert test_delta.score == 160
711+
assert len(test_delta.factors) == 3
712+
713+
assert 'license info added' in test_delta.factors
714+
715+
expected_factors = [
716+
'license info added',
717+
'commercial added',
718+
'copyleft added'
719+
]
720+
721+
for factor in expected_factors:
722+
assert factor in test_delta.factors
723+
657724
def test_determine_copyright_diff_empty(self):
658725
test_delta = deltacode.Delta()
659726

@@ -1016,6 +1083,32 @@ def test_determine_copyright_diff_one_copyright_removed(self):
10161083
assert len(test_delta.factors) == 1
10171084
assert 'copyright change' in test_delta.factors
10181085

1086+
def test_determine_copyright_diff_file_added_one_copyright(self):
1087+
test_file_new = models.File({
1088+
'path':'/test/path.txt',
1089+
'name': 'path.txt',
1090+
'sha1': 'a',
1091+
'original_path': '',
1092+
"copyrights": [
1093+
{
1094+
"statements": [
1095+
"Copyright (c) 2017-2018 Francois Hennebique and others."
1096+
],
1097+
"holders": [
1098+
"Francois Hennebique and others."
1099+
]
1100+
}
1101+
]
1102+
})
1103+
1104+
test_delta = deltacode.Delta(100, test_file_new, None)
1105+
1106+
utils.determine_copyright_diff(test_delta)
1107+
1108+
assert test_delta.score == 110
1109+
assert len(test_delta.factors) == 1
1110+
assert 'copyright info added' in test_delta.factors
1111+
10191112
def test_determine_lic_copy_diffs_copyright_and_license_info_added(self):
10201113
test_file_new = models.File({
10211114
'path':'/test/path.txt',
@@ -1352,6 +1445,98 @@ def test_determine_lic_copy_diffs_license_change_no_copyright_change(self):
13521445
for factor in expected_factors:
13531446
assert factor in test_delta.factors
13541447

1448+
def test_determine_lic_copy_diffs_file_added_copyright_and_permissive_license(self):
1449+
test_file_new = models.File({
1450+
'path':'/test/path.txt',
1451+
'name': 'path.txt',
1452+
'sha1': 'a',
1453+
'original_path': '',
1454+
"licenses": [
1455+
{
1456+
"key": "mit",
1457+
"score": 80.0,
1458+
"short_name": "MIT License",
1459+
"category": "Permissive"
1460+
}
1461+
],
1462+
"copyrights": [
1463+
{
1464+
"statements": [
1465+
"Copyright (c) 2017-2018 Francois Hennebique and others."
1466+
],
1467+
"holders": [
1468+
"Francois Hennebique and others."
1469+
]
1470+
}
1471+
]
1472+
})
1473+
1474+
test_delta = deltacode.Delta(100, test_file_new, None)
1475+
1476+
utils.determine_license_diff(test_delta, unique_categories)
1477+
utils.determine_copyright_diff(test_delta)
1478+
1479+
expected_factors = [
1480+
'license info added',
1481+
'copyright info added'
1482+
]
1483+
1484+
assert test_delta.score == 130
1485+
assert len(test_delta.factors) == 2
1486+
for factor in expected_factors:
1487+
assert factor in test_delta.factors
1488+
1489+
def test_determine_lic_copy_diffs_file_added_copyright_and_commercial_and_copyleft_licenses(self):
1490+
test_file_new = models.File({
1491+
'path':'/test/path.txt',
1492+
'name': 'path.txt',
1493+
'sha1': 'a',
1494+
'original_path': '',
1495+
"licenses": [
1496+
{
1497+
"key": "commercial-license",
1498+
"score": 55.0,
1499+
"short_name": "Commercial License",
1500+
"category": "Commercial",
1501+
"owner": "Unspecified"
1502+
},
1503+
{
1504+
"key": "adapt-1.0",
1505+
"score": 15.0,
1506+
"short_name": "APL 1.0",
1507+
"category": "Copyleft",
1508+
"owner": "OSI - Open Source Initiative"
1509+
}
1510+
],
1511+
"copyrights": [
1512+
{
1513+
"statements": [
1514+
"Copyright (c) 2017-2018 Francois Hennebique and others."
1515+
],
1516+
"holders": [
1517+
"Francois Hennebique and others."
1518+
]
1519+
}
1520+
]
1521+
})
1522+
1523+
test_delta = deltacode.Delta(100, test_file_new, None)
1524+
1525+
utils.determine_license_diff(test_delta, unique_categories)
1526+
utils.determine_copyright_diff(test_delta)
1527+
1528+
expected_factors = [
1529+
'license info added',
1530+
'commercial added',
1531+
'copyleft added',
1532+
'copyright info added'
1533+
]
1534+
1535+
assert test_delta.score == 170
1536+
assert len(test_delta.factors) == 4
1537+
for factor in expected_factors:
1538+
assert factor in test_delta.factors
1539+
13551540
def test_align_trees_simple(self):
13561541
test_scan_new = self.get_test_loc('utils/align-trees-simple-new.json')
13571542
# Our old scan uses --full-root option in scancode

0 commit comments

Comments
 (0)