Make error handling more robust

This commit is contained in:
Kristóf Tóth 2018-07-19 15:05:43 +02:00
parent a703ee821d
commit 0dd66c33bf

View File

@ -2,7 +2,7 @@
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
import re import re
from subprocess import run from subprocess import run, CalledProcessError
from getpass import getuser from getpass import getuser
from os.path import isdir from os.path import isdir
from datetime import datetime from datetime import datetime
@ -11,7 +11,8 @@ from uuid import uuid4
class SnapshotProvider: class SnapshotProvider:
def __init__(self, directory, git_dir): def __init__(self, directory, git_dir):
author = f'{getuser()} via TFW {self.__class__.__name__}' self._classname = self.__class__.__name__
author = f'{getuser()} via TFW {self._classname}'
self.gitenv = { self.gitenv = {
'GIT_DIR': git_dir, 'GIT_DIR': git_dir,
'GIT_WORK_TREE': directory, 'GIT_WORK_TREE': directory,
@ -32,13 +33,16 @@ class SnapshotProvider:
self._run(('git', 'init')) self._run(('git', 'init'))
if self._number_of_commits == 0: if self._number_of_commits == 0:
self._snapshot() try:
self._snapshot()
except CalledProcessError:
raise EnvironmentError(f'{self._classname} cannot init on empty directories!')
self._check_head_not_detached() self._check_head_not_detached()
def _check_environment(self): def _check_environment(self):
if not isdir(self.gitenv['GIT_DIR']) or not isdir(self.gitenv['GIT_WORK_TREE']): if not isdir(self.gitenv['GIT_DIR']) or not isdir(self.gitenv['GIT_WORK_TREE']):
raise EnvironmentError('Directories "directory" and "git_dir" must exist!') raise EnvironmentError(f'{self._classname}: "directory" and "git_dir" must exist!')
@property @property
def _repo_is_initialized(self): def _repo_is_initialized(self):
@ -69,7 +73,7 @@ class SnapshotProvider:
def _check_head_not_detached(self): def _check_head_not_detached(self):
if self._head_detached: if self._head_detached:
raise EnvironmentError(f'{self.__class__.__name__} cannot init from detached HEAD state!') raise EnvironmentError(f'{self._classname} cannot init from detached HEAD state!')
@property @property
def _head_detached(self): def _head_detached(self):