From 22c4f0a620a8cb6217d29a2230aa80b297f2d872 Mon Sep 17 00:00:00 2001 From: rariyama Date: Wed, 6 Dec 2023 18:50:29 +0900 Subject: [PATCH 1/4] made class changing directory a context manager. --- core/dbt/cli/main.py | 20 +++++++++----------- core/dbt/task/clean.py | 8 ++++++++ core/dbt/task/deps.py | 8 ++++++++ core/dbt/task/init.py | 7 +++++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index 2454a15a564..e33fb7c5115 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -239,10 +239,9 @@ def build(ctx, **kwargs): @requires.project def clean(ctx, **kwargs): """Delete all folders in the clean-targets list (usually the dbt_packages and target directories.)""" - task = CleanTask(ctx.obj["flags"], ctx.obj["project"]) - - results = task.run() - success = task.interpret_results(results) + with CleanTask(ctx.obj["flags"], ctx.obj["project"]) as task: + results = task.run() + success = task.interpret_results(results) return results, success @@ -489,9 +488,9 @@ def deps(ctx, **kwargs): message="Invalid flag `--dry-run` when not using `--add-package`.", option_name="--dry-run", ) - task = DepsTask(flags, ctx.obj["project"]) - results = task.run() - success = task.interpret_results(results) + with DepsTask(flags, ctx.obj["project"]) as task: + results = task.run() + success = task.interpret_results(results) return results, success @@ -511,10 +510,9 @@ def deps(ctx, **kwargs): @requires.preflight def init(ctx, **kwargs): """Initialize a new dbt project.""" - task = InitTask(ctx.obj["flags"], None) - - results = task.run() - success = task.interpret_results(results) + with InitTask(ctx.obj["flags"], None) as task: + results = task.run() + success = task.interpret_results(results) return results, success diff --git a/core/dbt/task/clean.py b/core/dbt/task/clean.py index 18b64e0b091..75dc1c96061 100644 --- a/core/dbt/task/clean.py +++ b/core/dbt/task/clean.py @@ -1,3 +1,4 @@ +import os from pathlib import Path from shutil import rmtree @@ -16,6 +17,13 @@ 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 diff --git a/core/dbt/task/deps.py b/core/dbt/task/deps.py index a694f78dac5..3ee79a1dcba 100644 --- a/core/dbt/task/deps.py +++ b/core/dbt/task/deps.py @@ -1,3 +1,4 @@ +import os from hashlib import sha1 from typing import Any, Dict, Optional, List import yaml @@ -95,12 +96,19 @@ 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: diff --git a/core/dbt/task/init.py b/core/dbt/task/init.py index 39d18b8dcb2..9eff07595c5 100644 --- a/core/dbt/task/init.py +++ b/core/dbt/task/init.py @@ -57,6 +57,13 @@ 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( From 08093c91155574b341cb668a4bbc008de8f169aa Mon Sep 17 00:00:00 2001 From: rariyama Date: Wed, 6 Dec 2023 19:25:27 +0900 Subject: [PATCH 2/4] add change log --- .changes/unreleased/Breaking Changes-20231206-192442.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Breaking Changes-20231206-192442.yaml diff --git a/.changes/unreleased/Breaking Changes-20231206-192442.yaml b/.changes/unreleased/Breaking Changes-20231206-192442.yaml new file mode 100644 index 00000000000..c6aad99bce5 --- /dev/null +++ b/.changes/unreleased/Breaking Changes-20231206-192442.yaml @@ -0,0 +1,6 @@ +kind: Breaking Changes +body: Fix changing the current working directory when using dpt deps, clean and init. +time: 2023-12-06T19:24:42.575372+09:00 +custom: + Author: rariyama + Issue: "8997" From 398cd0b79eed47546186bd7178b4888a38e1e199 Mon Sep 17 00:00:00 2001 From: "ryo.ariyama" Date: Mon, 19 Feb 2024 14:41:11 +0900 Subject: [PATCH 3/4] fix conflict --- core/dbt/cli/main.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index 87fb7365120..f1f844189b9 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -456,12 +456,6 @@ def deps(ctx, **kwargs): message=f"Version is required in --add-package when a package when source is {flags.SOURCE}", option_name="--add-package", ) - else: - if flags.DRY_RUN: - raise BadOptionUsage( - message="Invalid flag `--dry-run` when not using `--add-package`.", - option_name="--dry-run", - ) with DepsTask(flags, ctx.obj["project"]) as task: results = task.run() success = task.interpret_results(results) From 31ba979062d8c10d168a1847288c1aecc9fbeced Mon Sep 17 00:00:00 2001 From: "ryo.ariyama" Date: Wed, 21 Feb 2024 17:38:39 +0900 Subject: [PATCH 4/4] made base as a context manager --- core/dbt/task/base.py | 7 +++++++ core/dbt/task/clean.py | 8 -------- core/dbt/task/deps.py | 12 +----------- core/dbt/task/init.py | 7 ------- 4 files changed, 8 insertions(+), 26 deletions(-) diff --git a/core/dbt/task/base.py b/core/dbt/task/base.py index 690ae36a71b..c9793593d7e 100644 --- a/core/dbt/task/base.py +++ b/core/dbt/task/base.py @@ -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.""" diff --git a/core/dbt/task/clean.py b/core/dbt/task/clean.py index fc7bfe97fe6..efae26bf6e1 100644 --- a/core/dbt/task/clean.py +++ b/core/dbt/task/clean.py @@ -1,4 +1,3 @@ -import os from pathlib import Path from shutil import rmtree @@ -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 diff --git a/core/dbt/task/deps.py b/core/dbt/task/deps.py index 012869ec5fc..193c7ec0f62 100644 --- a/core/dbt/task/deps.py +++ b/core/dbt/task/deps.py @@ -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 @@ -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: @@ -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() diff --git a/core/dbt/task/init.py b/core/dbt/task/init.py index 88e997f2fd5..0b6f4fb22d6 100644 --- a/core/dbt/task/init.py +++ b/core/dbt/task/init.py @@ -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(