Skip to content

Commit

Permalink
cli: add --cd flag to change directories before executing commands (#…
Browse files Browse the repository at this point in the history
…4566)

* cli: add -C flag to change directories before executing commands

Fixes #4025

* add deprecation notice to dvc repro help message
  • Loading branch information
bobertlo authored Sep 25, 2020
1 parent 9643ff7 commit c94ab35
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
9 changes: 9 additions & 0 deletions dvc/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""DVC command line interface"""
import argparse
import logging
import os
import sys

from .command import (
Expand Down Expand Up @@ -191,6 +192,14 @@ def get_main_parser():
help="Show program's version.",
)

parser.add_argument(
"--cd",
default=os.path.curdir,
metavar="<path>",
help="Change to directory before executing.",
type=str,
)

# Sub commands
subparsers = parser.add_subparsers(
title="Available Commands",
Expand Down
5 changes: 5 additions & 0 deletions dvc/command/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
from abc import ABC, abstractmethod

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -32,6 +33,8 @@ def __init__(self, args):
from dvc.repo import Repo
from dvc.updater import Updater

os.chdir(args.cd)

self.repo = Repo()
self.config = self.repo.config
self.args = args
Expand All @@ -55,3 +58,5 @@ def run(self):
class CmdBaseNoRepo(CmdBase):
def __init__(self, args): # pylint: disable=super-init-not-called
self.args = args

os.chdir(args.cd)
6 changes: 3 additions & 3 deletions dvc/command/repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
class CmdRepro(CmdBase):
def run(self):
saved_dir = os.path.realpath(os.curdir)
if self.args.cwd:
os.chdir(self.args.cwd)
os.chdir(self.args.cwd)

# Dirty hack so the for loop below can at least enter once
if self.args.all_pipelines:
Expand Down Expand Up @@ -97,7 +96,8 @@ def add_parser(subparsers, parent_parser):
"-c",
"--cwd",
default=os.path.curdir,
help="Directory within your repo to reproduce from.",
help="Directory within your repo to reproduce from. Note: deprecated "
"by `dvc --cd <path>`.",
metavar="<path>",
)
repro_parser.add_argument(
Expand Down
20 changes: 19 additions & 1 deletion tests/func/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,37 @@ def test(self):

class TestFindRoot(TestDvc):
def test(self):
os.chdir("..")
class Cmd(CmdBase):
def run(self):
pass

class A:
quiet = False
verbose = True
cd = os.path.pardir

args = A()
with self.assertRaises(DvcException):
Cmd(args)


class TestCd(TestDvc):
def test(self):
class Cmd(CmdBase):
def run(self):
pass

class A:
quiet = False
verbose = True
cd = os.path.pardir

parent_dir = os.path.realpath(os.path.pardir)
args = A()
with self.assertRaises(DvcException):
Cmd(args)
current_dir = os.path.realpath(os.path.curdir)
self.assertEqual(parent_dir, current_dir)


def test_unknown_command_help(capsys):
Expand Down

0 comments on commit c94ab35

Please sign in to comment.