Skip to content

Commit

Permalink
made base as a context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rariyama committed Feb 21, 2024
1 parent 398cd0b commit 31ba979
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
7 changes: 7 additions & 0 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def __init__(self, args, config, project=None) -> None:
self.config = config
self.project = config if isinstance(config, Project) else project

def __enter__(self):
self.orig_dir = os.getcwd()
return self

def __exit__(self, exc_type, exc_value, traceback):
os.chdir(self.orig_dir)

@classmethod
def pre_init_hook(cls, args):
"""A hook called before the task is initialized."""
Expand Down
8 changes: 0 additions & 8 deletions core/dbt/task/clean.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path
from shutil import rmtree

Expand All @@ -17,13 +16,6 @@


class CleanTask(BaseTask):
def __enter__(self):
self.orig_dir = os.getcwd()
return self

def __exit__(self, exc_type, exc_value, traceback):
os.chdir(self.orig_dir)

def run(self):
"""
This function takes all the paths in the target file
Expand Down
12 changes: 1 addition & 11 deletions core/dbt/task/deps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
from hashlib import sha1
from typing import Any, Dict, Optional, List
import yaml
from pathlib import Path
import dbt.utils
import dbt.deprecations
import dbt.exceptions
Expand Down Expand Up @@ -99,19 +97,10 @@ def __init__(self, args: Any, project: Project) -> None:
# --project-dir with deps. A larger overhaul of our path handling methods
# is needed to fix this the "right" way.
# See GH-7615
self.orig_dir = os.getcwd()
project.project_root = str(Path(project.project_root).resolve())

move_to_nearest_project_dir(project.project_root)
super().__init__(args=args, config=None, project=project)
self.cli_vars = args.vars

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
os.chdir(self.orig_dir)

def track_package_install(
self, package_name: str, source_type: str, version: Optional[str]
) -> None:
Expand Down Expand Up @@ -214,6 +203,7 @@ def lock(self) -> None:
fire_event(DepsLockUpdating(lock_filepath=lock_filepath))

def run(self) -> None:
move_to_nearest_project_dir(self.args.project_dir)
if self.args.add_package:
self.add()

Expand Down
7 changes: 0 additions & 7 deletions core/dbt/task/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@


class InitTask(BaseTask):
def __enter__(self):
self.orig_dir = os.getcwd()
return self

def __exit__(self, exc_type, exc_value, traceback):
os.chdir(self.orig_dir)

def copy_starter_repo(self, project_name):
fire_event(StarterProjectPath(dir=starter_project_directory))
shutil.copytree(
Expand Down

0 comments on commit 31ba979

Please sign in to comment.