Skip to content

Commit

Permalink
[DPTOOLS-1858] Add shutdown hook for Closer (#3)
Browse files Browse the repository at this point in the history
* [DPTOOLS-1858] Add shutdown hook for deleting tmp folder in loader

* Update
  • Loading branch information
jinhyukchang authored Feb 15, 2019
1 parent 50cd555 commit 2ad85cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions databuilder/loader/file_system_neo4j_csv_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class FsNeo4jCSVLoader(Loader):
# Config keys
NODE_DIR_PATH = 'node_dir_path'
RELATION_DIR_PATH = 'relationship_dir_path'
FORCE_CREATE_DIR = 'force_create_directory'
SHOULD_DELETE_CREATED_DIR = 'delete_created_directories'

_DEFAULT_CONFIG = ConfigFactory.from_dict({
SHOULD_DELETE_CREATED_DIR: True
SHOULD_DELETE_CREATED_DIR: True,
FORCE_CREATE_DIR: False
})

def __init__(self):
Expand All @@ -55,6 +57,7 @@ def init(self, conf):

self._delete_created_dir = \
conf.get_bool(FsNeo4jCSVLoader.SHOULD_DELETE_CREATED_DIR)
self._force_create_dir = conf.get_bool(FsNeo4jCSVLoader.FORCE_CREATE_DIR)
self._create_directory(self._node_dir)
self._create_directory(self._relation_dir)

Expand All @@ -67,8 +70,11 @@ def _create_directory(self, path):
:return:
"""
if os.path.exists(path):
raise RuntimeError(
'Directory should not exist: {}'.format(path))
if self._force_create_dir:
LOGGER.info('Directory exist. Deleting directory {}'.format(path))
shutil.rmtree(path)
else:
raise RuntimeError('Directory should not exist: {}'.format(path))

os.makedirs(path)

Expand Down
3 changes: 3 additions & 0 deletions databuilder/utils/closer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import atexit

from typing import Callable, List # noqa: F401


Expand All @@ -13,6 +15,7 @@ class Closer(object):
def __init__(self):
# type: () -> None
self._stack = [] # type: List
atexit.register(self.close)

def register(self, close_callable):
# type: (Callable) -> None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages


__version__ = '1.0.0'
__version__ = '1.0.1'

setup(
name='amundsen-databuilder',
Expand Down

0 comments on commit 2ad85cd

Please sign in to comment.