Skip to content

metrics/plots: initial support non-dvc repos #4627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dvc/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ def append_doc_link(help_message, path):


class CmdBase(ABC):
UNINITIALIZED = False

def __init__(self, args):
from dvc.repo import Repo
from dvc.updater import Updater

os.chdir(args.cd)

self.repo = Repo()
self.repo = Repo(uninitialized=self.UNINITIALIZED)
self.config = self.repo.config
self.args = args
hardlink_lock = self.config["core"].get("hardlink_lock", False)
Expand Down
8 changes: 6 additions & 2 deletions dvc/command/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def show_metrics(
raise BadMetricError(missing)


class CmdMetricsShow(CmdBase):
class CmdMetricsBase(CmdBase):
UNINITIALIZED = True


class CmdMetricsShow(CmdMetricsBase):
def run(self):
try:
metrics = self.repo.metrics.show(
Expand Down Expand Up @@ -104,7 +108,7 @@ def _round(val):
return table(header, rows, markdown)


class CmdMetricsDiff(CmdBase):
class CmdMetricsDiff(CmdMetricsBase):
def run(self):
try:
diff = self.repo.metrics.diff(
Expand Down
2 changes: 2 additions & 0 deletions dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@


class CmdPlots(CmdBase):
UNINITILIZED = True

def _func(self, *args, **kwargs):
raise NotImplementedError

Expand Down
16 changes: 14 additions & 2 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ class Repo:
from dvc.repo.status import status
from dvc.repo.update import update

def __init__(self, root_dir=None, scm=None, rev=None, subrepos=False):
def __init__(
self,
root_dir=None,
scm=None,
rev=None,
subrepos=False,
uninitialized=False,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might consider getting rid of this in the future (for now needed for backward compatibility). Currently dvc init doesn't do much useful things and all .dvc contents could be (and should probably) be created dynamically.

Even things like dvc add could potentially just work in a pure git repo. Just something to think about...

):
from dvc.cache import Cache
from dvc.data_cloud import DataCloud
from dvc.lock import make_lock
Expand All @@ -103,7 +110,12 @@ def __init__(self, root_dir=None, scm=None, rev=None, subrepos=False):
)
self.state = StateNoop()
else:
root_dir = self.find_root(root_dir)
try:
root_dir = self.find_root(root_dir)
except NotDvcRepoError:
if not uninitialized:
raise
root_dir = SCM(root_dir or os.curdir).root_dir
self.root_dir = os.path.abspath(os.path.realpath(root_dir))
self.tree = LocalTree(
self,
Expand Down
16 changes: 14 additions & 2 deletions tests/func/metrics/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,31 @@ def test_missing_cache(tmp_dir, dvc, run_copy_metrics):
assert dvc.metrics.show() == {"": {"metrics.yaml": 1.1}}


def test_show_non_metric(tmp_dir, dvc):
@pytest.mark.parametrize("use_dvc", [True, False])
def test_show_non_metric(tmp_dir, scm, use_dvc):
tmp_dir.gen("metrics.yaml", "foo: 1.1")

if use_dvc:
dvc = Repo.init()
else:
dvc = Repo(uninitialized=True)

assert dvc.metrics.show(targets=["metrics.yaml"]) == {
"": {"metrics.yaml": {"foo": 1.1}}
}


def test_show_non_metric_branch(tmp_dir, scm, dvc):
@pytest.mark.parametrize("use_dvc", [True, False])
def test_show_non_metric_branch(tmp_dir, scm, use_dvc):
tmp_dir.scm_gen("metrics.yaml", "foo: 1.1", commit="init")
with tmp_dir.branch("branch", new=True):
tmp_dir.scm_gen("metrics.yaml", "foo: 2.2", commit="other")

if use_dvc:
dvc = Repo.init()
else:
dvc = Repo(uninitialized=True)

assert dvc.metrics.show(targets=["metrics.yaml"], revs=["branch"]) == {
"workspace": {"metrics.yaml": {"foo": 1.1}},
"branch": {"metrics.yaml": {"foo": 2.2}},
Expand Down
10 changes: 9 additions & 1 deletion tests/func/plots/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
from funcy import first

from dvc.repo import Repo
from dvc.repo.plots.data import (
JSONPlotData,
NoMetricInHistoryError,
Expand Down Expand Up @@ -604,12 +605,19 @@ def test_multiple_plots(tmp_dir, scm, dvc, run_copy_metrics):
assert len(dvc.plots.show().keys()) == 2


def test_show_non_plot(tmp_dir, scm, dvc):
@pytest.mark.parametrize("use_dvc", [True, False])
def test_show_non_plot(tmp_dir, scm, use_dvc):
metric = [
{"first_val": 100, "val": 2},
{"first_val": 200, "val": 3},
]
_write_json(tmp_dir, metric, "metric.json")

if use_dvc:
dvc = Repo.init()
else:
dvc = Repo(uninitialized=True)

plot_string = dvc.plots.show(targets=["metric.json"])["metric.json"]

plot_content = json.loads(plot_string)
Expand Down