|
"""Utility functions for the objaverse_xl package.""" |
|
|
|
import uuid |
|
import os |
|
import hashlib |
|
|
|
|
|
def get_uid_from_str(string: str) -> str: |
|
"""Generates a UUID from a string. |
|
|
|
Args: |
|
string (str): String to generate a UUID from. |
|
|
|
Returns: |
|
str: UUID generated from the string. |
|
""" |
|
namespace = uuid.NAMESPACE_DNS |
|
return str(uuid.uuid5(namespace, string)) |
|
|
|
|
|
def get_file_hash(file_path: str) -> str: |
|
"""Get the sha256 hash of a file. |
|
|
|
Args: |
|
file_path (str): Path to the file. |
|
|
|
Returns: |
|
str: sha256 hash of the file. |
|
""" |
|
|
|
if os.path.islink(file_path): |
|
|
|
resolved_path = os.readlink(file_path) |
|
|
|
if not os.path.exists(resolved_path): |
|
raise FileNotFoundError( |
|
f"The symbolic link points to a file that doesn't exist: {resolved_path}" |
|
) |
|
sha256 = hashlib.sha256() |
|
|
|
with open(file_path, "rb") as f: |
|
|
|
for byte_block in iter(lambda: f.read(4096), b""): |
|
sha256.update(byte_block) |
|
return sha256.hexdigest() |
|
|