1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 02:19:14 +00:00
This commit is contained in:
deadc0de6
2021-04-29 20:55:55 +02:00
parent 1da3ca6da1
commit 4683460206

View File

@@ -38,10 +38,10 @@ def run(cmd, debug=False):
"""run a command (expects a list)""" """run a command (expects a list)"""
if debug: if debug:
LOG.dbg('exec: {}'.format(' '.join(cmd)), force=True) LOG.dbg('exec: {}'.format(' '.join(cmd)), force=True)
p = subprocess.Popen(cmd, shell=False, proc = subprocess.Popen(cmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = p.communicate() out, _ = proc.communicate()
ret = p.returncode ret = proc.returncode
out = out.splitlines(keepends=True) out = out.splitlines(keepends=True)
lines = ''.join([x.decode('utf-8', 'replace') for x in out]) lines = ''.join([x.decode('utf-8', 'replace') for x in out])
return ret == 0, lines return ret == 0, lines
@@ -50,8 +50,8 @@ def run(cmd, debug=False):
def write_to_tmpfile(content): def write_to_tmpfile(content):
"""write some content to a tmp file""" """write some content to a tmp file"""
path = get_tmpfile() path = get_tmpfile()
with open(path, 'wb') as f: with open(path, 'wb') as file:
f.write(content) file.write(content)
return path return path
@@ -92,25 +92,27 @@ def diff(original, modified,
def get_tmpdir(): def get_tmpdir():
"""create and return the temporary directory""" """create and return the temporary directory"""
# pylint: disable=W0603
global TMPDIR global TMPDIR
# pylint: enable=W0603
if TMPDIR: if TMPDIR:
return TMPDIR return TMPDIR
t = _get_tmpdir() tmp = _get_tmpdir()
TMPDIR = t TMPDIR = tmp
return t return tmp
def _get_tmpdir(): def _get_tmpdir():
"""create the tmpdir""" """create the tmpdir"""
try: try:
if ENV_TEMP in os.environ: if ENV_TEMP in os.environ:
t = os.environ[ENV_TEMP] tmp = os.environ[ENV_TEMP]
t = os.path.expanduser(t) tmp = os.path.expanduser(tmp)
t = os.path.abspath(t) tmp = os.path.abspath(tmp)
t = os.path.normpath(t) tmp = os.path.normpath(tmp)
os.makedirs(t, exist_ok=True) os.makedirs(tmp, exist_ok=True)
return t return tmp
except Exception: except OSError:
pass pass
return tempfile.mkdtemp(prefix='dotdrop-') return tempfile.mkdtemp(prefix='dotdrop-')
@@ -159,12 +161,12 @@ def removepath(path, logger=None):
else: else:
err = 'Unsupported file type for deletion: {}'.format(path) err = 'Unsupported file type for deletion: {}'.format(path)
raise OSError(err) raise OSError(err)
except Exception as e: except Exception as exc:
err = str(e) err = str(exc)
if logger: if logger:
logger.warn(err) logger.warn(err)
return return
raise OSError(err) raise OSError(err) from exc
def samefile(path1, path2): def samefile(path1, path2):
@@ -207,29 +209,32 @@ def must_ignore(paths, ignores, debug=False):
force=True) force=True)
ignored_negative, ignored = categorize( ignored_negative, ignored = categorize(
lambda ign: ign.startswith('!'), ignores) lambda ign: ign.startswith('!'), ignores)
for p in paths: for path in paths:
ignore_matches = [] ignore_matches = []
# First ignore dotfiles # First ignore dotfiles
for i in ignored: for i in ignored:
if fnmatch.fnmatch(p, i): if fnmatch.fnmatch(path, i):
if debug: if debug:
LOG.dbg('ignore \"{}\" match: {}'.format(i, p), force=True) LOG.dbg('ignore \"{}\" match: {}'.format(i, path),
ignore_matches.append(p)
# Then remove any matches that actually shouldn't be ignored
for ni in ignored_negative:
# Each of these will start with an '!' so we need to remove that
ni = ni[1:]
if fnmatch.fnmatch(p, ni):
if debug:
LOG.dbg('negative ignore \"{}\" match: {}'.format(ni, p),
force=True) force=True)
ignore_matches.append(path)
# Then remove any matches that actually shouldn't be ignored
for nign in ignored_negative:
# Each of these will start with an '!' so we need to remove that
nign = nign[1:]
if fnmatch.fnmatch(path, nign):
if debug:
msg = 'negative ignore \"{}\" match: {}'.format(nign, path)
LOG.dbg(msg, force=True)
try: try:
ignore_matches.remove(p) ignore_matches.remove(path)
except ValueError: except ValueError:
LOG.warn('no files that are currently being ignored match ' LOG.warn('no files that are currently being '
'\"{}\". In order for a negative ignore pattern ' 'ignored match \"{}\". In order '
'to work, it must match a file that is being ' 'for a negative ignore pattern '
'ignored by a previous ignore pattern.'.format(ni) 'to work, it must match a file '
'that is being ignored by a '
'previous ignore pattern.'.format(nign)
) )
if ignore_matches: if ignore_matches:
return True return True
@@ -241,9 +246,9 @@ def must_ignore(paths, ignores, debug=False):
def uniq_list(a_list): def uniq_list(a_list):
"""unique elements of a list while preserving order""" """unique elements of a list while preserving order"""
new = [] new = []
for a in a_list: for elem in a_list:
if a not in new: if elem not in new:
new.append(a) new.append(elem)
return new return new
@@ -286,8 +291,8 @@ def patch_ignores(ignores, prefix, debug=False):
def get_module_functions(mod): def get_module_functions(mod):
"""return a list of fonction from a module""" """return a list of fonction from a module"""
funcs = [] funcs = []
for m in inspect.getmembers(mod): for memb in inspect.getmembers(mod):
name, func = m name, func = memb
if not inspect.isfunction(func): if not inspect.isfunction(func):
continue continue
funcs.append((name, func)) funcs.append((name, func))
@@ -315,10 +320,11 @@ def dependencies_met():
# check python deps # check python deps
err = 'missing python module \"{}\"' err = 'missing python module \"{}\"'
# pylint: disable=C0415
# python-magic # python-magic
try: try:
import magic import magic
assert(magic) assert magic
if not hasattr(magic, 'from_file'): if not hasattr(magic, 'from_file'):
LOG.warn(err.format('python-magic')) LOG.warn(err.format('python-magic'))
except ImportError: except ImportError:
@@ -327,23 +333,24 @@ def dependencies_met():
# docopt # docopt
try: try:
from docopt import docopt from docopt import docopt
assert(docopt) assert docopt
except ImportError: except ImportError as exc:
raise Exception(err.format('docopt')) raise Exception(err.format('docopt')) from exc
# jinja2 # jinja2
try: try:
import jinja2 import jinja2
assert(jinja2) assert jinja2
except ImportError: except ImportError as exc:
raise Exception(err.format('jinja2')) raise Exception(err.format('jinja2')) from exc
# ruamel.yaml # ruamel.yaml
try: try:
from ruamel.yaml import YAML from ruamel.yaml import YAML
assert(YAML) assert YAML
except ImportError: except ImportError as exc:
raise Exception(err.format('ruamel.yaml')) raise Exception(err.format('ruamel.yaml')) from exc
# pylint: enable=C0415
def mirror_file_rights(src, dst): def mirror_file_rights(src, dst):
@@ -376,6 +383,7 @@ def get_file_perm(path):
def chmod(path, mode, debug=False): def chmod(path, mode, debug=False):
"""change mode of file"""
if debug: if debug:
LOG.dbg('chmod {} {}'.format(oct(mode), path), force=True) LOG.dbg('chmod {} {}'.format(oct(mode), path), force=True)
os.chmod(path, mode) os.chmod(path, mode)
@@ -383,6 +391,7 @@ def chmod(path, mode, debug=False):
def adapt_workers(options, logger): def adapt_workers(options, logger):
"""adapt number of workers if safe/dry"""
if options.safe and options.workers > 1: if options.safe and options.workers > 1:
logger.warn('workers set to 1 when --force is not used') logger.warn('workers set to 1 when --force is not used')
options.workers = 1 options.workers = 1