From b0384f8f51221f22b3926f107f3ad276aa7c57e9 Mon Sep 17 00:00:00 2001 From: AlexandreKempf Date: Tue, 5 Mar 2024 07:03:16 +0100 Subject: [PATCH] `dvc studio login` automatically push experiments by default (#10331) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * studio login set auto_push + add logs for the user * set a separate global config dir for studio login tests The `isolate` fixture sets a separate, _isolated_ global config directory. But it is shared among other tests. So any mutations to it affects other tests too. We should force `studio login` tests to use a separate tmp_dir for global config. * cleanup message --------- Co-authored-by: Saugat Pachhai (सौगात) --- dvc/commands/studio.py | 11 +++++++++++ dvc/repo/experiments/executor/base.py | 7 +++++++ tests/unit/command/test_studio.py | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/dvc/commands/studio.py b/dvc/commands/studio.py index 49009bfff..90c910d4f 100644 --- a/dvc/commands/studio.py +++ b/dvc/commands/studio.py @@ -46,6 +46,15 @@ class CmdStudioLogin(CmdConfig): self.save_config(hostname, access_token) + if not self.config["exp"].get("auto_push", True): + from dvc.ui import ui + + ui.warn( + "exp.auto_push is disabled. \n" + "Enable with 'dvc config exp.auto_push true' " + "to automatically push experiments to Studio." + ) + config_path = self.config.files["global"] ui.write(f"Authentication complete. Saved token to {config_path}.") return 0 @@ -54,6 +63,8 @@ class CmdStudioLogin(CmdConfig): with self.config.edit("global") as conf: conf["studio"]["token"] = token conf["studio"]["url"] = hostname + if "auto_push" not in conf["exp"]: + conf["exp"]["auto_push"] = True class CmdStudioLogout(CmdConfig): diff --git a/dvc/repo/experiments/executor/base.py b/dvc/repo/experiments/executor/base.py index 855232d36..86a7f048a 100644 --- a/dvc/repo/experiments/executor/base.py +++ b/dvc/repo/experiments/executor/base.py @@ -688,8 +688,15 @@ class BaseExecutor(ABC): push_cache=True, run_cache=True, ): + from dvc.ui import ui + branch = dvc.scm.get_ref(EXEC_BRANCH, follow=False) try: + ui.write( + f"Auto pushing experiment to '{git_remote}'. You can cancel the push " + "with CTRL+C. If you need to push your experiment again, you can " + f"retry later using `dvc exp push`", + ) dvc.experiments.push( git_remote, branch, diff --git a/tests/unit/command/test_studio.py b/tests/unit/command/test_studio.py index 6484d71c4..c79ce9c8a 100644 --- a/tests/unit/command/test_studio.py +++ b/tests/unit/command/test_studio.py @@ -1,9 +1,18 @@ +import pytest from dvc_studio_client.auth import AuthorizationExpiredError +from dvc import env from dvc.cli import main from dvc.utils.studio import STUDIO_URL +@pytest.fixture(autouse=True) +def global_config_dir(monkeypatch, tmp_dir_factory): + monkeypatch.setenv( + env.DVC_GLOBAL_CONFIG_DIR, str(tmp_dir_factory.mktemp("studio-login")) + ) + + def test_studio_login_token_check_failed(mocker): mocker.patch( "dvc_studio_client.auth.get_access_token",