Skip to content

Commit ec18545

Browse files
scripts: west_commands: blobs: support ZEPHYR_BLOBS_CACHE
In environment variable `ZEPHYR_BLOBS_CACHE` one or more directories (separated by `;`) can be specified. `west blobs fetch` will search in these folders for according file and copy it if it exists. If only one directory was given, it will automatically fill the cache by fetching the blob to this cache dir (with prefixed sha256 hashsum) and subsequently copy the file to the blobs path. Otherwise it fetches the blob as usual directly to the blobs path. Signed-off-by: Thorsten Klein <[email protected]>
1 parent 4758cfa commit ec18545

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

scripts/west_commands/blobs.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import argparse
66
import os
77
import re
8+
import shutil
89
import sys
910
import textwrap
1011
from pathlib import Path
@@ -121,16 +122,47 @@ def list(self, args):
121122
def ensure_folder(self, path):
122123
path.parent.mkdir(parents=True, exist_ok=True)
123124

124-
def fetch_blob(self, url, path):
125+
def fetch_blob(self, blob):
126+
url = blob['url']
127+
path = Path(blob['path'])
128+
129+
130+
cached_blob = None
131+
sha256 = blob['sha256']
132+
cached_filenames = [f'{sha256}-{path.name}', path.name]
133+
134+
# check if the blob is already cached
135+
cache_dirs = os.getenv('ZEPHYR_BLOBS_CACHE', '')
136+
for cache in cache_dirs.split(';'):
137+
for cached_filename in cached_filenames:
138+
cached_path = Pathcache) / cached_filename
139+
status = zephyr_module.get_blob_status(cached_path.resolve(), sha256)
140+
if status == zephyr_module.BLOB_PRESENT:
141+
cached_blob = cached_path
142+
break
143+
if cached_blob:
144+
break
145+
125146
scheme = urlparse(url).scheme
126147
self.dbg(f'Fetching {path} with {scheme}')
127148
import fetchers
128149
fetcher = fetchers.get_fetcher_cls(scheme)
129150

130151
self.dbg(f'Found fetcher: {fetcher}')
131152
inst = fetcher()
153+
154+
# Fill the cache if only one cache dir is used.
155+
if not cached_blob and len(cache_dirs) == 1:
156+
cached_path = cache_dirs[0] / cached_filenames[0]
157+
if not cached_path.exists(): # do not overwrite existing files
158+
inst.fetch(url, cached_path)
159+
cached_blob = cached_path
160+
132161
self.ensure_folder(path)
133-
inst.fetch(url, path)
162+
if cached_blob:
163+
shutil.copy(cached_path, path)
164+
else:
165+
inst.fetch(url, path)
134166

135167
# Compare the checksum of a file we've just downloaded
136168
# to the digest in blob metadata, warn user if they differ.
@@ -200,7 +232,7 @@ def fetch(self, args):
200232
self.wrn('Skip fetching this blob.')
201233
continue
202234

203-
self.fetch_blob(blob['url'], blob['abspath'])
235+
self.fetch_blob(blob)
204236
if not self.verify_blob(blob):
205237
bad_checksum_count += 1
206238

0 commit comments

Comments
 (0)