Skip to content

Commit

Permalink
feat: add migrate_cache();
Browse files Browse the repository at this point in the history
  • Loading branch information
WenjieDu committed Jan 15, 2024
1 parent c387ebe commit fa3aac1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/test_tsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_4_migrate(self):
with open("dir_for_migration/test.txt", "a") as f:
f.write("hello world")
tsdb.migrate("dir_for_migration", "new_dir/put_it_here")
tsdb.migrate_cache("new_cache_dir")

def test_5_logging(self):
# different level logging
Expand Down
40 changes: 24 additions & 16 deletions tsdb/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import os
import pickle
import shutil
from typing import Optional
from configparser import ConfigParser

from typing import Optional

from .logging import logger
from ..database import CACHED_DATASET_DIR
Expand Down Expand Up @@ -99,7 +98,7 @@ def purge_path(path: str, ignore_errors: bool = True) -> None:


def migrate(old_path: str, new_path: str) -> None:
"""Migrate datasets from old_path to new_path.
"""Migrate files in a directory from old_path to new_path.
Parameters
----------
Expand All @@ -114,7 +113,7 @@ def migrate(old_path: str, new_path: str) -> None:
raise FileNotFoundError(f"Given old_path {old_path} does not exist.")

if os.path.exists(new_path):
logger.warning(f"Please note that new_path {new_path} already exists.")
logger.warning(f"‼️ Please note that new_path {new_path} already exists.")
# if new_path exists, we have to move everything from old_path into it
all_old_files = os.listdir(old_path)
for f in all_old_files:
Expand All @@ -132,21 +131,30 @@ def migrate(old_path: str, new_path: str) -> None:
os.makedirs(new_parent_dir, exist_ok=True)
os.rename(old_path, new_path)

logger.info(
f"Successfully migrated {old_path} to {new_path}, and deleted {old_path}"
)


def migrate_cache(target_path: str) -> None:
"""Migrate datasets from old_path to new_path.
Parameters
----------
target_path:
The new path for TSDB to store cached datasets.
"""

config = ConfigParser()
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
tsdb_config_path = os.path.join(parent_dir, "config.ini")
config.read(tsdb_config_path)

if os.path.abspath(old_path) == os.path.abspath(CACHED_DATASET_DIR):
config.set("path", "data_home", new_path)
with open(tsdb_config_path, "w") as f:
config.write(f)

logger.info(
f"Found the given old_path is the current TSDB dataset cache directory. "
f"Have already set the new cache directory to {new_path}."
)
old_path = os.path.abspath(CACHED_DATASET_DIR)
config.set("path", "data_home", target_path)
with open(tsdb_config_path, "w") as f:
config.write(f)

logger.info(
f"Successfully migrated {old_path} to {new_path}, and deleted {old_path}"
)
migrate(old_path, target_path)
logger.info(f"Have set {target_path} as the default cache dir.")

0 comments on commit fa3aac1

Please sign in to comment.