2525from urllib .parse import urlparse
2626
2727import requests
28+ from license_expression import Licensing
2829from packageurl import PackageURL
2930from packageurl .contrib import purl2url
3031
@@ -46,19 +47,11 @@ def check_input_and_return_purl(project):
4647 if input_purl .type != "maven" :
4748 error_msg = "Only maven purl is supported."
4849 raise ValueError (error_msg )
49- # Version is required
5050 if not input_purl .version :
5151 error_msg = "Version is required."
5252 raise ValueError (error_msg )
5353
54- purl = PackageURL (
55- type = input_purl .type ,
56- namespace = input_purl .namespace ,
57- name = input_purl .name ,
58- version = input_purl .version ,
59- )
60-
61- return purl
54+ return input_purl
6255
6356
6457def fetch_inputs (purl ):
@@ -88,11 +81,10 @@ def fetch_and_scan_remote_pom(input_purl, scan_output_location):
8881 """Fetch the .pom file from from maven.org if not present in codebase."""
8982 with open (scan_output_location ) as file :
9083 data = json .load (file )
91- # Return and do nothing if data has pom.xml
92- for file in data ["files" ]:
93- if "pom.xml" in file ["path" ]:
94- return []
95- packages = data .get ("packages" , [])
84+ # Return and do nothing if data has pom.xml
85+ for file in data ["files" ]:
86+ if "pom.xml" in file ["path" ]:
87+ return []
9688
9789 pom_url = get_pom_url (input_purl )
9890 if not pom_url :
@@ -103,14 +95,18 @@ def fetch_and_scan_remote_pom(input_purl, scan_output_location):
10395 scanning_errors = scan_pom_file (pom_file )
10496
10597 scanned_pom_packages , scanned_dependencies = update_datafile_paths (pom_file )
98+ updated_data = update_scan_data (data , scanned_pom_packages , scanned_dependencies )
10699
107- updated_packages = packages + scanned_pom_packages
108- # Replace/Update the package and dependencies section
109- data ["packages" ] = updated_packages
110- data ["dependencies" ] = scanned_dependencies
111100 with open (scan_output_location , "w" ) as file :
112- json .dump (data , file , indent = 2 )
113- return scanning_errors
101+ json .dump (updated_data , file , indent = 2 )
102+ return [scanning_errors ]
103+
104+
105+ def update_scan_data (data , scanned_packages , scanned_dependencies ):
106+ """Update packages and dependencies data"""
107+ data ["packages" ] = data .get ("packages" , []) + scanned_packages
108+ data ["dependencies" ] = scanned_dependencies
109+ return data
114110
115111
116112def get_pom_url (input_purl ):
@@ -135,25 +131,9 @@ def get_pom_url(input_purl):
135131
136132def download_pom_file (pom_url ):
137133 """Fetch the pom file from the input pom_url"""
138- # PO: Could we use fetchcode to fetch instead? Yes, we could, but
139- # the issue is do we want to. Following is the code if we switch to
140- # fetchcode which seems making things complicated, OR we can move
141- # the "fetch_http" to fetchcode and use it.
142- """
143- import os
144- import fetchcode
145- downloaded_pom = fetchcode.fetch(pom_url)
146- location = str(downloaded_pom.location)
147- path = location + ".pom"
148- # The fetch function from fetchcode save the file as /tmp/name
149- # without an extension. We need to add the ".pom" extension so that
150- # the package scan can work properly for this file.
151- os.rename(location, path)
152- """
153134 try :
154135 downloaded_pom = fetch .fetch_http (pom_url )
155136 except requests .RequestException :
156- # Return an empty dictionary
157137 return {}
158138 path = str (downloaded_pom .path )
159139 pom_file_dict = {}
@@ -165,21 +145,18 @@ def download_pom_file(pom_url):
165145
166146def scan_pom_file (pom_file_dict ):
167147 """Fetch and scan the pom file from the input pom_urls"""
168- scan_errors = []
169148 pom_file_path = pom_file_dict .get ("pom_file_path" , "" )
170149 scanned_pom_output_path = pom_file_dict .get ("output_path" , "" )
171150
172151 # Run a package scan on the fetched pom.xml
173- scanning_errors = scancode .run_scan (
152+ # Return scanning errors, if present
153+ return scancode .run_scan (
174154 location = pom_file_path ,
175155 output_file = scanned_pom_output_path ,
176156 run_scan_args = {
177157 "package" : True ,
178158 },
179159 )
180- if scanning_errors :
181- scan_errors .append (scanning_errors )
182- return scan_errors
183160
184161
185162def update_datafile_paths (pom_file_dict ):
@@ -197,11 +174,9 @@ def update_datafile_paths(pom_file_dict):
197174 scanned_dependencies = scanned_pom_data .get ("dependencies" , [])
198175
199176 for scanned_package in scanned_packages :
200- # Replace the 'datafile_path' with the pom_url
201177 scanned_package ["datafile_paths" ] = [pom_url ]
202178 scanned_pom_packages .append (scanned_package )
203179 for scanned_dep in scanned_dependencies :
204- # Replace the 'datafile_path' with empty string
205180 # See https://github.com/aboutcode-org/scancode.io/issues/1763#issuecomment-3525165830
206181 scanned_dep ["datafile_path" ] = ""
207182 scanned_pom_deps .append (scanned_dep )
@@ -210,20 +185,20 @@ def update_datafile_paths(pom_file_dict):
210185
211186def update_package_license_from_resource_if_missing (project ):
212187 """Populate missing licenses to packages based on resource data."""
213- from license_expression import Licensing
214-
215188 for package in project .discoveredpackages .all ():
216189 if not package .get_declared_license_expression ():
217190 package_uid = package .package_uid
218- detected_lics = []
191+ detected_licenses = []
219192 for resource in project .codebaseresources .has_license_expression ():
220193 for for_package in resource .for_packages :
221194 if for_package == package_uid :
222- detected_lic_exp = resource .detected_license_expression
223- if detected_lic_exp not in detected_lics :
224- detected_lics .append (detected_lic_exp )
225- if detected_lics :
226- lic_exp = " AND " .join (detected_lics )
227- declared_lic_exp = str (Licensing ().dedup (lic_exp ))
228- package .declared_license_expression = declared_lic_exp
195+ detected_license_expression = (
196+ resource .detected_license_expression
197+ )
198+ if detected_license_expression not in detected_licenses :
199+ detected_licenses .append (detected_license_expression )
200+ if detected_licenses :
201+ license_expression = " AND " .join (detected_licenses )
202+ declared_license_expression = str (Licensing ().dedup (license_expression ))
203+ package .declared_license_expression = declared_license_expression
229204 package .save ()
0 commit comments