Skip to content

Commit 7432324

Browse files
authored
Merge pull request #92 from nexB/84-improve-scoring-for-added
Add license/copyright score for 'added' files #84
2 parents bcfd9be + b0dc286 commit 7432324

8 files changed

Lines changed: 335 additions & 94 deletions

File tree

src/deltacode/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def license_diff(self):
204204
])
205205

206206
for delta in self.deltas:
207-
utils.determine_license_diff(delta, unique_categories)
207+
utils.update_from_license_info(delta, unique_categories)
208208

209209
def copyright_diff(self):
210210
"""
@@ -215,7 +215,7 @@ def copyright_diff(self):
215215
attribute -- if there has been a copyright change.
216216
"""
217217
for delta in self.deltas:
218-
utils.determine_copyright_diff(delta)
218+
utils.update_from_copyright_info(delta)
219219

220220
def index_deltas(self, index_key='path', delta_list=[]):
221221
"""
@@ -277,9 +277,16 @@ def is_unmodified(self):
277277
other than 'unmodified' and return True if all but 'unmodified' are
278278
ruled out.
279279
"""
280-
if (self.old_file and self.new_file and
281-
self.old_file.sha1 == self.new_file.sha1 and
282-
self.old_file.path == self.new_file.path):
280+
if (self.new_file and self.old_file and
281+
self.new_file.sha1 == self.old_file.sha1 and
282+
self.new_file.path == self.old_file.path):
283+
return True
284+
285+
def is_added(self):
286+
"""
287+
Identify a Delta object reflecting the addition of a File.
288+
"""
289+
if self.new_file and not self.old_file:
283290
return True
284291

285292
def to_dict(self):

src/deltacode/utils.py

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,43 @@
3333
from commoncode import paths
3434

3535

36-
def determine_license_diff(delta, unique_categories):
36+
def update_from_license_info(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.
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.
4141
"""
42-
if not delta.is_modified():
42+
if delta.is_added():
43+
update_added_from_license_info(delta, unique_categories)
44+
45+
if delta.is_modified():
46+
update_modified_from_license_info(delta, unique_categories)
47+
48+
49+
def update_added_from_license_info(delta, unique_categories):
50+
"""
51+
Increase an 'added' Delta object's 'score' attribute and add
52+
one or more categories to its 'factors' attribute if there has
53+
been a license change.
54+
"""
55+
new_licenses = delta.new_file.licenses or []
56+
new_categories = set(license.category for license in new_licenses)
57+
58+
if delta.new_file.has_licenses():
59+
delta.update(20, 'license info added')
60+
# no license ==> 'Copyleft Limited'or higher
61+
for category in new_categories:
62+
if category in unique_categories:
63+
delta.update(20, category.lower() + ' added')
4364
return
4465

66+
67+
def update_modified_from_license_info(delta, unique_categories):
68+
"""
69+
Increase a 'modified' Delta object's 'score' attribute and add
70+
one or more categories to its 'factors' attribute if there has
71+
been a license change.
72+
"""
4573
if not delta.new_file.has_licenses() and delta.old_file.has_licenses():
4674
delta.update(15, 'license info removed')
4775
return
@@ -71,15 +99,36 @@ def determine_license_diff(delta, unique_categories):
7199
delta.update(20, category.lower() + ' added')
72100

73101

74-
def determine_copyright_diff(delta):
102+
def update_from_copyright_info(delta):
103+
"""
104+
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
105+
one or more appropriate categories to its 'factors' attribute if there has
106+
been a copyright change and depending on the nature of that change.
107+
"""
108+
if delta.is_added():
109+
update_added_from_copyright_info(delta)
110+
111+
if delta.is_modified():
112+
update_modified_from_copyright_info(delta)
113+
114+
115+
def update_added_from_copyright_info(delta):
75116
"""
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.
117+
Increase an 'added' Delta object's 'score' attribute and add
118+
one or more categories to its 'factors' attribute if there has
119+
been a copyright change.
79120
"""
80-
if not delta.is_modified():
121+
if delta.new_file.has_copyrights():
122+
delta.update(10, 'copyright info added')
81123
return
82124

125+
126+
def update_modified_from_copyright_info(delta):
127+
"""
128+
Increase a 'modified' Delta object's 'score' attribute and add
129+
one or more categories to its 'factors' attribute if there has
130+
been a copyright change.
131+
"""
83132
new_copyrights = delta.new_file.copyrights or []
84133
old_copyrights = delta.old_file.copyrights or []
85134

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'],

0 commit comments

Comments
 (0)