@@ -63,31 +63,26 @@ def check_input_and_return_purl(project):
6363
6464
6565def fetch_inputs (purl ):
66- # Fetch the bianry and source for the given input purl and return the
67- # location of the fetched tarballs
66+ """Fetch the binary and source for the given input purl"""
6867 purl_str = PackageURL .to_string (purl )
69- purl_bin = purl_str
70- purl_src = f"{ purl_str } ?classifier=sources"
7168
72- try :
73- package = fetch .fetch_url (url = purl_bin )
74- purl_bin_path = [package .path ]
75- except Exception as e :
76- logger .info ("Failed to fetch binary package: %s" , e )
77- purl_bin_path = []
78-
79- try :
80- package_src = fetch .fetch_url (url = purl_src )
81- purl_src_path = [package_src .path ]
82- except Exception as e :
83- logger .info ("Failed to fetch source package: %s" , e )
84- purl_src_path = []
69+ purl_bin_path = fetch_path (purl_str , "binary" )
70+ purl_src_path = fetch_path (f"{ purl_str } ?classifier=sources" , "source" )
8571
8672 if not purl_bin_path and not purl_src_path :
8773 err_msg = f"No source or binary could be resolved for { purl } ."
8874 raise ValueError (err_msg )
8975
90- return purl_src_path , purl_bin_path
76+ return [purl_src_path ], [purl_bin_path ]
77+
78+
79+ def fetch_path (url , package_type ):
80+ """Fetch the url and return the location of the fetched tarball"""
81+ try :
82+ return fetch .fetch_url (url = url ).path
83+ except Exception as e :
84+ logger .info ("Failed to fetch %s package: %s" , package_type , e )
85+ return None
9186
9287
9388def fetch_and_scan_remote_pom (input_purl , scan_output_location ):
@@ -97,7 +92,7 @@ def fetch_and_scan_remote_pom(input_purl, scan_output_location):
9792 # Return and do nothing if data has pom.xml
9893 for file in data ["files" ]:
9994 if "pom.xml" in file ["path" ]:
100- return
95+ return []
10196 packages = data .get ("packages" , [])
10297
10398 pom_url = get_pom_url (input_purl )
@@ -141,33 +136,31 @@ def get_pom_url(input_purl):
141136
142137def download_pom_file (pom_url ):
143138 """Fetch the pom file from the input pom_url"""
144- pom_file_dict = {}
145-
139+ # PO: Could we use fetchcode to fetch instead? Yes, we could, but
140+ # the issue is do we want to. Following is the code if we switch to
141+ # fetchcode which seems making things complicated, OR we can move
142+ # the "fetch_http" to fetchcode and use it.
143+ """
144+ import os
145+ import fetchcode
146+ downloaded_pom = fetchcode.fetch(pom_url)
147+ location = str(downloaded_pom.location)
148+ path = location + ".pom"
149+ # The fetch function from fetchcode save the file as /tmp/name
150+ # without an extension. We need to add the ".pom" extension so that
151+ # the package scan can work properly for this file.
152+ os.rename(location, path)
153+ """
146154 try :
147- # PO: Could we use fetchcode to fetch instead? Yes, we could, but
148- # the issue is do we want to. Following is the code if we switch to
149- # fetchcode which seems making things complicated, OR we can move
150- # the "fetch_http" to fetchcode and use it.
151- """
152- import os
153- import fetchcode
154- downloaded_pom = fetchcode.fetch(pom_url)
155- location = str(downloaded_pom.location)
156- path = location + ".pom"
157- # The fetch function from fetchcode save the file as /tmp/name
158- # without an extension. We need to add the ".pom" extension so that
159- # the package scan can work properly for this file.
160- os.rename(location, path)
161- """
162155 downloaded_pom = fetch .fetch_http (pom_url )
163- path = str (downloaded_pom .path )
164-
165- pom_file_dict ["pom_file_path" ] = path
166- pom_file_dict ["output_path" ] = path + "-output.json"
167- pom_file_dict ["pom_url" ] = pom_url
168156 except requests .RequestException :
169157 # Return an empty dictionary
170- pass
158+ return {}
159+ path = str (downloaded_pom .path )
160+ pom_file_dict = {}
161+ pom_file_dict ["pom_file_path" ] = path
162+ pom_file_dict ["output_path" ] = path + "-output.json"
163+ pom_file_dict ["pom_url" ] = pom_url
171164 return pom_file_dict
172165
173166
0 commit comments