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
17 changes: 12 additions & 5 deletions src/deltacode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def license_diff(self):
])

for delta in self.deltas:
utils.determine_license_diff(delta, unique_categories)
utils.update_from_license_info(delta, unique_categories)

def copyright_diff(self):
"""
Expand All @@ -215,7 +215,7 @@ def copyright_diff(self):
attribute -- if there has been a copyright change.
"""
for delta in self.deltas:
utils.determine_copyright_diff(delta)
utils.update_from_copyright_info(delta)

def index_deltas(self, index_key='path', delta_list=[]):
"""
Expand Down Expand Up @@ -277,9 +277,16 @@ def is_unmodified(self):
other than 'unmodified' and return True if all but 'unmodified' are
ruled out.
"""
if (self.old_file and self.new_file and
self.old_file.sha1 == self.new_file.sha1 and
self.old_file.path == self.new_file.path):
if (self.new_file and self.old_file and
self.new_file.sha1 == self.old_file.sha1 and
self.new_file.path == self.old_file.path):
return True

def is_added(self):
"""
Identify a Delta object reflecting the addition of a File.
"""
if self.new_file and not self.old_file:
return True

def to_dict(self):
Expand Down
69 changes: 59 additions & 10 deletions src/deltacode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,43 @@
from commoncode import paths


def determine_license_diff(delta, unique_categories):
def update_from_license_info(delta, unique_categories):
"""
Increase the Delta object's 'score' attribute and add one or more
appropriate categories to its 'factors' attribute if there has been a
license change and depending on the nature of that change.
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
one or more appropriate categories to its 'factors' attribute if there has
been a license change and depending on the nature of that change.
"""
if not delta.is_modified():
if delta.is_added():
update_added_from_license_info(delta, unique_categories)

if delta.is_modified():
update_modified_from_license_info(delta, unique_categories)


def update_added_from_license_info(delta, unique_categories):
"""
Increase an 'added' Delta object's 'score' attribute and add
one or more categories to its 'factors' attribute if there has
been a license change.
"""
new_licenses = delta.new_file.licenses or []
new_categories = set(license.category for license in new_licenses)

if delta.new_file.has_licenses():
delta.update(20, 'license info added')
# no license ==> 'Copyleft Limited'or higher
for category in new_categories:
if category in unique_categories:
delta.update(20, category.lower() + ' added')
return


def update_modified_from_license_info(delta, unique_categories):
"""
Increase a 'modified' Delta object's 'score' attribute and add
one or more categories to its 'factors' attribute if there has
been a license change.
"""
if not delta.new_file.has_licenses() and delta.old_file.has_licenses():
delta.update(15, 'license info removed')
return
Expand Down Expand Up @@ -71,15 +99,36 @@ def determine_license_diff(delta, unique_categories):
delta.update(20, category.lower() + ' added')


def determine_copyright_diff(delta):
def update_from_copyright_info(delta):
"""
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
one or more appropriate categories to its 'factors' attribute if there has
been a copyright change and depending on the nature of that change.
"""
if delta.is_added():
update_added_from_copyright_info(delta)

if delta.is_modified():
update_modified_from_copyright_info(delta)


def update_added_from_copyright_info(delta):
"""
Increase the Delta object's 'score' attribute and add one or more
appropriate categories to its 'factors' attribute if there has been a
copyright change and depending on the nature of that change.
Increase an 'added' Delta object's 'score' attribute and add
one or more categories to its 'factors' attribute if there has
been a copyright change.
"""
if not delta.is_modified():
if delta.new_file.has_copyrights():
delta.update(10, 'copyright info added')
return


def update_modified_from_copyright_info(delta):
"""
Increase a 'modified' Delta object's 'score' attribute and add
one or more categories to its 'factors' attribute if there has
been a copyright change.
"""
new_copyrights = delta.new_file.copyrights or []
old_copyrights = delta.old_file.copyrights or []

Expand Down
4 changes: 2 additions & 2 deletions tests/data/cli/1_file_moved_and_1_copy.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Factors,Score,Path,Name,Type,Size,Old Path
added,100,b/a4.py,a4.py,file,200,
added,100,b/a4_copy.py,a4_copy.py,file,200,
added license info added copyright info added,130,b/a4.py,a4.py,file,200,
added license info added copyright info added,130,b/a4_copy.py,a4_copy.py,file,200,
removed,0,a/a4.py,a4.py,file,200,
4 changes: 2 additions & 2 deletions tests/data/cli/1_file_moved_and_added.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Factors,Score,Path,Name,Type,Size,Old Path
added,100,b/a4.py,a4.py,file,200,
added,100,c/a4.py,a4.py,file,200,
added license info added copyright info added,130,b/a4.py,a4.py,file,200,
added license info added copyright info added,130,c/a4.py,a4.py,file,200,
removed,0,a/a4.py,a4.py,file,200,
2 changes: 1 addition & 1 deletion tests/data/cli/added1.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Factors,Score,Path,Name,Type,Size,Old Path
added,100,a/a5.py,a5.py,file,200,
added license info added copyright info added,130,a/a5.py,a5.py,file,200,
2 changes: 1 addition & 1 deletion tests/data/cli/renamed1.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Factors,Score,Path,Name,Type,Size,Old Path
added,100,a/a4_renamed_not_modified.py,a4_renamed_not_modified.py,file,200,
added license info added copyright info added,130,a/a4_renamed_not_modified.py,a4_renamed_not_modified.py,file,200,
removed,0,a/a4.py,a4.py,file,200,
2 changes: 1 addition & 1 deletion tests/test_deltacode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ def test_DeltaCode_sort_order(self):
deltas_object = deltacode_object.deltas

expected = [
['added'],
['added', 'license info added', 'copyright info added'],
['modified'],
['moved'],
['removed'],
Expand Down
Loading