7
7
import datetime
8
8
import io
9
9
import json
10
+ import os
11
+ import shutil
10
12
import zipfile
11
13
from pathlib import Path
12
14
20
22
from cve_bin_tool .version import HTTP_HEADERS
21
23
22
24
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
+
23
34
class OSV_Source (Data_Source ):
24
35
"""Class to retrieve CVE's from the Open Source Vulnerabilities (OSV) Database"""
25
36
@@ -52,16 +63,17 @@ async def update_ecosystems(self):
52
63
"""Gets names of all ecosystems that OSV provides."""
53
64
54
65
ecosystems = []
66
+ gsutil_path = find_gsutil () # use helper function
55
67
56
68
# 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 ])
58
70
lines = stdout .split (b"\n " )
59
71
60
72
# For each line in the directory listing determine if it is a folder that
61
73
# contains all.zip.
62
74
for line in lines :
63
75
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 ])
65
77
if stdout .strip (b"\n " ) == ecosystem_zip :
66
78
# Found a valid ecosystem
67
79
ecosystem = str (line ).split ("/" )[- 2 ]
@@ -126,8 +138,9 @@ def parse_filename(self, str):
126
138
async def get_newfiles (self , ecosystem , time_of_last_update ):
127
139
"""Gets list of files modified after time of last update."""
128
140
141
+ gsutil_path = find_gsutil () # use helper function
129
142
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 ])
131
144
stdout = str (stdout ).split ("json" )
132
145
133
146
newfiles = []
@@ -142,8 +155,9 @@ async def get_newfiles(self, ecosystem, time_of_last_update):
142
155
async def get_totalfiles (self , ecosystem ):
143
156
"""Gets total number of files in an ecosystem"""
144
157
158
+ gsutil_path = find_gsutil () # use helper function
145
159
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 ])
147
161
148
162
zip_path = Path (self .osv_path ) / "all.zip"
149
163
totalfiles = 0
0 commit comments