Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
55 changes: 55 additions & 0 deletions src/licensedcode/plugin_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import attr

from commoncode import fileutils
from commoncode.cliutils import PluggableCommandLineOption
from plugincode.scan import ScanPlugin
from plugincode.scan import scan_impl
Expand Down Expand Up @@ -116,3 +117,57 @@ def get_scanner(
license_text_diagnostics=license_text_diagnostics,
license_url_template=license_url_template
)

def process_codebase(self, codebase, **kwargs):
Comment thread
akugarg marked this conversation as resolved.

if codebase.has_single_resource:
return
Comment thread
pombredanne marked this conversation as resolved.

Comment thread
akugarg marked this conversation as resolved.
for resource in codebase.walk(topdown=False):
match_reference_license(resource,codebase)


def match_reference_license(resource, codebase):
Comment thread
akugarg marked this conversation as resolved.
"""
Return the updated license matches having reference to another files
Comment thread
akugarg marked this conversation as resolved.
Outdated
"""
Comment thread
akugarg marked this conversation as resolved.
if not resource.is_file:
return

licenses = resource.licenses
license_expressions = resource.license_expressions
if not licenses:
return

referenced_licenses = []
referenced_license_expressions = []
referenced_filenames = []

for license in licenses:
referenced_files = license['matched_rule']['referenced_filenames']
for referenced_filename in referenced_files:
if not referenced_filename in referenced_filenames:
referenced_filenames.append(referenced_filename)

for referenced_filename in referenced_filenames:
new_resource = find_reference_licenses(referenced_filename=referenced_filename, resource=resource, codebase=codebase)
if new_resource:
referenced_licenses.extend(new_resource.licenses)
referenced_license_expressions.extend(new_resource.license_expressions)

licenses.extend(referenced_licenses)
license_expressions.extend(referenced_license_expressions)
codebase.save_resource(resource)
Comment thread
akugarg marked this conversation as resolved.
Outdated
return resource


def find_reference_licenses(referenced_filename, resource, codebase, **kwargs):
"""
Return the resource object for matching referenced-filename given a resource in codebase
Comment thread
akugarg marked this conversation as resolved.
Outdated
"""
parent = resource.parent(codebase)

