Skip to content

Commit c7c51eb

Browse files
fix(gsutil): Add graceful error handling for missing gsutil (#4833)
Co-authored-by: Terri Oda <[email protected]>
1 parent e9d01bf commit c7c51eb

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

cve_bin_tool/data_sources/osv_source.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import datetime
88
import io
99
import json
10+
import os
11+
import shutil
1012
import zipfile
1113
from pathlib import Path
1214

@@ -20,6 +22,15 @@
2022
from cve_bin_tool.version import HTTP_HEADERS
2123

2224

25+
def find_gsutil():
26+
gsutil_path = shutil.which("gsutil")
27+
if not os.path.exists(gsutil_path):
28+
raise FileNotFoundError(
29+
"gsutil not found. Did you need to install requirements or activate a venv where gsutil is installed?"
30+
)
31+
return gsutil_path
32+
33+
2334
class OSV_Source(Data_Source):
2435
"""Class to retrieve CVE's from the Open Source Vulnerabilities (OSV) Database"""
2536

@@ -52,16 +63,17 @@ async def update_ecosystems(self):
5263
"""Gets names of all ecosystems that OSV provides."""
5364

5465
ecosystems = []
66+
gsutil_path = find_gsutil() # use helper function
5567

5668
# Inspect the list of files and folders at the top level in the GS bucket.
57-
stdout, _, _ = await aio_run_command(["gsutil", "ls", self.gs_url])
69+
stdout, _, _ = await aio_run_command([gsutil_path, "ls", self.gs_url])
5870
lines = stdout.split(b"\n")
5971

6072
# For each line in the directory listing determine if it is a folder that
6173
# contains all.zip.
6274
for line in lines:
6375
ecosystem_zip = line + b"all.zip"
64-
stdout, _, _ = await aio_run_command(["gsutil", "ls", ecosystem_zip])
76+
stdout, _, _ = await aio_run_command([gsutil_path, "ls", ecosystem_zip])
6577
if stdout.strip(b"\n") == ecosystem_zip:
6678
# Found a valid ecosystem
6779
ecosystem = str(line).split("/")[-2]
@@ -126,8 +138,9 @@ def parse_filename(self, str):
126138
async def get_newfiles(self, ecosystem, time_of_last_update):
127139
"""Gets list of files modified after time of last update."""
128140

141+
gsutil_path = find_gsutil() # use helper function
129142
gs_file = self.gs_url + ecosystem
130-
stdout, _, _ = await aio_run_command(["gsutil", "ls", "-l", gs_file])
143+
stdout, _, _ = await aio_run_command([gsutil_path, "ls", "-l", gs_file])
131144
stdout = str(stdout).split("json")
132145

133146
newfiles = []
@@ -142,8 +155,9 @@ async def get_newfiles(self, ecosystem, time_of_last_update):
142155
async def get_totalfiles(self, ecosystem):
143156
"""Gets total number of files in an ecosystem"""
144157

158+
gsutil_path = find_gsutil() # use helper function
145159
gs_file = self.gs_url + ecosystem + "/all.zip"
146-
await aio_run_command(["gsutil", "cp", gs_file, self.osv_path])
160+
await aio_run_command([gsutil_path, "cp", gs_file, self.osv_path])
147161

148162
zip_path = Path(self.osv_path) / "all.zip"
149163
totalfiles = 0

0 commit comments

Comments
 (0)