Skip to content

Commit

Permalink
checkpoints/live: move execution env to stage (#5257)
Browse files Browse the repository at this point in the history
* checkpoints/live: move execution env to stage

* stage: env: provide DVC_ROOT for all repo runs
  • Loading branch information
pared authored Jan 18, 2021
1 parent 628f878 commit 28f6bea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
23 changes: 1 addition & 22 deletions dvc/output/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import logging
import os
from copy import copy
from typing import Dict, Type
from typing import Type
from urllib.parse import urlparse

from funcy import cached_property, project
from voluptuous import Any

import dvc.prompt as prompt
from dvc.cache import NamedCache
from dvc.env import DVCLIVE_PATH, DVCLIVE_REPORT, DVCLIVE_SUMMARY
from dvc.exceptions import (
CheckoutError,
CollectCacheError,
Expand Down Expand Up @@ -607,22 +605,3 @@ def merge(self, ancestor, other):
self.hash_info = self.cache.merge(
ancestor_info, self.hash_info, other.hash_info
)

@cached_property
def env(self) -> Dict[str, str]:
if self.live:
from dvc.schema import LIVE_PROPS

env = {DVCLIVE_PATH: str(self.path_info)}
if isinstance(self.live, dict):

config = project(self.live, LIVE_PROPS)

env[DVCLIVE_SUMMARY] = str(
int(config.get(BaseOutput.PARAM_LIVE_SUMMARY, True))
)
env[DVCLIVE_REPORT] = str(
int(config.get(BaseOutput.PARAM_LIVE_REPORT, True))
)
return env
return {}
36 changes: 32 additions & 4 deletions dvc/stage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,41 @@ def is_checkpoint(self):
"""
return any(out.checkpoint for out in self.outs)

@property
def env(self) -> Env:
def _read_env(self, out, checkpoint_func=None) -> Env:
env: Env = {}
if out.live:
from dvc.env import DVCLIVE_PATH, DVCLIVE_REPORT, DVCLIVE_SUMMARY
from dvc.output import BaseOutput
from dvc.schema import LIVE_PROPS

env[DVCLIVE_PATH] = str(out.path_info)
if isinstance(out.live, dict):
config = project(out.live, LIVE_PROPS)

env[DVCLIVE_SUMMARY] = str(
int(config.get(BaseOutput.PARAM_LIVE_SUMMARY, True))
)
env[DVCLIVE_REPORT] = str(
int(config.get(BaseOutput.PARAM_LIVE_REPORT, True))
)
elif out.checkpoint and checkpoint_func:
from dvc.env import DVC_CHECKPOINT

env.update({DVC_CHECKPOINT: "1"})
return env

def env(self, checkpoint_func=None) -> Env:
from dvc.env import DVC_ROOT

env: Env = {}
if self.repo:
env.update({DVC_ROOT: self.repo.root_dir})

for out in self.outs:
if any(out.env.keys() and env.keys()):
current = self._read_env(out, checkpoint_func=checkpoint_func)
if set(env.keys()).intersection(set(current.keys())):
raise DvcException("Duplicated env variable")
env.update(out.env)
env.update(current)
return env

def changed_deps(self):
Expand Down
10 changes: 1 addition & 9 deletions dvc/stage/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import threading
from contextlib import contextmanager

from dvc.env import DVC_CHECKPOINT, DVC_ROOT
from dvc.utils import fix_env

from .decorators import relock_repo, unlocked_repo
Expand Down Expand Up @@ -52,11 +51,8 @@ def _enforce_cmd_list(cmd):

def prepare_kwargs(stage, checkpoint_func=None):
kwargs = {"cwd": stage.wdir, "env": fix_env(None), "close_fds": True}
if checkpoint_func:
# indicate that checkpoint cmd is being run inside DVC
kwargs["env"].update(_checkpoint_env(stage))

kwargs["env"].update(stage.env)
kwargs["env"].update(stage.env(checkpoint_func=checkpoint_func))

# NOTE: when you specify `shell=True`, `Popen` [1] will default to
# `/bin/sh` on *nix and will add ["/bin/sh", "-c"] to your command.
Expand Down Expand Up @@ -143,10 +139,6 @@ def run_stage(stage, dry=False, force=False, checkpoint_func=None, **kwargs):
run(stage, dry=dry, checkpoint_func=checkpoint_func)


def _checkpoint_env(stage):
return {DVC_CHECKPOINT: "1", DVC_ROOT: stage.repo.root_dir}


@contextmanager
def checkpoint_monitor(stage, callback_func, proc, killed):
if not callback_func:
Expand Down

0 comments on commit 28f6bea

Please sign in to comment.