for child in parent.children(codebase):
path = child.path
if path.endswith(referenced_filename) or fileutils.file_base_name(child.path) == referenced_filename:
return child
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"headers": [
{
"tool_name": "scancode-toolkit",
"options": {
"input": "<path>",
"--json": "<file>",
"--license": true,
"--license-text": true,
"--license-text-diagnostics": true,
"--strip-root": true
},
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
"message": null,
"errors": [],
"extra_data": {
"files_count": 2
}
}
],
"files": [
{
"path": "LICENSE",
"type": "file",
"licenses": [
{
"key": "mit",
"score": 100.0,
"name": "MIT License",
"short_name": "MIT License",
"category": "Permissive",
"is_exception": false,
"is_unknown": false,
"owner": "MIT",
"homepage_url": "http://opensource.org/licenses/mit-license.php",
"text_url": "http://opensource.org/licenses/mit-license.php",
"reference_url": "https://scancode-licensedb.aboutcode.org/mit",
"scancode_text_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/mit.LICENSE",
"scancode_data_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/mit.yml",
"spdx_license_key": "MIT",
"spdx_url": "https://spdx.org/licenses/MIT",
"start_line": 1,
"end_line": 1,
"matched_rule": {
"identifier": "mit_66.RULE",
"license_expression": "mit",
"licenses": [
"mit"
],
"referenced_filenames": [],
"is_license_text": false,
"is_license_notice": true,
"is_license_reference": false,
"is_license_tag": false,
"is_license_intro": false,
"has_unknown": false,
"matcher": "1-hash",
"rule_length": 10,
"matched_length": 10,
"match_coverage": 100.0,
"rule_relevance": 100
},
"matched_text": "that is licensed under [MIT](http://opensource.org/licenses/MIT)."
}
],
"license_expressions": [
"mit"
],
"percentage_of_license_text": 100.0,
"scan_errors": []
},
{
"path": "license-notice.txt",
"type": "file",
"licenses": [
{
"key": "unknown-license-reference",
"score": 27.0,
"name": "Unknown License file reference",
"short_name": "Unknown License reference",
"category": "Unstated License",
"is_exception": false,
"is_unknown": true,
"owner": "Unspecified",
"homepage_url": null,
"text_url": "",
"reference_url": "https://scancode-licensedb.aboutcode.org/unknown-license-reference",
"scancode_text_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/unknown-license-reference.LICENSE",
"scancode_data_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/unknown-license-reference.yml",
"spdx_license_key": "LicenseRef-scancode-unknown-license-reference",
"spdx_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/unknown-license-reference.LICENSE",
"start_line": 34,
"end_line": 34,
"matched_rule": {
"identifier": "unknown-license-reference_25.RULE",
"license_expression": "unknown-license-reference",
"licenses": [
"unknown-license-reference"
],
"referenced_filenames": [
"LICENSE"
],
"is_license_text": false,
"is_license_notice": false,
"is_license_reference": false,
"is_license_tag": true,
"is_license_intro": false,
"has_unknown": true,
"matcher": "2-aho",
"rule_length": 5,
"matched_length": 5,
"match_coverage": 100.0,
"rule_relevance": 27
},
"matched_text": "license\": \"SEE LICENSE IN LICENSE."
},
{
"key": "mit",
"score": 100.0,
"name": "MIT License",
"short_name": "MIT License",
"category": "Permissive",
"is_exception": false,
"is_unknown": false,
"owner": "MIT",
"homepage_url": "http://opensource.org/licenses/mit-license.php",
"text_url": "http://opensource.org/licenses/mit-license.php",
"reference_url": "https://scancode-licensedb.aboutcode.org/mit",
"scancode_text_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/mit.LICENSE",
"scancode_data_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/mit.yml",
"spdx_license_key": "MIT",
"spdx_url": "https://spdx.org/licenses/MIT",
"start_line": 1,
"end_line": 1,
"matched_rule": {
"identifier": "mit_66.RULE",
"license_expression": "mit",
"licenses": [
"mit"
],
"referenced_filenames": [],
"is_license_text": false,
"is_license_notice": true,
"is_license_reference": false,
"is_license_tag": false,
"is_license_intro": false,
"has_unknown": false,
"matcher": "1-hash",
"rule_length": 10,
"matched_length": 10,
"match_coverage": 100.0,
"rule_relevance": 100
},
"matched_text": "that is licensed under [MIT](http://opensource.org/licenses/MIT)."
}
],
"license_expressions": [
"unknown-license-reference",
"mit"
],
"percentage_of_license_text": 0.2,
"scan_errors": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"headers": [
{
"tool_name": "scancode-toolkit",
"options": {
"input": "<path>",
"--json": "<file>",
"--license": true,
"--license-text": true,
"--license-text-diagnostics": true,
"--strip-root": true
},
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
"message": null,
"errors": [],
"extra_data": {
"files_count": 1
}
}
],
"files": [
{
"path": "license-notice.txt",
"type": "file",
"licenses": [
{
"key": "unknown-license-reference",
"score": 27.0,
"name": "Unknown License file reference",
"short_name": "Unknown License reference",
"category": "Unstated License",
"is_exception": false,
"is_unknown": true,
"owner": "Unspecified",
"homepage_url": null,
"text_url": "",
"reference_url": "https://scancode-licensedb.aboutcode.org/unknown-license-reference",
"scancode_text_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/unknown-license-reference.LICENSE",
"scancode_data_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/unknown-license-reference.yml",
"spdx_license_key": "LicenseRef-scancode-unknown-license-reference",
"spdx_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/unknown-license-reference.LICENSE",
"start_line": 34,
"end_line": 34,
"matched_rule": {
"identifier": "unknown-license-reference_25.RULE",
"license_expression": "unknown-license-reference",
"licenses": [
"unknown-license-reference"
],
"referenced_filenames": [
"LICENSE"
],
"is_license_text": false,
"is_license_notice": false,
"is_license_reference": false,
"is_license_tag": true,
"is_license_intro": false,
"has_unknown": true,
"matcher": "2-aho",
"rule_length": 5,
"matched_length": 5,
"match_coverage": 100.0,
"rule_relevance": 27
},
"matched_text": "license\": \"SEE LICENSE IN LICENSE."
}
],
"license_expressions": [
"unknown-license-reference"
],
"percentage_of_license_text": 0.2,
"scan_errors": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
that is licensed under [MIT](http://opensource.org/licenses/MIT).
Loading