Extract small classes to files

This commit is contained in:
Dmitry
2017-11-19 03:47:41 -08:00
parent 1349589ec7
commit 47e72559f2
5 changed files with 67 additions and 65 deletions
+3 -56
View File
@@ -1,59 +1,6 @@
import fasteners
from dvc.exceptions import DvcException
from dvc.logger import Logger
class CmdBaseError(DvcException):
def __init__(self, msg):
super(CmdBaseError, self).__init__('{}'.format(msg))
class DvcLockerError(CmdBaseError):
def __init__(self, msg):
super(DvcLockerError, self).__init__('DVC locker error: {}'.format(msg))
class DvcLock(object):
def __init__(self, is_locker, git):
self.is_locker = is_locker
self.git = git
self.lock = None
def __enter__(self):
if self.is_locker:
self.lock = fasteners.InterProcessLock(self.git.lock_file)
if not self.lock.acquire(timeout=5):
raise DvcLockerError('Cannot perform the cmd since DVC is busy and locked. Please retry the cmd later.')
return self.lock
def __exit__(self, type, value, traceback):
if self.is_locker:
self.lock.release()
class ForeignBranch(object):
def __init__(self, branch, new_branch, git):
self.branch = branch
self.new_branch = new_branch
self.git = git
if branch and new_branch:
raise DvcLockerError("Commands conflict: --branch and --new-branch cannot be used at the same command")
self.perform_action = branch or new_branch
def __enter__(self):
if self.perform_action:
if self.branch:
self.git.checkout(self.branch)
else:
self.git.checkout_new(self.new_branch)
return self
def __exit__(self, type, value, traceback):
if self.perform_action:
self.git.checkout()
from dvc.command.common.branch_changer import BranchChanger
from dvc.command.common.dvc_lock import DvcLock
class CmdBase(object):
@@ -120,7 +67,7 @@ class CmdBase(object):
Logger.warn('changes were not committed to git')
def run_cmd(self):
with ForeignBranch(self.parsed_args.branch, self.parsed_args.new_branch, self.git):
with BranchChanger(self.parsed_args.branch, self.parsed_args.new_branch, self.git):
return self.run()
# Abstract method that has to be implemented by any inheritance class
+28
View File
@@ -0,0 +1,28 @@
from dvc.command.common.common_error import CmdCommonError
class BranchChangerError(CmdCommonError):
def __init__(self, msg):
super(CmdCommonError, self).__init__('DVC locker error: {}'.format(msg))
class BranchChanger(object):
def __init__(self, branch, new_branch, git):
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.git = git
def __enter__(self):
if self.perform_action:
print('========PERFORM')
self.git.checkout(self.branch, self.create_new)
return self
def __exit__(self, type, value, traceback):
if self.perform_action:
self.git.checkout_previous()
+6
View File
@@ -0,0 +1,6 @@
from dvc.exceptions import DvcException
class CmdCommonError(DvcException):
def __init__(self, msg):
super(CmdCommonError, self).__init__('{}'.format(msg))
+26
View File
@@ -0,0 +1,26 @@
import fasteners
from dvc.command.common.common_error import CmdCommonError
class DvcLockerError(CmdCommonError):
def __init__(self, msg):
super(DvcLockerError, self).__init__('DVC locker error: {}'.format(msg))
class DvcLock(object):
def __init__(self, is_locker, git):
self.is_locker = is_locker
self.git = git
self.lock = None
def __enter__(self):
if self.is_locker:
self.lock = fasteners.InterProcessLock(self.git.lock_file)
if not self.lock.acquire(timeout=5):
raise DvcLockerError('Cannot perform the cmd since DVC is busy and locked. Please retry the cmd later.')
return self.lock
def __exit__(self, type, value, traceback):
if self.is_locker:
self.lock.release()
+4 -9
View File
@@ -317,15 +317,10 @@ class GitWrapper(GitWrapperI):
Executor.exec_cmd_only_success(cmd.split())
@staticmethod
def checkout(commit_or_branch):
Logger.debug('[dvc-git] Checkout {}'.format(commit_or_branch))
cmd = 'git checkout {}'.format(commit_or_branch)
Executor.exec_cmd(cmd.split())
@staticmethod
def checkout_new(branch):
Logger.debug('[dvc-git] Checkout new branch {}'.format(branch))
cmd = 'git checkout -b{}'.format(branch)
def checkout(commit_or_branch, create_new=False):
prefix = '-b ' if create_new else ''
Logger.debug('[dvc-git] Checkout {}{}'.format(prefix, commit_or_branch))
cmd = 'git checkout {}{}'.format(prefix, commit_or_branch)
Executor.exec_cmd(cmd.split())
@staticmethod