1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-05 23:43:03 +00:00

handle bad diff_command

This commit is contained in:
deadc0de6
2022-06-25 14:58:27 +02:00
parent da272553e0
commit d147dfca5d
3 changed files with 135 additions and 5 deletions

View File

@@ -6,10 +6,12 @@ settings block
"""
import os
from dotdrop.exceptions import YamlException
# local imports
from dotdrop.linktypes import LinkTypes
from dotdrop.dictparser import DictParser
from dotdrop.utils import is_bin_in_path
ENV_WORKDIR = 'DOTDROP_WORKDIR'
@@ -56,6 +58,9 @@ class Settings(DictParser):
key_import_configs = 'import_configs'
key_import_variables = 'import_variables'
# defaults
default_diff_command = 'diff -r -u {0} {1}'
def __init__(self, backup=True, banner=True,
create=True, default_actions=None, dotpath='dotfiles',
ignoreempty=False, import_actions=None, import_configs=None,
@@ -66,7 +71,7 @@ class Settings(DictParser):
impignore=None, workdir='~/.config/dotdrop',
showdiff=False, minversion=None,
func_file=None, filter_file=None,
diff_command='diff -r -u {0} {1}',
diff_command=default_diff_command,
template_dotfile_default=True,
ignore_missing_in_dotdrop=False,
force_chmod=False, chmod_on_import=False,
@@ -108,6 +113,11 @@ class Settings(DictParser):
self.key_prefix = key_prefix
self.key_separator = key_separator
# check diff command
if not is_bin_in_path(self.diff_command):
err = 'bad diff_command: {}'.format(self.diff_command)
raise YamlException(err)
def _serialize_seq(self, name, dic):
"""serialize attribute 'name' into 'dic'"""
seq = getattr(self, name)

View File

@@ -14,7 +14,7 @@ import inspect
import importlib
import filecmp
import itertools
from shutil import rmtree, which
import shutil
import json
import requests
from packaging import version
@@ -176,7 +176,7 @@ def removepath(path, logger=None):
if os.path.islink(path) or os.path.isfile(path):
os.unlink(path)
elif os.path.isdir(path):
rmtree(path)
shutil.rmtree(path)
else:
err = 'Unsupported file type for deletion: {}'.format(path)
raise OSError(err)
@@ -343,10 +343,11 @@ def get_module_from_path(path):
def dependencies_met():
"""make sure all dependencies are met"""
# check unix tools deps
deps = ['file', 'diff']
# diff command is checked in settings.py
deps = ['file']
err = 'The tool \"{}\" was not found in the PATH!'
for dep in deps:
if not which(dep):
if not shutil.which(dep):
raise UnmetDependency(err.format(dep))
# check python deps
err = 'missing python module \"{}\"'
@@ -381,6 +382,13 @@ def dependencies_met():
assert YAML
except ImportError as exc:
raise Exception(err.format('ruamel.yaml')) from exc
# toml
try:
import toml
assert toml
except ImportError as exc:
raise Exception(err.format('toml')) from exc
# pylint: enable=C0415
@@ -494,3 +502,25 @@ def pivot_path(path, newdir, striphome=False, logger=None):
if logger:
logger.dbg('pivot \"{}\" to \"{}\"'.format(path, new))
return new
def is_bin_in_path(command):
"""
check binary from command is in path
"""
bpath = ""
if not command:
return False
try:
binary = command.split(" ")[0]
except ValueError:
return False
if not binary:
return False
try:
bpath = shutil.which(binary)
except shutil.Error:
return False
if not bpath:
return False
return os.path.exists(bpath)