Skip to content

Commit

Permalink
use environment variables or per-run temp directories to assign the t…
Browse files Browse the repository at this point in the history
…emporary downloads directory
  • Loading branch information
Jacob Beck committed Nov 6, 2018
1 parent bc20d71 commit 24a4224
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions dbt/task/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@

from dbt.task.base_task import BaseTask

DOWNLOADS_PATH = os.path.join(tempfile.gettempdir(), "dbt-downloads")
DOWNLOADS_PATH = None
REMOVE_DOWNLOADS = False


def _initialize_downloads():
global DOWNLOADS_PATH, REMOVE_DOWNLOADS
# the user might have set an environment variable. Set it to None, and do
# not remove it when finished.
if DOWNLOADS_PATH is None:
DOWNLOADS_PATH = os.environ.get('DBT_DOWNLOADS_DIR', None)
REMOVE_DOWNLOADS = False
# if we are making a per-run temp directory, remove it at the end of
# successful runs
if DOWNLOADS_PATH is None:
DOWNLOADS_PATH = tempfile.mkdtemp(prefix='dbt-downloads')
REMOVE_DOWNLOADS = True

dbt.clients.system.make_directory(DOWNLOADS_PATH)


class Package(APIObject):
Expand Down Expand Up @@ -396,6 +413,16 @@ def _read_packages(project_yaml):


class DepsTask(BaseTask):
def __init__(self, args, config=None):
super(DepsTask, self).__init__(args=args, config=config)
self._downloads_path = None

@property
def downloads_path(self):
if self._downloads_path is None:
self._downloads_path = tempfile.mkdtemp(prefix='dbt-downloads')
return self._downloads_path

def _check_for_duplicate_project_names(self, final_deps):
seen = set()
for _, package in final_deps.items():
Expand All @@ -421,7 +448,7 @@ def track_package_install(self, package_name, source_type, version):

def run(self):
dbt.clients.system.make_directory(self.config.modules_path)
dbt.clients.system.make_directory(DOWNLOADS_PATH)
_initialize_downloads()

packages = self.config.packages.packages
if not packages:
Expand Down Expand Up @@ -451,3 +478,6 @@ def run(self):
package_name=package.name,
source_type=package.source_type(),
version=package.version_name())

if REMOVE_DOWNLOADS:
shutil.rmtree(DOWNLOADS_PATH)

0 comments on commit 24a4224

Please sign in to comment.