Skip to content

Commit

Permalink
Merge pull request #108 from nexB/105-add-permissive-and-public-domai…
Browse files Browse the repository at this point in the history
…n-categories

Report all new license categories for add/modify #105
  • Loading branch information
steven-esser authored Apr 27, 2018
2 parents 436fadd + 5ab7d5a commit e9cdf8e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
21 changes: 18 additions & 3 deletions src/deltacode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ def update_added_from_license_info(delta, unique_categories):

if delta.new_file.has_licenses():
delta.update(20, 'license info added')
# no license ==> 'Copyleft Limited'or higher

for category in new_categories:
# no license ==> 'Copyleft Limited'or higher
if category in unique_categories:
delta.update(20, category.lower() + ' added')
# no license ==> 'Permissive' or 'Public Domain'
else:
delta.update(0, category.lower() + ' added')
return


Expand All @@ -82,10 +86,14 @@ def update_modified_from_license_info(delta, unique_categories):

if delta.new_file.has_licenses() and not delta.old_file.has_licenses():
delta.update(20, 'license info added')
# no license ==> 'Copyleft Limited'or higher

for category in new_categories:
# no license ==> 'Copyleft Limited'or higher
if category in unique_categories:
delta.update(20, category.lower() + ' added')
# no license ==> 'Permissive' or 'Public Domain'
else:
delta.update(0, category.lower() + ' added')
return

new_keys = set(license.key for license in new_licenses)
Expand All @@ -94,9 +102,16 @@ def update_modified_from_license_info(delta, unique_categories):
if new_keys != old_keys:
delta.update(10, 'license change')
for category in new_categories - old_categories:
unique_categories_in_old_file = len(old_categories & unique_categories)
# 'Permissive' or 'Public Domain' ==> 'Copyleft Limited' or higher
if len(old_categories & unique_categories) == 0 and category in unique_categories:
if unique_categories_in_old_file == 0 and category in unique_categories:
delta.update(20, category.lower() + ' added')
# at least 1 category in the old file was 'Copyleft Limited' or higher ==> 'Copyleft Limited' or higher
elif unique_categories_in_old_file != 0 and category in unique_categories:
delta.update(10, category.lower() + ' added')
# 'Permissive' or 'Public Domain' ==> 'Permissive' or 'Public Domain' if not in old_categories
elif category not in unique_categories:
delta.update(0, category.lower() + ' added')


def update_from_copyright_info(delta):
Expand Down
4 changes: 3 additions & 1 deletion tests/data/deltacode/score_old_no_lic_info_new.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"licenses": [
{
"key": "mit",
"score": 50.0
"score": 50.0,
"short_name": "MIT License",
"category": "Permissive"
}
]
}
Expand Down
23 changes: 12 additions & 11 deletions tests/test_deltacode.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,16 @@ def test_DeltaCode_license_modified(self):
deltas = result.deltas

assert len([i for i in deltas if i.score == 50]) == 1
assert len([i for i in deltas if i.score == 30]) == 1
assert len([i for i in deltas if i.score == 40]) == 1
assert len([i for i in deltas if i.score == 30]) == 0
assert len([i for i in deltas if i.score == 20]) == 1

assert [d.score for d in deltas if d.new_file.path == 'some/path/a/a1.py'] == [50]
assert [d.score for d in deltas if d.new_file.path == 'some/path/b/b1.py'] == [30]
assert [d.score for d in deltas if d.new_file.path == 'some/path/b/b1.py'] == [40]
assert [d.score for d in deltas if d.new_file.path == 'some/path/c/c1.py'] == [20]

assert [d.factors for d in deltas if d.new_file.path == 'some/path/a/a1.py'].pop() == ['modified', 'license change', 'copyleft added']
assert [d.factors for d in deltas if d.new_file.path == 'some/path/b/b1.py'].pop() == ['modified', 'license change']
assert [d.factors for d in deltas if d.new_file.path == 'some/path/b/b1.py'].pop() == ['modified', 'license change', 'copyleft added']
assert [d.factors for d in deltas if d.new_file.path == 'some/path/c/c1.py'].pop() == ['modified']

def test_DeltaCode_errors_empty(self):
Expand Down Expand Up @@ -611,14 +612,14 @@ def test_score_old_no_lic_info(self):
assert [d.new_file.sha1 for d in deltas_object if d.new_file.path == 'path.txt'] == ['b_modified']

assert [d.score for d in deltas_object if d.new_file.path == 'path.txt'] == [40]
assert [d.factors for d in deltas_object if d.new_file.path == 'path.txt'].pop() == ['modified', 'license info added']
assert [d.factors for d in deltas_object if d.new_file.path == 'path.txt'].pop() == ['modified', 'license info added', 'permissive added']
assert [d.to_dict().get('old').get('licenses') for d in deltas_object if d.new_file.path == 'path.txt'].pop() == []
assert [d.to_dict().get('new').get('licenses') for d in deltas_object if d.new_file.path == 'path.txt'].pop() == [
OrderedDict([
('key', 'mit'),
('score', 50.0),
('short_name', None),
('category', None),
('short_name', 'MIT License'),
('category', 'Permissive'),
('owner', None)
])
]
Expand Down Expand Up @@ -1463,7 +1464,7 @@ def test_DeltaCode_sort_order(self):
deltas_object = deltacode_object.deltas

