island: Extract old data dir manipulation to a separate function

This commit is contained in:
Shreya Malviya 2021-10-20 16:30:52 +05:30
parent dd480d1703
commit 4ec53cb1d1
1 changed files with 23 additions and 17 deletions

View File

@ -5,7 +5,7 @@ import shutil
import sys
from pathlib import Path
from threading import Thread
from typing import Tuple
from typing import Optional, Tuple
import gevent.hub
from gevent.pywsgi import WSGIServer
@ -71,12 +71,18 @@ def _setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, st
print(f"Error loading server config: {ex}")
exit(1)
except OldDataError as ex:
return _handle_existing_data_directory(ex, island_args)
def _handle_existing_data_directory(
exception: Exception, island_args: IslandCmdArgs
) -> Optional[Tuple[IslandConfigOptions, str]]:
user_response = input(
f"\nExisting data directory ({ex.old_data_dir}) needs to be deleted."
f"\nExisting data directory ({exception.old_data_dir}) needs to be deleted."
" All data from previous runs will be lost. Proceed to delete? (y/n) "
)
if user_response == "y":
shutil.rmtree(ex.old_data_dir)
shutil.rmtree(exception.old_data_dir)
print("\nOld data directory was deleted. Trying to set up again...\n")
return _setup_data_dir(island_args)
elif user_response == "n":