예제 #1
0
def _maybe_iterdir(path: utils.ReadOnlyPath) -> Iterator[utils.ReadOnlyPath]:
    """Same as `path.iterdir()`, but don't fail if path does not exists."""
    # Use try/except rather than `.exists()` to avoid an extra RPC call
    # per namespace
    try:
        for f in path.iterdir():
            yield f
    except (FileNotFoundError, tf.errors.NotFoundError):
        pass
예제 #2
0
def _compute_dir_hash(path: utils.ReadOnlyPath) -> str:
  """Computes the checksums of the given directory deterministically."""
  all_files = sorted(path.iterdir())

  if any(f.is_dir() for f in all_files):
    raise ValueError('Installed package should only contains files.')

  # Concatenate the filenames and files content to create the directory hash
  all_checksums = [f.name for f in all_files]
  all_checksums += [checksums.compute_url_info(f).checksum for f in all_files]
  return hashlib.sha256(''.join(all_checksums).encode()).hexdigest()