Skip to content

Commit

Permalink
feat: create parent dirs for new_path, and add test case for tsdb.mig…
Browse files Browse the repository at this point in the history
…rate();
  • Loading branch information
WenjieDu committed Dec 20, 2023
1 parent a89efb6 commit cf78382
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tests/test_tsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def test_3_dataset_purging(self):
tsdb.delete_cache("physionet_2012") # delete single
tsdb.delete_cache() # delete all

def test_4_logging(self):
def test_4_migrate(self):
os.makedirs("dir_for_migration")
with open("dir_for_migration/test.txt", "a") as f:
f.write("hello world")
tsdb.migrate("dir_for_migration", "new_dir/put_it_here")

def test_5_logging(self):
# different level logging
self.logger.debug("debug")
self.logger.info("info")
Expand Down
5 changes: 4 additions & 1 deletion tsdb/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def migrate(old_path: str, new_path: str) -> None:
"""
if not os.path.exists(old_path):
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.")
# if new_path exists, we have to move everything from old_path into it
Expand All @@ -123,10 +124,12 @@ def migrate(old_path: str, new_path: str) -> None:
shutil.copytree(old_f_path, new_f_path)
else:
shutil.move(old_f_path, new_path)

shutil.rmtree(old_path, ignore_errors=True)
else:
# if new_path does not exist, just rename the old_path into it
new_parent_dir = os.path.abspath(os.path.join(new_path, ".."))
if not os.path.exists(new_parent_dir):
os.makedirs(new_parent_dir, exist_ok=True)
os.rename(old_path, new_path)

config = ConfigParser()
Expand Down

0 comments on commit cf78382

Please sign in to comment.