mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-26 07:18:31 +01:00
Extract small classes to files
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
@@ -0,0 +1,6 @@
|
||||
from dvc.exceptions import DvcException
|
||||
|
||||
|
||||
class CmdCommonError(DvcException):
|
||||
def __init__(self, msg):
|
||||
super(CmdCommonError, self).__init__('{}'.format(msg))
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user