MCPcopy Create free account
hub / github.com/modelscope/modelscope / exists

Method exists

modelscope/hub/utils/caching.py:313–352  ·  view source on GitHub ↗

Check the file is cached or not. Note existence check will also cover digest check Args: model_file_info (CachedFileInfo): The cached file info Returns: bool: If exists and has the same hash, return True otherwise False

(self, model_file_info)

Source from the content-addressed store, hash-verified

311 return cache_key
312
313 def exists(self, model_file_info):
314 """Check the file is cached or not. Note existence check will also cover digest check
315
316 Args:
317 model_file_info (CachedFileInfo): The cached file info
318
319 Returns:
320 bool: If exists and has the same hash, return True otherwise False
321 """
322 key = self.__get_cache_key(model_file_info)
323 is_exists = False
324 file_path = key['Path']
325 cache_file_path = os.path.join(self.cache_root_location,
326 model_file_info['Path'])
327 for cached_key in self.cached_files:
328 if cached_key['Path'] == file_path and (
329 cached_key['Revision'].startswith(key['Revision'])
330 or key['Revision'].startswith(cached_key['Revision'])):
331 expected_hash = model_file_info[FILE_HASH]
332 if expected_hash is not None and os.path.exists(
333 cache_file_path):
334 # compute hash only when enabled, otherwise just meet expectation by default
335 if enable_default_hash_validation:
336 cache_file_sha256 = compute_hash(cache_file_path)
337 else:
338 cache_file_sha256 = expected_hash
339 if expected_hash == cache_file_sha256:
340 is_exists = True
341 break
342 else:
343 logger.info(
344 f'File [{file_path}] exists in cache but with a mismatched hash, will re-download.'
345 )
346 if is_exists:
347 if os.path.exists(cache_file_path):
348 return True
349 else:
350 self.remove_key(
351 model_file_info) # someone may manual delete the file
352 return False
353
354 def remove_if_exists(self, model_file_info):
355 """We in cache, remove it.

Callers 1

Calls 5

__get_cache_keyMethod · 0.95
compute_hashFunction · 0.90
infoMethod · 0.80
remove_keyMethod · 0.80
existsMethod · 0.45

Tested by

no test coverage detected