diff --git a/dvc/command/run.py b/dvc/command/run.py index 35c23b2b6..25b9bae62 100644 --- a/dvc/command/run.py +++ b/dvc/command/run.py @@ -1,6 +1,7 @@ import os from dvc.command.common.base import CmdBase +from dvc.data_cloud import file_md5 from dvc.exceptions import DvcException from dvc.logger import Logger from dvc.state_file import StateFile @@ -18,7 +19,7 @@ class CmdRun(CmdBase): def run(self): cmd = ' '.join(self.parsed_args.command) - command = StateFile(data_item=None, + state = StateFile(data_item=None, cmd=cmd, out=self.parsed_args.out, out_git=self.parsed_args.out_git, @@ -26,36 +27,43 @@ class CmdRun(CmdBase): locked=self.parsed_args.lock, md5=None) - self.run_command(self.settings, command) - return self.commit_if_needed('DVC run: {}'.format(command.cmd)) + self.run_command(self.settings, state) + return self.commit_if_needed('DVC run: {}'.format(state.cmd)) @staticmethod - def run_command(settings, command): - Executor.exec_cmd_only_success(command.cmd, shell=True) + def run_command(settings, state): + Executor.exec_cmd_only_success(state.cmd, shell=True) - CmdRun.apply_to_files(command.out, command, CmdRun._create_cache_and_state_files, settings) - CmdRun.apply_to_files(command.out_git, command, CmdRun._create_state_file, settings) + CmdRun.apply_to_files(state.out, state, CmdRun._create_cache_and_state_files, settings) + CmdRun.apply_to_files(state.out_git, state, CmdRun._create_state_file, settings) @staticmethod - def apply_to_files(files, command, func, settings): - [func(i, command, settings) for i in settings.path_factory.to_data_items(files)[0]] + def apply_to_files(files, state, func, settings): + items = settings.path_factory.to_data_items(files)[0] + [func(i, state, settings) for i in items] @staticmethod - def _create_cache_and_state_files(data_item, command, settings): + def _create_cache_and_state_files(data_item, state, settings): Logger.debug('Move output file "{}" to cache dir "{}" and create a hardlink'.format( data_item.data.relative, data_item.cache_dir_abs)) data_item.move_data_to_cache() - return CmdRun._create_state_file(data_item, command, settings) + return CmdRun._create_state_file(data_item, state, settings, cache_file_exists = True) @staticmethod - def _create_state_file(data_item, command, settings): + def _create_state_file(data_item, state, settings, cache_file_exists=False): Logger.debug('Create state file "{}"'.format(data_item.state.relative)) - state_file = StateFile(data_item=data_item, - cmd=command.cmd, - out=command.out, - out_git=command.out_git, - locked=command.locked, - deps=StateFile.parse_deps_state(settings, command.deps), - md5=os.path.basename(data_item.cache.relative)) + + if cache_file_exists: + md5 = os.path.basename(data_item.cache.relative) + else: + md5 = file_md5(data_item.data.relative)[0] + + state_file = StateFile(data_item, + state.cmd, + state.out, + state.out_git, + locked=state.locked, + deps=StateFile.parse_deps_state(settings, state.deps), + md5=md5) state_file.save() return state_file diff --git a/dvc/git_wrapper.py b/dvc/git_wrapper.py index d02e4fbe3..769ae7639 100644 --- a/dvc/git_wrapper.py +++ b/dvc/git_wrapper.py @@ -5,10 +5,9 @@ from collections import defaultdict from dvc import utils from dvc.exceptions import DvcException from dvc.logger import Logger -from dvc.config import Config, ConfigI +from dvc.config import Config from dvc.executor import Executor, ExecutorError -from dvc.path.data_item import DataItemError -from dvc.state_file import StateFile +from dvc.path.data_item import DataItem from dvc.system import System from dvc.graph.workflow import Workflow from dvc.graph.commit import Commit diff --git a/dvc/path/data_item.py b/dvc/path/data_item.py index 9a35d5293..fe539f84a 100644 --- a/dvc/path/data_item.py +++ b/dvc/path/data_item.py @@ -23,7 +23,7 @@ class DataDirError(DvcException): class DataItemInConfigDirError(DataDirError): def __init__(self, file): msg = 'File "{}" is in config directory'.format(file) - super(DataItemInStatusDirError, self).__init__(msg) + super(DataDirError, self).__init__(msg) class NotInGitDirError(DataDirError): diff --git a/dvc/state_file.py b/dvc/state_file.py index d84a0ce56..7f439af05 100644 --- a/dvc/state_file.py +++ b/dvc/state_file.py @@ -1,14 +1,10 @@ import os -import ntpath -import sys import yaml -import re -from dvc import utils from dvc.exceptions import DvcException -from dvc.system import System from dvc.data_cloud import file_md5 + class StateFileError(DvcException): def __init__(self, msg): DvcException.__init__(self, 'State file error: {}'.format(msg)) @@ -48,7 +44,6 @@ class StateFile(StateFileBase): locked=None, md5=None): super(StateFile, self).__init__() - self.data_item = data_item self.cmd = cmd