@@ -150,61 +150,80 @@ def compute_normalized_license(self):
150150 return compute_normalized_license (self .declared_license )
151151
152152
153- def compute_normalized_license (listed_license_dictionary ):
153+ def compute_normalized_license (declared_license ):
154154 """
155155 Return a detected license expression from a declared license mapping.
156156 """
157- if listed_license_dictionary :
158- licensing = Licensing ()
159- # Use set instead of list to avoid duplication.
160- detected_licenses = set ()
161- for license_declaration in listed_license_dictionary :
162- name = license_declaration .get ('name' )
163- url = license_declaration .get ('url' )
164- comments = license_declaration .get ('comments' )
165- # 1. try detection on the value of name if not empty and keep this
166- # 2. try detection on the value of url if not empty and keep this
167- # 3. try detection on the value of comment if not empty and keep this
168- # 4. if the three detection are for the same license, this becomes the kept license for that one licenses item
169- # 5. if not, the name should have precedence and any unknowns
157+ if not declared_license :
158+ return
159+
160+ licensing = Licensing ()
161+
162+ detected_licenses = []
163+
164+ for license_declaration in declared_license :
165+ # 1. try detection on the value of name if not empty and keep this
166+ name = license_declaration .get ('name' )
167+ via_name = models .compute_normalized_license (name )
168+
169+ # 2. try detection on the value of url if not empty and keep this
170+ url = license_declaration .get ('url' )
171+ via_url = models .compute_normalized_license (url )
172+
173+ # 3. try detection on the value of comment if not empty and keep this
174+ comments = license_declaration .get ('comments' )
175+ via_comments = models .compute_normalized_license (comments )
176+
177+
178+ if via_name :
179+ # The name should have precedence and any unknowns
170180 # in url and comment should be ignored.
171- via_name = models .compute_normalized_license (name )
172- via_url = models .compute_normalized_license (url )
173- via_comments = models .compute_normalized_license (comments )
174-
175- if via_name :
176- # The name should have precedence and any unknowns
177- # in url and comment should be ignored.
178- if via_url == 'unknown' :
179- via_url = None
180- if via_comments == 'unknown' :
181- via_comments = None
182-
183- if via_name and ((via_name == via_url and via_comments == via_url ) or (via_name == via_url and not via_comments ) or (via_name == via_comments and not via_url )):
184- # if three detection are the same and not empty, return the value
185- # or one of url or comments is empty and the non-empty one equals to the name value
186- detected_licenses .add (via_name )
181+ if via_url == 'unknown' :
182+ via_url = None
183+ if via_comments == 'unknown' :
184+ via_comments = None
185+
186+ # Check the three detections to decide which license to keep
187+ name_and_url = via_name == via_url
188+ name_and_comment = via_name == via_comments
189+ all_same = name_and_url and name_and_comment
190+
191+ if via_name :
192+ if all_same :
193+ detected_licenses .append (via_name )
194+
195+ # name and (url or comment) are same
196+ elif name_and_url and not via_comments :
197+ detected_licenses .append (via_name )
198+ elif name_and_comment and not via_url :
199+ detected_licenses .append (via_name )
200+
187201 else :
188- # Form a list and the element does not contain any None value, since the None value means 'unknown' or real empty value from above assignment.
189- detected_items = [item for item in (via_name , via_url , via_comments ) if item ]
190- if detected_items :
191- if len (detected_items ) == 1 :
192- detected_licenses .add (detected_items [0 ])
202+ # we have some non-unknown license detected in url or comment
203+ detections = via_name , via_url , via_comments
204+ detections = [l for l in detections if l ]
205+ if detections :
206+ if len (detections ) == 1 :
207+ combined_expression = detections [0 ]
193208 else :
194- # Combine if name, url and comments are different licenses
195- licensing = Licensing ()
196- total_license_expression = [licensing .parse (detected_item , simple = True ) for detected_item in detected_items ]
197- combined_expression_object = licensing .AND (* total_license_expression )
198- detected_licenses .add (str (combined_expression_object ))
199- if detected_licenses :
200- if len (detected_licenses ) == 1 :
201- return str (detected_licenses .pop ())
202- else :
203- # Combine if pom contains more than 1 licenses declarations.
204- licensing = Licensing ()
205- total_license_expression = [licensing .parse (detected_license , simple = True ) for detected_license in detected_licenses ]
206- combined_expression_object = licensing .AND (* total_license_expression )
207- return str (combined_expression_object )
209+ expressions = [
210+ licensing .parse (le , simple = True ) for le in detections ]
211+ combined_expression = str (licensing .AND (* expressions ))
212+ detected_licenses .append (combined_expression )
213+
214+ elif via_url :
215+ detected_licenses .append (via_url )
216+ elif via_comments :
217+ detected_licenses .append (via_comments )
218+
219+ if len (detected_licenses ) == 1 :
220+ return detected_licenses [0 ]
221+
222+ if detected_licenses :
223+ # Combine if pom contains more than one licenses declarations.
224+ expressions = [licensing .parse (le , simple = True ) for le in detected_licenses ]
225+ combined_expression = licensing .AND (* expressions )
226+ return str (combined_expression )
208227
209228
210229def build_url (group_id , artifact_id , version , filename , baseurl = 'http://repo1.maven.org/maven2' ):
0 commit comments