-
-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathplugin_license_policy.py
More file actions
142 lines (106 loc) · 4.32 KB
/
Copy pathplugin_license_policy.py
File metadata and controls
142 lines (106 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from os.path import exists
from os.path import isdir
import attr
import os
import logging
import saneyaml
from plugincode.post_scan import PostScanPlugin
from plugincode.post_scan import post_scan_impl
from commoncode.cliutils import PluggableCommandLineOption
from commoncode.cliutils import POST_SCAN_GROUP
from licensedcode.detection import get_license_keys_from_detections
TRACE = os.environ.get('SCANCODE_DEBUG_LICENSE_POLICY', False)
def logger_debug(*args):
pass
if TRACE:
logger = logging.getLogger(__name__)
import sys
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))
@post_scan_impl
class LicensePolicy(PostScanPlugin):
"""
Add the "license_policy" attribute to a resouce if it contains a
detected license key that is found in the license_policy.yml file
"""
resource_attributes = dict(license_policy=attr.ib(default=attr.Factory(list)))
run_order = 9
sort_order = 9
options = [
PluggableCommandLineOption(('--license-policy',),
multiple=False,
metavar='FILE',
help='Load a License Policy file and apply it to the scan at the '
'Resource level.',
help_group=POST_SCAN_GROUP)
]
def is_enabled(self, license_policy, **kwargs):
return license_policy
def process_codebase(self, codebase, license_policy, **kwargs):
"""
Populate a license_policy mapping with four attributes: license_key, label,
icon, and color_code at the File Resource level.
"""
if not self.is_enabled(license_policy):
return
if has_policy_duplicates(license_policy):
codebase.errors.append('ERROR: License Policy file contains duplicate entries.\n')
return
# get a list of unique license policies from the license_policy file
policies = load_license_policy(license_policy).get('license_policies', [])
# apply policy to Resources if they contain an offending license
for resource in codebase.walk(topdown=True):
if not resource.is_file:
continue
try:
resource_license_keys = get_license_keys_from_detections(resource.license_detections)
except AttributeError:
# add license_policy regardless if there is license info or not
resource.license_policy = []
codebase.save_resource(resource)
continue
license_policies = []
for key in resource_license_keys:
for policy in policies:
if key == policy.get('license_key'):
# Apply the policy to the Resource
license_policies.append(policy)
resource.license_policy = sorted(license_policies, key=lambda d: d['license_key'])
codebase.save_resource(resource)
def has_policy_duplicates(license_policy_location):
"""
Returns True if the policy file contains duplicate entries for a specific license
key. Returns False otherwise.
"""
policies = load_license_policy(license_policy_location).get('license_policies', [])
unique_policies = {}
if policies == []:
return False
for policy in policies:
license_key = policy.get('license_key')
if license_key in unique_policies.keys():
return True
else:
unique_policies[license_key] = policy
return False
def load_license_policy(license_policy_location):
"""
Return a license_policy dictionary loaded from a license policy file.
"""
if not license_policy_location or not exists(license_policy_location):
return {}
elif isdir(license_policy_location):
return {}
with open(license_policy_location, 'r') as conf:
conf_content = conf.read()
return saneyaml.load(conf_content)