diff --git a/dvc/cli.py b/dvc/cli.py index 1aaf42f9a..3b34fdf8e 100644 --- a/dvc/cli.py +++ b/dvc/cli.py @@ -230,7 +230,7 @@ def parse_args(argv=None): workflow_parser.add_argument( 'target', nargs='?', - help='Target metric data file') + help='Target metric file') workflow_parser.add_argument( '-d', '--dvc-commits', diff --git a/dvc/command/checkout.py b/dvc/command/checkout.py index 28a588e27..a3d355967 100644 --- a/dvc/command/checkout.py +++ b/dvc/command/checkout.py @@ -1,4 +1,5 @@ import os +import stat from dvc.command.common.base import CmdBase from dvc.logger import Logger @@ -15,6 +16,7 @@ class CmdCheckout(CmdBase): for file in self.project.cache.find_cache(untracked).keys(): Logger.info(u'Remove \'{}\''.format(file)) + os.chmod(file, stat.S_IWRITE) os.remove(file) dir = os.path.dirname(file) diff --git a/dvc/command/common/base.py b/dvc/command/common/base.py index f0c7e98b9..3e024ade9 100644 --- a/dvc/command/common/base.py +++ b/dvc/command/common/base.py @@ -23,7 +23,7 @@ class CmdBase(object): if os.path.isdir(dvc_dir): return root root = os.path.dirname(root) - msg = "Not a dvc repository (or any parent up to mount point {})" + msg = "Not a dvc repository (checked up to mount point {})" raise DvcException(msg.format(root)) def run_cmd(self): diff --git a/dvc/command/show_workflow.py b/dvc/command/show_workflow.py index c91c7bc30..59d51d0f3 100644 --- a/dvc/command/show_workflow.py +++ b/dvc/command/show_workflow.py @@ -7,6 +7,7 @@ from dvc.command.common.base import CmdBase from dvc.graph.workflow import Workflow from dvc.graph.commit import Commit from dvc.executor import Executor, ExecutorError +from dvc.utils import parse_target_metric_file class CmdShowWorkflow(CmdBase): @@ -48,7 +49,7 @@ class CmdShowWorkflow(CmdBase): if target not in changed_files: return False, None - metric = utils.parse_target_metric_file(target) + metric = parse_target_metric_file(target) if metric is None: return False, None diff --git a/dvc/config.py b/dvc/config.py index 3f3323b85..e9765766b 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -5,7 +5,6 @@ import os import configparser from dvc.exceptions import DvcException -from dvc.logger import Logger class ConfigError(DvcException): @@ -16,6 +15,7 @@ class ConfigError(DvcException): class Config(object): CONFIG = 'config' + SECTION_GLOBAL = 'Global' CONFIG_TEMPLATE = ''' [Global] # Supported clouds: AWS, GCP @@ -59,9 +59,16 @@ ProjectName = def __init__(self, dvc_dir): self.dvc_dir = os.path.abspath(os.path.realpath(dvc_dir)) self.config_file = os.path.join(dvc_dir, self.CONFIG) - + self._config = configparser.SafeConfigParser() - self._config.read(self.config_file) + + try: + self._config.read(self.config_file) + except configparser.Error as ex: + raise ConfigError(ex.message) + + if not self._config.has_section(self.SECTION_GLOBAL): + raise ConfigError(u'section \'{}\' was not found'.format(self.SECTION_GLOBAL)) @staticmethod def init(dvc_dir): diff --git a/dvc/logger.py b/dvc/logger.py index 6985e3cbf..619146856 100644 --- a/dvc/logger.py +++ b/dvc/logger.py @@ -1,8 +1,8 @@ import sys import logging - import colorama +from dvc.config import Config colorama.init() @@ -26,7 +26,7 @@ class Logger(object): def __init__(self, config=None): if config: - level = config['Global'].get('LogLevel', None) + level = config[Config.SECTION_GLOBAL].get('LogLevel', None) Logger.set_level(level) @staticmethod diff --git a/dvc/stage.py b/dvc/stage.py index 0a180c67f..1de91b039 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -232,7 +232,6 @@ class Output(Dependency): if self.project.scm.is_tracked(self.path): raise CmdOutputAlreadyTrackedError(self.path) - self.project.scm.ignore(self.path) self.link() def remove(self): @@ -242,7 +241,6 @@ class Output(Dependency): self.project.logger.debug("Removing '{}'".format(self.path)) os.chmod(self.path, stat.S_IWUSR) os.unlink(self.path) - self.project.scm.ignore_remove(self.path) if os.path.exists(self.cache): os.chmod(self.cache, stat.S_IREAD) @@ -295,6 +293,7 @@ class Stage(object): def remove_outs(self): for out in self.outs: out.remove() + self.project.scm.ignore_remove(out.path) def remove(self): self.remove_outs() @@ -354,6 +353,7 @@ class Stage(object): for out in self.outs: out.update() out.save() + self.project.scm.ignore(out.path) def run(self): if not self.is_data_source: diff --git a/tests/test_checkout.py b/tests/test_checkout.py index 62bc25193..9a079cbde 100644 --- a/tests/test_checkout.py +++ b/tests/test_checkout.py @@ -4,11 +4,7 @@ import shutil import filecmp from dvc.main import main -from dvc.data_cloud import file_md5 -from dvc.stage import Stage, CmdOutputNoCacheError, CmdOutputOutsideOfRepoError -from dvc.stage import CmdOutputDoesNotExistError, CmdOutputIsNotFileError -from dvc.project import StageNotFoundError - +from tests.basic_env import TestDvc from tests.test_repro import TestRepro @@ -35,3 +31,93 @@ class TestCmdCheckout(TestCheckout): ret = main(['checkout']) self.assertEqual(ret, 0) self._test_checkout() + + +class CheckoutBase(TestDvc): + GIT_IGNORE = '.gitignore' + + def commit_data_file(self, fname, content='random text'): + with open(fname, 'w') as fd: + fd.write(content) + self.dvc.add(fname) + self.dvc.scm.add([fname + '.dvc', '.gitignore']) + self.dvc.scm.commit('adding ' + fname) + + def read_ignored(self): + return list(map(lambda s: s.strip('\n'), open(self.GIT_IGNORE).readlines())) + + +class TestRemoveFilesWhenCheckout(CheckoutBase): + def test(self): + fname = 'file_in_a_branch' + branch_master = 'master' + branch_1 = 'b1' + + self.dvc.scm.add(self.dvc.scm.untracked_files()) + self.dvc.scm.commit('add all files') + + # add the file into a separate branch + self.dvc.scm.checkout(branch_1, True) + main(['checkout']) + self.commit_data_file(fname) + + # Checkout back in master + self.dvc.scm.checkout(branch_master) + self.assertTrue(os.path.exists(fname)) + + # Make sure `dvc checkout` removes the file + # self.dvc.checkout() + main(['checkout']) + self.assertFalse(os.path.exists(fname)) + + +class TestGitIgnoreBasic(CheckoutBase): + def test(self): + fname1 = 'file_1' + fname2 = 'file_2' + + self.dvc.scm.add(self.dvc.scm.untracked_files()) + self.dvc.scm.commit('add all files') + + self.assertFalse(os.path.exists(self.GIT_IGNORE)) + + self.commit_data_file(fname1) + self.commit_data_file(fname2) + + self.assertTrue(os.path.exists(self.GIT_IGNORE)) + + ignored = self.read_ignored() + + self.assertEqual(len(ignored), 2) # FAILS!!! + + self.assertIn(fname1, ignored) + self.assertIn(fname2, ignored) # FAILS!!! + + +class TestGitIgnoreWhenCheckout(CheckoutBase): + def test(self): + fname_master = 'file_in_a_master' + branch_master = 'master' + fname_branch = 'file_in_a_branch' + branch_1 = 'b1' + + self.dvc.scm.add(self.dvc.scm.untracked_files()) + self.dvc.scm.commit('add all files') + self.commit_data_file(fname_master) + + self.dvc.scm.checkout(branch_1, True) + main(['checkout']) + self.commit_data_file(fname_branch) + + self.dvc.scm.checkout(branch_master) + main(['checkout']) + + ignored = self.read_ignored() + + self.assertEqual(len(ignored), 1) + self.assertIn(fname_master, ignored) + + self.dvc.scm.checkout(branch_1) + main(['checkout']) + ignored = self.read_ignored() + self.assertIn(fname_branch, ignored) # FAILS!!!