expected = [
['added', 'license info added', 'copyright info added'],
['added', 'license info added', 'permissive added', 'copyright info added'],
['modified'],
['moved'],
['removed'],
Expand Down Expand Up @@ -1502,7 +1503,7 @@ def test_DeltaCode_no_lic_to_all_notable_lic(self):
assert [d.score for d in deltas_object if d.new_file.path == 'a1.py'] == [170]
assert [d.score for d in deltas_object if d.new_file.path == 'a2.py'] == [0]

assert sorted([d.factors for d in deltas_object if d.new_file.path == 'a1.py'].pop()) == sorted(['modified', 'license info added', 'commercial added', 'copyleft added', 'copyleft limited added', 'free restricted added', 'patent license added', 'proprietary free added', 'copyright info added'])
assert sorted([d.factors for d in deltas_object if d.new_file.path == 'a1.py'].pop()) == sorted(['modified', 'license info added', 'commercial added', 'copyleft added', 'copyleft limited added', 'free restricted added', 'patent license added', 'permissive added', 'proprietary free added', 'copyright info added'])
assert [d.factors for d in deltas_object if d.new_file.path == 'a2.py'].pop() == ['unmodified']

def test_DeltaCode_apache_to_all_notable_lic(self):
Expand Down Expand Up @@ -1535,10 +1536,10 @@ def test_DeltaCode_copyleft_etc_to_prop_free_and_commercial(self):

deltas_object = deltacode_object.deltas

assert [d.score for d in deltas_object if d.new_file.path == 'a1.py'] == [30]
assert [d.score for d in deltas_object if d.new_file.path == 'a1.py'] == [50]
assert [d.score for d in deltas_object if d.new_file.path == 'a2.py'] == [0]

assert [d.factors for d in deltas_object if d.new_file.path == 'a1.py'].pop() == ['modified', 'license change']
assert [d.factors for d in deltas_object if d.new_file.path == 'a1.py'].pop() == ['modified', 'license change', 'commercial added', 'proprietary free added']
assert [d.factors for d in deltas_object if d.new_file.path == 'a2.py'].pop() == ['unmodified']

def test_DeltaCode_permissive_add_public_domain(self):
Expand All @@ -1556,5 +1557,5 @@ def test_DeltaCode_permissive_add_public_domain(self):
assert [d.score for d in deltas_object if d.new_file.path == 'a1.py'] == [30]
assert [d.score for d in deltas_object if d.new_file.path == 'a2.py'] == [0]

assert [d.factors for d in deltas_object if d.new_file.path == 'a1.py'].pop() == ['modified', 'license change']
assert [d.factors for d in deltas_object if d.new_file.path == 'a1.py'].pop() == ['modified', 'license change', 'public domain added']
assert [d.factors for d in deltas_object if d.new_file.path == 'a2.py'].pop() == ['unmodified']
15 changes: 10 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ def test_update_from_license_info_permissive_license_info_added(self):
utils.update_modified_from_license_info(test_delta, unique_categories)

assert test_delta.score == 40
assert len(test_delta.factors) == 1
assert len(test_delta.factors) == 2
assert 'license info added' in test_delta.factors
assert 'permissive added' in test_delta.factors

def test_update_from_license_info_permissive_license_info_removed(self):
test_file_new = models.File({
Expand Down Expand Up @@ -650,9 +651,11 @@ def test_update_from_license_info_copyleft_to_copyleft_limited(self):

utils.update_modified_from_license_info(test_delta, unique_categories)

assert test_delta.score == 30
assert len(test_delta.factors) == 1
assert test_delta.score == 40
assert len(test_delta.factors) == 2
assert 'license change' in test_delta.factors
assert 'copyleft limited added' in test_delta.factors


def test_update_from_license_info_file_added_permissive_license(self):
test_file_new = models.File({
Expand All @@ -675,9 +678,10 @@ def test_update_from_license_info_file_added_permissive_license(self):
utils.update_added_from_license_info(test_delta, unique_categories)

assert test_delta.score == 120
assert len(test_delta.factors) == 1
assert len(test_delta.factors) == 2

assert 'license info added' in test_delta.factors
assert 'permissive added' in test_delta.factors

def test_update_from_license_info_file_added_commercial_and_copyleft_licenses(self):
test_file_new = models.File({
Expand Down Expand Up @@ -1478,11 +1482,12 @@ def test_update_from_lic_copy_info_file_added_copyright_and_permissive_license(s

expected_factors = [
'license info added',
'permissive added',
'copyright info added'
]

assert test_delta.score == 130
assert len(test_delta.factors) == 2
assert len(test_delta.factors) == 3
for factor in expected_factors:
assert factor in test_delta.factors

Expand Down

0 comments on commit e9cdf8e

Please sign in to comment.