Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix license diffs to match new data format #192

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions src/deltacode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def create_deltas(
def determine_delta(self):
"""
Create Delta objects and append them to the list. Top Down BFS Traversal is used
to visit the codebase structures of the old and new Codebase Directiries.
to visit the codebase structures of the old and new Codebase Directories.
"""

old_resource_considered = set()
Expand Down Expand Up @@ -271,8 +271,14 @@ def license_diff(self):
]
)

license_refs = {}
for license_reference in self.codebase1.attributes.license_references:
license_refs[license_reference["key"]] = license_reference["category"]
for license_reference in self.codebase2.attributes.license_references:
license_refs[license_reference["key"]] = license_reference["category"]

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

def copyright_diff(self):
"""
Expand Down Expand Up @@ -380,15 +386,13 @@ def licenses_to_dict(self, file):
"""
licenses = []
try:
for license in file.licenses:
for license in file.license_detections:
licenses.append(
OrderedDict(
[
("key", license.get("key", None)),
("score", license.get("score", None)),
("short_name", license.get("short_name", None)),
("category", license.get("category", None)),
("owner", license.get("owner", None)),
("identifier", license.get("identifier", None)),
("license_expression", license.get("license_expression", None)),
("matches", license.get("matches", None)),
]
)
)
Expand Down
3 changes: 3 additions & 0 deletions src/deltacode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ def cli(new, old, json_file, all_delta_types):
deltacode = DeltaCode(new, old, options)
# generate JSON output
write_json(deltacode, json_file, all_delta_types)

if __name__ == "__main__":
cli()
58 changes: 55 additions & 3 deletions src/deltacode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,36 @@ class License(object):
"""
def __init__(self, dictionary={}):
self.key = dictionary.get('key')
self.score = dictionary.get('score')
self.language = dictionary.get('language')
self.short_name = dictionary.get('short_name')
self.name = dictionary.get('name')
self.category = dictionary.get('category')
self.owner = dictionary.get('owner')
self.homepage_url = dictionary.get('homepage_url')
self.notes = dictionary.get('notes')
self.is_builtin = dictionary.get('is_builtin')
self.is_exception = dictionary.get('is_exception')
self.is_unknown = dictionary.get('is_unknown')
self.is_generic = dictionary.get('is_generic')
self.spdx_license_key = dictionary.get('spdx_license_key')
self.other_spdx_license_keys = dictionary.get('other_spdx_license_keys')
self.osi_license_key = dictionary.get('osi_license_key')
self.text_urls = dictionary.get('text_urls')
self.osi_url = dictionary.get('osi_url')
self.faq_url = dictionary.get('faq_url')
self.other_urls = dictionary.get('other_urls')
self.key_aliases = dictionary.get('key_aliases')
self.minimum_coverage = dictionary.get('minimum_coverage')
self.standard_notice = dictionary.get('standard_notice')
self.ignorable_copyrights = dictionary.get('ignorable_copyrights')
self.ignorable_holders = dictionary.get('ignorable_holders')
self.ignorable_authors = dictionary.get('ignorable_authors')
self.ignorable_urls = dictionary.get('ignorable_urls')
self.ignorable_emails = dictionary.get('ignorable_emails')
self.text = dictionary.get('text')
self.scancode_url = dictionary.get('scancode_url')
self.licensedb_url = dictionary.get('licensedb_url')
self.spdx_url = dictionary.get('spdx_url')

def to_dict(self):
"""
Expand All @@ -260,10 +286,36 @@ def to_dict(self):
"""
d = OrderedDict([
('key', self.key),
('score', self.score),
('language', self.language),
('short_name', self.short_name),
('name', self.name),
('category', self.category),
('owner', self.owner)
('owner', self.owner),
('homepage_url', self.homepage_url),
('notes', self.notes),
('is_builtin', self.is_builtin),
('is_exception', self.is_exception),
('is_unknown', self.is_unknown),
('is_generic', self.is_generic),
('spdx_license_key', self.spdx_license_key),
('other_spdx_license_keys', self.other_spdx_license_keys),
('osi_license_key', self.osi_license_key),
('text_urls', self.text_urls),
('osi_url', self.osi_url),
('faq_url', self.faq_url),
('other_urls', self.other_urls),
('key_aliases', self.key_aliases),
('minimum_coverage', self.minimum_coverage),
('standard_notice', self.standard_notice),
('ignorable_copyrights', self.ignorable_copyrights),
('ignorable_holders', self.ignorable_holders),
('ignorable_authors', self.ignorable_authors),
('ignorable_urls', self.ignorable_urls),
('ignorable_emails', self.ignorable_emails),
('text', self.text),
('scancode_url', self.scancode_url),
('licensedb_url', self.licensedb_url),
('spdx_url', self.spdx_url)
])

return d
Expand Down
37 changes: 21 additions & 16 deletions src/deltacode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,33 @@
from collections import OrderedDict


def update_from_license_info(delta, unique_categories):
def update_from_license_info(delta, unique_categories, license_refs):
"""
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 delta.is_added():
update_added_from_license_info(delta, unique_categories)
update_added_from_license_info(delta, unique_categories, license_refs)

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


def update_added_from_license_info(delta, unique_categories):
def update_added_from_license_info(delta, unique_categories, license_refs):
"""
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 if hasattr(delta.new_file, "licenses") else []
delta.new_file.license_detections if hasattr(delta.new_file, "license_detections") else []
)

new_categories = set(license["category"] for license in new_licenses)
if hasattr(delta.new_file, "licenses"):
new_categories = set()
for license in new_licenses:
new_categories.add(license_refs.get(license["license_expression"], "N/A"))
if hasattr(delta.new_file, "license_detections"):
delta.update(20, "license info added")
for category in new_categories:
# no license ==> 'Copyleft Limited'or higher
Expand All @@ -72,26 +74,30 @@ def update_added_from_license_info(delta, unique_categories):
return


def update_modified_from_license_info(delta, unique_categories):
def update_modified_from_license_info(delta, unique_categories, license_refs):
"""
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.
"""

new_licenses = (
delta.new_file.licenses if hasattr(delta.new_file, "licenses") else []
delta.new_file.license_detections if hasattr(delta.new_file, "license_detections") else []
)
old_licenses = (
delta.old_file.licenses if hasattr(delta.old_file, "licenses") else []
delta.old_file.license_detections if hasattr(delta.old_file, "license_detections") else []
)

if not new_licenses and old_licenses:
delta.update(15, "license info removed")
return

new_categories = set(license.get("category", "") for license in new_licenses)
old_categories = set(license.get("category", "") for license in old_licenses)
new_categories = set()
for license in new_licenses:
new_categories.add(license_refs.get(license["license_expression"], "N/A"))
old_categories = set()
for license in old_licenses:
old_categories.add(license_refs.get(license["license_expression"], "N/A"))

if new_licenses and not old_licenses:
delta.update(20, "license info added")
Expand All @@ -105,11 +111,10 @@ def update_modified_from_license_info(delta, unique_categories):
delta.update(0, category.lower() + " added")
return

new_keys = set(license.get("key", "") for license in new_licenses)
old_keys = set(license.get("key", "") for license in old_licenses)

if new_keys != old_keys:
new_ids = set(license.get("identifier", "") for license in new_licenses)
old_ids = set(license.get("identifier", "") for license in old_licenses)

if new_ids != old_ids:
delta.update(10, "license change")
for category in new_categories - old_categories:
unique_categories_in_old_file = len(old_categories & unique_categories)
Expand Down
Loading