Skip to content

Commit 5189fc1

Browse files
authored
Merge pull request #1037 from nexB/update-license-policy
Update License Policy Plugin
2 parents 833c138 + 4e37b29 commit 5189fc1

24 files changed

+391
-985
lines changed

src/scancode/plugin_license_policy.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from __future__ import print_function
2828
from __future__ import unicode_literals
2929

30+
from collections import OrderedDict
3031
from os.path import abspath
3132
from os.path import dirname
3233
from os.path import exists
@@ -57,7 +58,7 @@ class LicensePolicy(PostScanPlugin):
5758
CommandLineOption(('--license-policy',),
5859
multiple=False,
5960
metavar='FILE',
60-
help='Load and a License Policy file and apply it to the codebase at the '
61+
help='Load a License Policy file and apply it to the scan at the '
6162
'Resource level.',
6263
help_group=POST_SCAN_GROUP)
6364
]
@@ -67,33 +68,62 @@ def is_enabled(self, license_policy, **kwargs):
6768

6869
def process_codebase(self, codebase, license_policy, **kwargs):
6970
"""
70-
Populate a license_policy mapping with three attributes: label, icon,
71-
and color_code at the File Resource level.
71+
Populate a license_policy mapping with four attributes: license_key, label,
72+
icon, and color_code at the File Resource level.
7273
"""
7374
if not self.is_enabled(license_policy):
7475
return
76+
77+
if has_policy_duplicates(license_policy):
78+
codebase.errors.append('ERROR: License Policy file contains duplicate entries.\n')
79+
return
7580

76-
# load a dictionary of license_policies from a file
77-
license_policies = load_license_policy(license_policy).get('license_policies')
81+
# get a list of unique license policies from the license_policy file
82+
policies = load_license_policy(license_policy).get('license_policies', [])
7883

7984
# apply policy to Resources if they contain an offending license
8085
for resource in codebase.walk(topdown=True):
8186
if not resource.is_file:
8287
continue
8388

8489
try:
85-
resource_license_keys = [entry.get('key') for entry in resource.licenses]
86-
87-
for key in resource_license_keys:
88-
if key in license_policies.keys():
89-
# Apply the policy to the Resource
90-
resource.license_policy = license_policies[key]
91-
codebase.save_resource(resource)
90+
resource_license_keys = set([entry.get('key') for entry in resource.licenses])
9291

9392
except AttributeError:
94-
# add license_policy regardless if there is --license info or not
93+
# add license_policy regardless if there is license info or not
9594
resource.license_policy = {}
9695
codebase.save_resource(resource)
96+
continue
97+
98+
for key in resource_license_keys:
99+
for policy in policies:
100+
if key == policy.get('license_key'):
101+
# Apply the policy to the Resource
102+
resource.license_policy = policy
103+
codebase.save_resource(resource)
104+
105+
106+
def has_policy_duplicates(license_policy_location):
107+
"""
108+
Returns True if the policy file contains duplicate entries for a specific license
109+
key. Returns False otherwise.
110+
"""
111+
policies = load_license_policy(license_policy_location).get('license_policies', [])
112+
113+
unique_policies = OrderedDict()
114+
115+
if policies == []:
116+
return False
117+
118+
for policy in policies:
119+
license_key = policy.get('license_key')
120+
121+
if license_key in unique_policies.keys():
122+
return True
123+
else:
124+
unique_policies[license_key] = policy
125+
126+
return False
97127

98128

99129
def load_license_policy(license_policy_location):

tests/scancode/data/help/help.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Options:
7575
post-scan:
7676
--copyrights-summary Summarize copyrights, holders and authors at the file
7777
and directory level.
78-
--license-policy FILE Load and a License Policy file and apply it to the
79-
codebase at the Resource level.
78+
--license-policy FILE Load a License Policy file and apply it to the scan at
79+
the Resource level.
8080
--mark-source Set the "is_source" to true for directories that
8181
contain over 90% of source files as children and
8282
descendants. Count the number of source files in a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
license_policies: []
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
license_policies:
2+
- license_key: broadcom-commercial
3+
label: Restricted License
4+
color_code: '#FFcc33'
5+
icon: icon-warning-sign
6+
- license_key: bsd-1988
7+
label: Approved License
8+
color_code: '#008000'
9+
icon: icon-ok-circle
10+
- license_key: esri-devkit
11+
label: Restricted License
12+
color_code: '#FFcc33'
13+
icon: icon-warning-sign
14+
- license_key: oracle-java-ee-sdk-2010
15+
label: Restricted License
16+
color_code: '#FFcc33'
17+
icon: icon-warning-sign
18+
- license_key: rh-eula
19+
label: Restricted License
20+
color_code: '#FFcc33'
21+
icon: icon-warning-sign
22+
- license_key: broadcom-commercial
23+
label: Approved License
24+
color_code: '#008000'
25+
icon: icon-ok-circle

tests/scancode/data/plugin_license_policy/license_policies_invalid.yml renamed to tests/scancode/data/plugin_license_policy/has_policy_duplicates_invalid_no_dupes.yml

File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
license_policies:
2+
- license_key: broadcom-commercial
3+
label: Restricted License
4+
color_code: '#FFcc33'
5+
icon: icon-warning-sign
6+
- license_key: bsd-1988
7+
label: Approved License
8+
color_code: '#008000'
9+
icon: icon-ok-circle
10+
- license_key: esri-devkit
11+
label: Restricted License
12+
color_code: '#FFcc33'
13+
icon: icon-warning-sign
14+
- license_key: oracle-java-ee-sdk-2010
15+
label: Restricted License
16+
color_code: '#FFcc33'
17+
icon: icon-warning-sign
18+
- license_key: rh-eula
19+
label: Restricted License
20+
color_code: '#FFcc33'
21+
icon: icon-warning-sign

0 commit comments

Comments
 (0)