mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
Merge pull request #419 from efiop/master
Remove ml-specific code and fix gitignore
This commit is contained in:
+1
-39
@@ -5,7 +5,6 @@ import sys
|
||||
import argparse
|
||||
from multiprocessing import cpu_count
|
||||
|
||||
from dvc.command.find import CmdFind
|
||||
from dvc.command.fsck import CmdFsck
|
||||
from dvc.command.init import CmdInit
|
||||
from dvc.command.remove import CmdRemove
|
||||
@@ -38,17 +37,6 @@ def parse_args(argv=None):
|
||||
default=False,
|
||||
help='Be verbose.')
|
||||
|
||||
parent_parser.add_argument(
|
||||
'-b',
|
||||
'--branch',
|
||||
metavar='BRANCH',
|
||||
help='Execute a command in the branch.')
|
||||
parent_parser.add_argument(
|
||||
'-n',
|
||||
'--new-branch',
|
||||
metavar='BRANCH',
|
||||
help='Create a new branch and execute a command in the branch.')
|
||||
|
||||
# Main parser
|
||||
desc = 'Data Version Control'
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -272,33 +260,7 @@ def parse_args(argv=None):
|
||||
help='Checkout')
|
||||
checkout_parser.set_defaults(func=CmdCheckout)
|
||||
|
||||
# Find
|
||||
find_parser = subparsers.add_parser(
|
||||
'find',
|
||||
parents=[parent_parser],
|
||||
help='Find branch name')
|
||||
find_parser.set_defaults(func=CmdFind)
|
||||
find_parser.add_argument(
|
||||
'branch_name',
|
||||
nargs='?',
|
||||
help='Branch name regexp.')
|
||||
find_parser.add_argument(
|
||||
'target',
|
||||
nargs='?',
|
||||
help='Target metric file.')
|
||||
find_parser.add_argument(
|
||||
'-c',
|
||||
'--criteria',
|
||||
help='Search criteria. By default it finds max.',
|
||||
choices=['max', 'min', 'all'],
|
||||
default='max')
|
||||
find_parser.add_argument(
|
||||
'-s',
|
||||
'--show-value',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Show metrics value.')
|
||||
|
||||
# Fsck
|
||||
fsck_parser = subparsers.add_parser(
|
||||
'fsck',
|
||||
parents=[parent_parser],
|
||||
|
||||
@@ -27,7 +27,6 @@ class CmdBase(object):
|
||||
|
||||
def run_cmd(self):
|
||||
with self.project.lock:
|
||||
with self.project.scm.brancher(self.args.branch, self.args.new_branch):
|
||||
return self.run()
|
||||
|
||||
# Abstract methods that have to be implemented by any inheritance class
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
from dvc.command.common.common_error import CmdCommonError
|
||||
|
||||
|
||||
class BranchChangerError(CmdCommonError):
|
||||
def __init__(self, branch, msg):
|
||||
super(CmdCommonError, self).__init__('Error in changing branch \'{}\': {}'.format(branch, msg))
|
||||
|
||||
|
||||
class BranchChanger(object):
|
||||
def __init__(self, scm, branch, new_branch):
|
||||
if branch and new_branch:
|
||||
raise BranchChangerError("Commands conflict: --branch and --new-branch cannot be used at the same command")
|
||||
|
||||
self.perform_action = branch or new_branch
|
||||
|
||||
self.branch = branch if branch else new_branch
|
||||
self.create_new = new_branch is not None
|
||||
self.scm = scm
|
||||
|
||||
def __enter__(self):
|
||||
if self.perform_action:
|
||||
code, _, err = self.scm.checkout(self.branch, self.create_new)
|
||||
if code != 0:
|
||||
raise BranchChangerError(self.branch, err)
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if self.perform_action:
|
||||
code, _, err = self.scm.checkout('-')
|
||||
if code != 0:
|
||||
raise BranchChangerError(self.branch, err)
|
||||
@@ -1,61 +0,0 @@
|
||||
from dvc import utils
|
||||
from dvc.command.common.base import CmdBase
|
||||
from dvc.executor import ExecutorError
|
||||
from dvc.logger import Logger
|
||||
|
||||
|
||||
class CmdFind(CmdBase):
|
||||
def __init__(self, settings):
|
||||
super(CmdFind, self).__init__(settings)
|
||||
|
||||
def run(self):
|
||||
fname = self.parsed_args.target
|
||||
branch_name = self.parsed_args.branch_name
|
||||
|
||||
branches = self.git.branches(branch_name)
|
||||
metrics = [self.read_metrics(fname, b) for b in branches]
|
||||
|
||||
zipped = zip(metrics, branches)
|
||||
res = self.filter(zipped, self.parsed_args.criteria)
|
||||
res = filter(lambda x: x[0] is not None, res)
|
||||
|
||||
for (val, branch) in res:
|
||||
if self.parsed_args.show_value:
|
||||
print('{}: {}'.format(branch, val))
|
||||
else:
|
||||
print(branch)
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def filter(zipped, criteria):
|
||||
if criteria == 'max':
|
||||
res = [max(zipped, key=lambda t: t[0])]
|
||||
elif criteria == 'min':
|
||||
res = [min(zipped, key=lambda t: t[0])]
|
||||
else:
|
||||
res = zipped
|
||||
return res
|
||||
|
||||
def read_metrics(self, fname, branch):
|
||||
try:
|
||||
lines = self.git.get_file_content(fname, branch).split('\n')
|
||||
except ExecutorError as ex:
|
||||
msg = 'Unable to read metrics file from branch {}: {}'
|
||||
data_item = self.settings.path_factory.data_item(fname)
|
||||
|
||||
try:
|
||||
self.git.get_file_content(data_item.state.relative, branch)
|
||||
Logger.error(msg.format(branch, 'this is data file, not metric file'))
|
||||
except ExecutorError:
|
||||
Logger.error(msg.format(branch, 'file does not exist in this branch'))
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
metric = utils.parse_target_metric(lines)
|
||||
if not metric:
|
||||
msg = 'Unable to parse metrics from the first line of file {} in branch {}'
|
||||
Logger.error(msg.format(fname, branch))
|
||||
return None
|
||||
|
||||
return metric
|
||||
@@ -109,7 +109,6 @@ class Project(object):
|
||||
|
||||
ignore_file = os.path.join(dvc_dir, scm.ignore_file())
|
||||
scm.add([config.config_file, ignore_file])
|
||||
scm.commit('DVC init')
|
||||
|
||||
return Project(root_dir)
|
||||
|
||||
|
||||
+3
-6
@@ -2,7 +2,6 @@ import os
|
||||
import git
|
||||
|
||||
from dvc.exceptions import DvcException
|
||||
from dvc.command.common.branch_changer import BranchChanger
|
||||
|
||||
|
||||
class SCMError(DvcException):
|
||||
@@ -45,9 +44,6 @@ class Base(object):
|
||||
def branch(self, branch):
|
||||
pass
|
||||
|
||||
def brancher(self, branch, new_branch):
|
||||
return BranchChanger(self, branch, new_branch)
|
||||
|
||||
def untracked_files(self):
|
||||
pass
|
||||
|
||||
@@ -84,11 +80,12 @@ class Git(Base):
|
||||
ignore_list = []
|
||||
if os.path.exists(gitignore):
|
||||
ignore_list = open(gitignore, 'r').readlines()
|
||||
if entry in ignore_list:
|
||||
filtered = list(filter(lambda x: x.strip() == entry.strip(), ignore_list))
|
||||
if len(filtered) != 0:
|
||||
return
|
||||
|
||||
content = entry
|
||||
if ignore_list:
|
||||
if len(ignore_list) > 0:
|
||||
content = '\n' + content
|
||||
|
||||
open(gitignore, 'a').write(content)
|
||||
|
||||
@@ -12,7 +12,6 @@ from dvc.command.config import CmdConfig
|
||||
from dvc.command.show_pipeline import CmdShowPipeline
|
||||
from dvc.command.show_workflow import CmdShowWorkflow
|
||||
from dvc.command.checkout import CmdCheckout
|
||||
from dvc.command.find import CmdFind
|
||||
from dvc.command.fsck import CmdFsck
|
||||
|
||||
from tests.basic_env import TestDvc
|
||||
@@ -184,26 +183,6 @@ class TestCheckout(TestDvc):
|
||||
self.assertIsInstance(args.func(args), CmdCheckout)
|
||||
|
||||
|
||||
class TestFind(TestDvc):
|
||||
def test(self):
|
||||
branch = 'branch'
|
||||
target = 'target'
|
||||
criteria = 'max'
|
||||
|
||||
args = parse_args(['find',
|
||||
branch,
|
||||
target,
|
||||
'-c', criteria,
|
||||
'--criteria', criteria,
|
||||
'-s', '--show-value'])
|
||||
|
||||
self.assertIsInstance(args.func(args), CmdFind)
|
||||
self.assertEqual(args.branch_name, branch)
|
||||
self.assertEqual(args.target, target)
|
||||
self.assertEqual(args.criteria, criteria)
|
||||
self.assertTrue(args.show_value)
|
||||
|
||||
|
||||
class TestFsck(TestDvc):
|
||||
def test(self):
|
||||
target1 = 'target1'
|
||||
|
||||
+17
-5
@@ -20,13 +20,25 @@ class TestSCMGit(TestGit):
|
||||
def test_is_repo(self):
|
||||
self.assertTrue(Git.is_repo(os.curdir))
|
||||
|
||||
def test_ignore(self):
|
||||
Git(self._root_dir).ignore('foo')
|
||||
self.assertTrue(os.path.isfile(Git.GITIGNORE))
|
||||
self.assertTrue('foo' in open(Git.GITIGNORE, 'r').readlines())
|
||||
|
||||
def test_commit(self):
|
||||
G = Git(self._root_dir)
|
||||
G.add(['foo'])
|
||||
G.commit('add')
|
||||
self.assertTrue('foo' in self.git.git.ls_files())
|
||||
|
||||
|
||||
class TestIgnore(TestGit):
|
||||
def _count_gitignore(self):
|
||||
with open(Git.GITIGNORE, 'r') as fd:
|
||||
lines = fd.readlines()
|
||||
return len(list(filter(lambda x: x.strip() == 'foo', lines)))
|
||||
|
||||
def test(self):
|
||||
git = Git(self._root_dir)
|
||||
|
||||
git.ignore('foo')
|
||||
self.assertTrue(os.path.isfile(Git.GITIGNORE))
|
||||
self.assertEqual(self._count_gitignore(), 1)
|
||||
|
||||
git.ignore('foo')
|
||||
self.assertEqual(self._count_gitignore(), 1)
|
||||
|
||||
Reference in New Issue
Block a user