Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve directory checks #180

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/fairseq2/assets/download_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from urllib.request import Request, urlopen
from zipfile import BadZipFile, ZipFile

import torch
from tqdm import tqdm # type: ignore[import]

from fairseq2.assets.error import AssetError
Expand Down Expand Up @@ -283,24 +282,35 @@ def _try_uri_as_path(self) -> Optional[Path]:
def _init_download_path(self) -> None:
assert self.download_filename is not None

if pathname := os.getenv("FAIRSEQ2_CACHE_DIR"):
try:
cache_root_path = Path(pathname)
except ValueError as ex:
raise RuntimeError(
f"`FAIRSEQ2_CACHE_DIR` environment variable must contain a valid pathname, but contains '{pathname}' instead."
) from ex
else:
cache_root_path = Path(torch.hub.get_dir()).joinpath("fairseq2")
cache_dir = self._get_path_from_env("FAIRSEQ2_CACHE_DIR")
if cache_dir is None:
cache_dir = self._get_path_from_env("XDG_CACHE_HOME")
if cache_dir is None:
cache_dir = Path("~/.cache")

cache_dir = cache_dir.joinpath("fairseq2")

hash_ = sha1(self.uri.encode()).hexdigest()

hash_ = hash_[:24]

self.download_path = cache_root_path.expanduser().joinpath(
self.download_path = cache_dir.expanduser().joinpath(
"assets", hash_, self.download_filename
)

@staticmethod
def _get_path_from_env(var_name: str) -> Optional[Path]:
pathname = os.getenv(var_name)
if not pathname:
return None

try:
return Path(pathname)
except ValueError as ex:
raise RuntimeError(
f"`{var_name}` environment variable must contain a valid pathname, but contains '{pathname}' instead."
) from ex

def _download_asset(self) -> None:
assert self.download_path is not None

Expand Down
28 changes: 18 additions & 10 deletions src/fairseq2/assets/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,49 +161,57 @@ def _create_asset_store() -> ProviderBackedAssetStore:
asset_store = _create_asset_store()


def _get_asset_dir_from_env(var_name: str) -> Optional[Path]:
def _get_path_from_env(var_name: str) -> Optional[Path]:
pathname = os.getenv(var_name)
if not pathname:
return None

try:
asset_dir = Path(pathname)
path = Path(pathname)
except ValueError as ex:
raise RuntimeError(
f"`{var_name}` environment variable must contain a valid pathname, but contains '{pathname}' instead."
) from ex

if not asset_dir.exists():
if not path.exists():
logger = logging.getLogger("fairseq2.assets")

logger.warning(
f"The path '{asset_dir}' pointed to by the `{var_name}` environment variable does not exist."
f"The path '{path}' pointed to by the `{var_name}` environment variable does not exist."
)

return None

return asset_dir
return path


def _load_asset_directory() -> None:
asset_dir = _get_asset_dir_from_env("FAIRSEQ2_ASSET_DIR")
asset_dir = _get_path_from_env("FAIRSEQ2_ASSET_DIR")
if asset_dir is None:
asset_dir = Path("/etc/fairseq2/assets")
if not asset_dir.exists():
return

asset_dir = asset_dir.expanduser()

asset_store.metadata_providers.append(FileAssetMetadataProvider(asset_dir))


_load_asset_directory()


def _load_user_asset_directory() -> None:
asset_dir = _get_asset_dir_from_env("FAIRSEQ2_USER_ASSET_DIR")
asset_dir = _get_path_from_env("FAIRSEQ2_USER_ASSET_DIR")
if asset_dir is None:
asset_dir = Path("~/.config/fairseq2/assets").expanduser()
if not asset_dir.exists():
return
asset_dir = _get_path_from_env("XDG_CONFIG_HOME")
if asset_dir is None:
asset_dir = Path("~/.config").expanduser()
if not asset_dir.exists():
return

asset_dir = asset_dir.joinpath("fairseq2/assets")

asset_dir = asset_dir.expanduser()

asset_store.user_metadata_providers.append(FileAssetMetadataProvider(asset_dir))

Expand Down