target: remove 'target' command

Fixes #209

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2017-09-17 02:41:50 +03:00
parent 6ad073a038
commit 3286af59ef
6 changed files with 18 additions and 197 deletions
-16
View File
@@ -12,7 +12,6 @@ from dvc.command.lock import CmdLock
from dvc.command.gc import CmdGC
from dvc.command.import_file import CmdImportFile
from dvc.command.show_workflow import CmdShowWorkflow
from dvc.command.target import CmdTarget
from dvc.command.instance_create import CmdInstanceCreate
from dvc.command.config import CmdConfig
from dvc.command.show_pipeline import CmdShowPipeline
@@ -273,21 +272,6 @@ def parse_args(argv=None):
help='Do not remove data from cache.')
gc_parser.set_defaults(func=CmdGC)
# Target
target_parser = subparsers.add_parser(
'target',
parents=[parent_parser],
help='Set default target')
target_parser.add_argument('target_file',
nargs='?',
help='Target data item.')
target_parser.add_argument('-u',
'--unset',
action='store_true',
default=False,
help='Reset target.')
target_parser.set_defaults(func=CmdTarget)
# Cloud
ex_parser = subparsers.add_parser(
'ex',
+5 -5
View File
@@ -18,6 +18,9 @@ class CmdInit(CmdBase):
CONFIG_TEMPLATE = '''[Global]
DataDir = {}
# Default target
Target =
# Supported clouds: AWS, GCP
Cloud = AWS
@@ -92,7 +95,6 @@ ProjectName =
data_dir_path = self.get_not_existing_path(self.parsed_args.data_dir)
cache_dir_path = self.get_not_existing_path(Config.CONFIG_DIR, Config.CACHE_DIR)
state_dir_path = self.get_not_existing_path(Config.CONFIG_DIR, Config.STATE_DIR)
target_file_path = self.get_not_existing_path(Config.CONFIG_DIR, Config.TARGET_FILE_DEFAULT)
self.settings.config.set(self.parsed_args.data_dir)
@@ -102,13 +104,11 @@ ProjectName =
data_dir_path.mkdir()
cache_dir_path.mkdir()
state_dir_path.mkdir()
target_file_path.touch()
Logger.info('Directories {}/, {}/, {}/, {}/ and target file {} were created'.format(
Logger.info('Directories {}/, {}/, {}/, {}/ were created'.format(
config_dir_path.name,
data_dir_path.name,
cache_dir_path.name,
state_dir_path.name,
target_file_path.name))
state_dir_path.name))
self.create_empty_file()
+13 -11
View File
@@ -42,18 +42,20 @@ class CmdRepro(CmdRun):
def run(self):
with DvcLock(self.is_locker, self.git):
recursive = not self.parsed_args.single_item
if self.parsed_args.target:
target = self.parsed_args.target
else:
target_conf_file_path = self.settings.path_factory.path(self.settings.config.target_file)
target_file = open(target_conf_file_path.relative).read()
if not target_file:
Logger.error('Reproduction target is not defined. ' +
'Specify data file or set target by `dvc target` command.')
return 1
target = [target_file]
targets = []
return self.repro_target(target, recursive, self.parsed_args.force)
if self.parsed_args.target:
targets += self.parsed_args.target
else:
target = self.settings.config.get('Target', None)
if not target or len(target) == 0:
Logger.error('Reproduction target is not defined. ' +
'Specify data file or set target by ' +
'`dvc config global.target target` command.')
return 1
targets += [target]
return self.repro_target(targets, recursive, self.parsed_args.force)
pass
def repro_target(self, target, recursive, force):
-56
View File
@@ -1,56 +0,0 @@
import os
from dvc.command.base import CmdBase, DvcLock
from dvc.logger import Logger
class CmdTarget(CmdBase):
def __init__(self, settings):
super(CmdTarget, self).__init__(settings)
def run(self):
with DvcLock(self.is_locker, self.git):
target = self.parsed_args.target_file
unset = self.parsed_args.unset
if target and unset:
Logger.error('Unable to set target {} and use --unset in a single command'.format(target))
return 1
if not target and not unset:
Logger.error('Target is not defined')
return 1
target_conf_file_path = self.settings.path_factory.path(self.settings.config.target_file).relative
return self.change_target(target, unset, target_conf_file_path)
def change_target(self, target, unset, target_conf_file_path):
if unset:
return self.unset_target(target_conf_file_path)
else:
target_dvc_path = self.settings.path_factory.existing_data_item(target).data.dvc
if os.path.exists(target_conf_file_path):
with open(target_conf_file_path) as fd:
if fd.read() == target_dvc_path:
return 0
with open(target_conf_file_path, 'w') as fd:
fd.write(target_dvc_path)
return self.commit_if_needed('DVC target: {}'.format(target))
def unset_target(self, target_conf_file_path):
if not os.path.exists(target_conf_file_path):
Logger.error('Target conf file {} does not exists'.format(
target_conf_file_path))
return 1
if not os.path.isfile(target_conf_file_path):
Logger.error('Target conf file {} exists but it is not a regular file'.format(
target_conf_file_path))
return 1
if open(target_conf_file_path).read() == '':
return 0
os.remove(target_conf_file_path)
open(target_conf_file_path, 'a').close()
return self.commit_if_needed('DVC target unset')
-4
View File
@@ -40,10 +40,6 @@ class ConfigI(object):
def state_dir(self):
return os.path.join(self.CONFIG_DIR, self.STATE_DIR)
@property
def target_file(self):
return os.path.join(self.CONFIG_DIR, self.TARGET_FILE_DEFAULT)
@property
def cloud(self):
return self._cloud
-105
View File
@@ -1,105 +0,0 @@
import os
from dvc.command.target import CmdTarget
from dvc.config import ConfigI
from dvc.git_wrapper import GitWrapperI
from dvc.path.factory import PathFactory
from dvc.settings import Settings
from dvc.system import System
from tests.basic_env import BasicEnvironment
class TestCmdTarget(BasicEnvironment):
def setUp(self):
self._test_dir = os.path.join(os.path.sep, 'tmp', 'dvc_unit_test')
curr_dir = None
commit = 'abc1234'
BasicEnvironment.init_environment(self, self._test_dir, curr_dir)
self._commit = commit
self._git = GitWrapperI(git_dir=self._test_git_dir, commit=self._commit)
self._config = ConfigI('data')
self.path_factory = PathFactory(self._git, self._config)
self.settings = Settings(['target'], self._git, self._config)
os.chdir(self._test_git_dir)
os.mkdir(os.path.join('data', 'dir1'))
os.mkdir(os.path.join('.dvc', 'cache', 'dir1'))
self.file1_cache = os.path.join('.dvc', 'cache', 'dir1', 'file1')
self.file1_data = os.path.join('data', 'dir1', 'file1')
open(self.file1_cache, 'w').write('ddfdff')
System.symlink(self.file1_cache, self.file1_data)
self.file2_cache = os.path.join('.dvc', 'cache', 'file2')
self.file2_data = os.path.join('data', 'file2')
open(self.file2_cache, 'w').write('ddfdff')
System.symlink(self.file2_cache, self.file2_data)
def test_initial_default_target(self):
self.assertFalse(os.path.exists('.dvc/target'))
def test_single_file(self):
self.settings.parse_args('target {}'.format(self.file1_data))
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), self.file1_data)
def test_multiple_files(self):
self.settings.parse_args('target {}'.format(self.file1_data))
cmd = CmdTarget(self.settings)
cmd.run()
# Another target
self.settings.parse_args('target {}'.format(self.file2_data))
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), self.file2_data)
# Unset target
self.settings.parse_args('target --unset')
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), '')
def test_initial_unset(self):
self.settings.parse_args('target --unset')
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 1)
self.assertFalse(os.path.exists(os.path.join('.dvc', '.target')))
def test_unset_existing_target(self):
self.settings.parse_args('target {}'.format(self.file1_data))
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), self.file1_data)
self.settings.parse_args('target --unset')
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), '')
def test_the_same(self):
self.settings.parse_args('target {}'.format(self.file1_data))
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), self.file1_data)
self.assertEqual(cmd.run(), 0)
self.assertEqual(open(os.path.join('.dvc', 'target')).read(), self.file1_data)
def test_args_conflict(self):
self.settings.parse_args('target {} --unset'.format(self.file1_data))
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 1)
def test_no_args(self):
self.settings.parse_args('target --unset')
cmd = CmdTarget(self.settings)
self.assertEqual(cmd.run(), 1)