Merge pull request #420 from efiop/master

Misc bug fixes
This commit is contained in:
Ruslan Kuprieiev
2018-01-14 22:31:33 +02:00
committed by GitHub
8 changed files with 53 additions and 10 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ class CmdCheckout(CmdBase):
os.remove(file)
dir = os.path.dirname(file)
if not os.listdir(dir):
if len(dir) != 0 and not os.listdir(dir):
Logger.info(u'Remove empty directory \'{}\''.format(dir))
os.removedirs(dir)
pass
+6 -1
View File
@@ -3,6 +3,7 @@ import os
from dvc.project import Project
from dvc.logger import Logger
from dvc.exceptions import DvcException
from dvc.lock import LockError
class CmdBase(object):
@@ -26,8 +27,12 @@ class CmdBase(object):
raise DvcException(msg.format(root))
def run_cmd(self):
with self.project.lock:
try:
with self.project.lock:
return self.run()
except LockError as ex:
self.project.logger.error(str(ex))
return 1
# Abstract methods that have to be implemented by any inheritance class
def run(self):
+8 -1
View File
@@ -1,5 +1,11 @@
import os
import fasteners
from dvc.exceptions import DvcException
class LockError(DvcException):
pass
class Lock(object):
LOCK_FILE = 'lock'
@@ -14,7 +20,8 @@ class Lock(object):
return Lock(dvc_dir)
def lock(self):
self._lock.acquire(timeout=self.TIMEOUT)
if not self._lock.acquire(timeout=self.TIMEOUT):
raise LockError('Cannot perform the cmd since DVC is busy and locked. Please retry the cmd later.')
def unlock(self):
self._lock.release()
+4 -5
View File
@@ -25,7 +25,9 @@ class Logger(object):
}
def __init__(self, config=None):
self.init(config)
if config:
level = config['Global'].get('LogLevel', None)
Logger.set_level(level)
@staticmethod
def init(config=None):
@@ -34,10 +36,7 @@ class Logger(object):
sh.setLevel(logging.DEBUG)
Logger.logger().addHandler(sh)
level = None
if config:
level = config['Global'].get('LogLevel', None)
Logger.set_level(level)
Logger.set_level()
@staticmethod
def logger():
+7 -1
View File
@@ -12,4 +12,10 @@ def main(argv=None):
Logger.error('Initialization error: {}'.format(str(ex)))
return 255
return cmd.run_cmd()
try:
ret = cmd.run_cmd()
except Exception as ex:
Logger.error('Unexpected error: {}'.format(str(ex)))
return 254
return ret
+23 -1
View File
@@ -23,6 +23,9 @@ class Base(object):
def ignore(self, path):
pass
def ignore_remove(self, path):
pass
def ignore_file(self):
pass
@@ -68,7 +71,7 @@ class Git(Base):
git_dir = os.path.join(root_dir, Git.GIT_DIR)
return os.path.isdir(git_dir)
def ignore(self, path):
def _get_gitignore(self, path):
entry = os.path.basename(path)
gitignore = os.path.join(os.path.abspath(os.path.dirname(path)),
self.GITIGNORE)
@@ -77,6 +80,11 @@ class Git(Base):
if not gitignore.startswith(self.root_dir):
raise FileNotInRepoError()
return entry, gitignore
def ignore(self, path):
entry, gitignore = self._get_gitignore(path)
ignore_list = []
if os.path.exists(gitignore):
ignore_list = open(gitignore, 'r').readlines()
@@ -90,6 +98,20 @@ class Git(Base):
open(gitignore, 'a').write(content)
def ignore_remove(self, path):
entry, gitignore = self._get_gitignore(path)
if not os.path.exists(gitignore):
return
with open(gitignore, 'r') as fd:
lines = fd.readlines()
filtered = list(filter(lambda x: x.strip() != entry.strip(), lines))
with open(gitignore, 'w') as fd:
fd.writelines(filtered)
def add(self, paths):
self.repo.index.add(paths)
+1
View File
@@ -242,6 +242,7 @@ 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)
+3
View File
@@ -42,3 +42,6 @@ class TestIgnore(TestGit):
git.ignore('foo')
self.assertEqual(self._count_gitignore(), 1)
git.ignore_remove('foo')
self.assertEqual(self._count_gitignore(), 0)