99
1010import attr
1111
12- from packagedcode .utils import combine_expressions
13- from plugincode .post_scan import PostScanPlugin
14- from plugincode .post_scan import post_scan_impl
1512from commoncode .cliutils import PluggableCommandLineOption
1613from commoncode .cliutils import POST_SCAN_GROUP
17-
1814from license_expression import Licensing
15+ from plugincode .post_scan import PostScanPlugin
16+ from plugincode .post_scan import post_scan_impl
1917
18+ from packagedcode .utils import combine_expressions
2019
2120# Tracing flags
2221TRACE = False
@@ -48,12 +47,14 @@ class LicenseClarityScore(PostScanPlugin):
4847 """
4948 Compute a License clarity score at the codebase level.
5049 """
50+
5151 codebase_attributes = dict (summary = attr .ib (default = attr .Factory (dict )))
5252
5353 sort_order = 5
5454
5555 options = [
56- PluggableCommandLineOption (('--license-clarity-score' ,),
56+ PluggableCommandLineOption (
57+ ('--license-clarity-score' ,),
5758 is_flag = True ,
5859 default = False ,
5960 help = 'Compute a summary license clarity score at the codebase level.' ,
@@ -75,7 +76,6 @@ def process_codebase(self, codebase, license_clarity_score, **kwargs):
7576 codebase .attributes .summary ['license_clarity_score' ] = scoring_elements .to_dict ()
7677
7778
78-
7979def compute_license_score (codebase ):
8080 """
8181 Return a mapping of scoring elements and a license clarity score computed at
@@ -124,12 +124,25 @@ def compute_license_score(codebase):
124124 """
125125
126126 scoring_elements = ScoringElements ()
127- declared_licenses = get_field_values_from_codebase_resources (codebase , 'licenses' , key_files_only = True )
128- declared_license_expressions = get_field_values_from_codebase_resources (codebase , 'license_expressions' , key_files_only = True )
127+ declared_licenses = get_field_values_from_codebase_resources (
128+ codebase = codebase ,
129+ field_name = 'licenses' ,
130+ key_files_only = True ,
131+ )
132+ declared_license_expressions = get_field_values_from_codebase_resources (
133+ codebase = codebase , field_name = 'license_expressions' , key_files_only = True
134+ )
135+
129136 unique_declared_license_expressions = unique (declared_license_expressions )
130137 declared_license_categories = get_license_categories (declared_licenses )
131- copyrights = get_field_values_from_codebase_resources (codebase , 'copyrights' , key_files_only = True )
132- other_licenses = get_field_values_from_codebase_resources (codebase , 'licenses' , key_files_only = False )
138+
139+ copyrights = get_field_values_from_codebase_resources (
140+ codebase = codebase , field_name = 'copyrights' , key_files_only = True
141+ )
142+
143+ other_licenses = get_field_values_from_codebase_resources (
144+ codebase = codebase , field_name = 'licenses' , key_files_only = False
145+ )
133146
134147 scoring_elements .declared_license = bool (declared_licenses )
135148 if scoring_elements .declared_license :
@@ -149,20 +162,23 @@ def compute_license_score(codebase):
149162
150163 is_permissively_licensed = check_declared_license_categories (declared_license_categories )
151164 if is_permissively_licensed :
152- scoring_elements .conflicting_license_categories = check_for_conflicting_licenses (other_licenses )
153- if (
154- scoring_elements .conflicting_license_categories
155- and scoring_elements .score > 0
156- ):
165+ scoring_elements .conflicting_license_categories = check_for_conflicting_licenses (
166+ other_licenses
167+ )
168+ if scoring_elements .conflicting_license_categories and scoring_elements .score > 0 :
157169 scoring_elements .score -= 20
158170
159171 declared_license_expression = get_primary_license (unique_declared_license_expressions )
160172
161173 if not declared_license_expression :
162174 # If we cannot get a single primary license, then we combine and simplify the license expressions from key files
163- combined_declared_license_expression = combine_expressions (unique_declared_license_expressions )
175+ combined_declared_license_expression = combine_expressions (
176+ unique_declared_license_expressions
177+ )
164178 if combined_declared_license_expression :
165- declared_license_expression = str (Licensing ().parse (combined_declared_license_expression ).simplify ())
179+ declared_license_expression = str (
180+ Licensing ().parse (combined_declared_license_expression ).simplify ()
181+ )
166182 scoring_elements .ambigous_compound_licensing = True
167183 if scoring_elements .score > 0 :
168184 scoring_elements .score -= 10
@@ -201,7 +217,7 @@ def to_dict(self):
201217 'has_license_text' : self .has_license_text ,
202218 'declared_copyrights' : self .declared_copyrights ,
203219 'conflicting_license_categories' : self .conflicting_license_categories ,
204- 'ambigous_compound_licensing' : self .ambigous_compound_licensing
220+ 'ambigous_compound_licensing' : self .ambigous_compound_licensing ,
205221 }
206222
207223
@@ -235,13 +251,15 @@ def is_good_license(detected_license):
235251 rule = detected_license ['matched_rule' ]
236252 coverage = rule .get ('match_coverage' ) or 0
237253 relevance = rule .get ('rule_relevance' ) or 0
238- match_types = dict ([
239- ('is_license_text' , rule ['is_license_text' ]),
240- ('is_license_notice' , rule ['is_license_notice' ]),
241- ('is_license_reference' , rule ['is_license_reference' ]),
242- ('is_license_tag' , rule ['is_license_tag' ]),
243- ('is_license_intro' , rule ['is_license_intro' ]),
244- ])
254+ match_types = dict (
255+ [
256+ ('is_license_text' , rule ['is_license_text' ]),
257+ ('is_license_notice' , rule ['is_license_notice' ]),
258+ ('is_license_reference' , rule ['is_license_reference' ]),
259+ ('is_license_tag' , rule ['is_license_tag' ]),
260+ ('is_license_intro' , rule ['is_license_intro' ]),
261+ ]
262+ )
245263 matched = False
246264 for match_type , mval in match_types .items ():
247265 if mval :
@@ -256,9 +274,11 @@ def is_good_license(detected_license):
256274 if score >= thresholds .min_score :
257275 return True
258276 else :
259- if (score >= thresholds .min_score
260- and coverage >= thresholds .min_coverage
261- and relevance >= thresholds .min_relevance ):
277+ if (
278+ score >= thresholds .min_score
279+ and coverage >= thresholds .min_coverage
280+ and relevance >= thresholds .min_relevance
281+ ):
262282 return True
263283
264284 return False
@@ -270,11 +290,7 @@ def check_declared_licenses(declared_licenses):
270290
271291 If so, return True. Otherwise, return False.
272292 """
273- return any (
274- is_good_license (declared_license )
275- for declared_license
276- in declared_licenses
277- )
293+ return any (is_good_license (declared_license ) for declared_license in declared_licenses )
278294
279295
280296def get_field_values_from_codebase_resources (codebase , field_name , key_files_only = False ):
@@ -324,10 +340,12 @@ def check_for_license_texts(declared_licenses):
324340 """
325341 for declared_license in declared_licenses :
326342 matched_rule = declared_license .get ('matched_rule' , {})
327- if any ([
328- matched_rule .get ('is_license_text' , False ),
329- matched_rule .get ('is_license_notice' , False ),
330- ]):
343+ if any (
344+ [
345+ matched_rule .get ('is_license_text' , False ),
346+ matched_rule .get ('is_license_notice' , False ),
347+ ]
348+ ):
331349 return True
332350 return False
333351
@@ -362,10 +380,7 @@ def check_for_conflicting_licenses(other_licenses):
362380 If so, return True. Otherwise, return False.
363381 """
364382 for license_info in other_licenses :
365- if (
366- license_info .get ('category' , '' )
367- in CONFLICTING_LICENSE_CATEGORIES
368- ):
383+ if license_info .get ('category' , '' ) in CONFLICTING_LICENSE_CATEGORIES :
369384 return True
370385 return False
371386
@@ -402,10 +417,7 @@ def group_license_expressions(unique_license_expressions):
402417 break
403418 for j1 in joined_expressions [i :]:
404419 if licensing .is_equivalent (j , j1 ):
405- if (
406- j not in unique_joined_expressions
407- and j not in seen_joined_expression
408- ):
420+ if j not in unique_joined_expressions and j not in seen_joined_expression :
409421 unique_joined_expressions .append (j )
410422 seen_joined_expression .append (j1 )
411423 else :
@@ -445,9 +457,7 @@ def get_primary_license(declared_license_expressions):
445457 # Group single expressions to joined expressions to see if single
446458 # expressions are accounted for in a joined expression
447459 single_expressions_by_joined_expressions = {
448- joined_expression : []
449- for joined_expression
450- in unique_joined_expressions
460+ joined_expression : [] for joined_expression in unique_joined_expressions
451461 }
452462 not_in_joined_expressions = []
453463 # Check to see if the single expression is in the joined expression
0 commit comments