2727from __future__ import print_function
2828from __future__ import unicode_literals
2929
30+ from collections import OrderedDict
3031from os .path import abspath
3132from os .path import dirname
3233from 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
99129def load_license_policy (license_policy_location ):
0 commit comments