1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 07:29:17 +00:00
This commit is contained in:
deadc0de6
2021-04-29 14:31:45 +02:00
parent d7dc7da33b
commit d03bfaed6d

View File

@@ -17,10 +17,11 @@ from dotdrop.comparator import Comparator
class Importer: class Importer:
"""dotfile importer"""
def __init__(self, profile, conf, dotpath, diff_cmd, def __init__(self, profile, conf, dotpath, diff_cmd,
dry=False, safe=True, debug=False, dry=False, safe=True, debug=False,
keepdot=True, ignore=[]): keepdot=True, ignore=None):
"""constructor """constructor
@profile: the selected profile @profile: the selected profile
@conf: configuration manager @conf: configuration manager
@@ -40,6 +41,8 @@ class Importer:
self.safe = safe self.safe = safe
self.debug = debug self.debug = debug
self.keepdot = keepdot self.keepdot = keepdot
if not ignore:
ignore = []
self.ignore = ignore self.ignore = ignore
self.umask = get_umask() self.umask = get_umask()
@@ -124,6 +127,16 @@ class Importer:
if not self._prepare_hierarchy(src, dst): if not self._prepare_hierarchy(src, dst):
return -1 return -1
return self._import_it(path, src, dst, perm, linktype, import_mode)
def _import_it(self, path, src, dst, perm, linktype, import_mode):
"""
import path
returns:
1: 1 dotfile imported
0: ignored
"""
# handle file mode # handle file mode
chmod = None chmod = None
dflperm = get_default_file_perms(dst, self.umask) dflperm = get_default_file_perms(dst, self.umask)
@@ -147,19 +160,16 @@ class Importer:
def _prepare_hierarchy(self, src, dst): def _prepare_hierarchy(self, src, dst):
"""prepare hierarchy for dotfile""" """prepare hierarchy for dotfile"""
srcf = os.path.join(self.dotpath, src) srcf = os.path.join(self.dotpath, src)
if self._ignore(srcf):
return False
srcfd = os.path.dirname(srcf) srcfd = os.path.dirname(srcf)
if self._ignore(srcfd): if self._ignore(srcf) or self._ignore(srcfd):
return False return False
# a dotfile in dotpath already exists at that spot # a dotfile in dotpath already exists at that spot
if os.path.exists(srcf): if os.path.exists(srcf):
if self.safe: if self.safe:
c = Comparator(debug=self.debug, cmp = Comparator(debug=self.debug,
diff_cmd=self.diff_cmd) diff_cmd=self.diff_cmd)
diff = c.compare(srcf, dst) diff = cmp.compare(srcf, dst)
if diff != '': if diff != '':
# files are different, dunno what to do # files are different, dunno what to do
self.log.log('diff \"{}\" VS \"{}\"'.format(dst, srcf)) self.log.log('diff \"{}\" VS \"{}\"'.format(dst, srcf))
@@ -178,7 +188,7 @@ class Importer:
else: else:
try: try:
os.makedirs(srcfd, exist_ok=True) os.makedirs(srcfd, exist_ok=True)
except Exception: except OSError:
self.log.err('importing \"{}\" failed!'.format(dst)) self.log.err('importing \"{}\" failed!'.format(dst))
return False return False
@@ -190,15 +200,15 @@ class Importer:
if os.path.isdir(dst): if os.path.isdir(dst):
if os.path.exists(srcf): if os.path.exists(srcf):
shutil.rmtree(srcf) shutil.rmtree(srcf)
ig = shutil.ignore_patterns(*self.ignore) ign = shutil.ignore_patterns(*self.ignore)
shutil.copytree(dst, srcf, shutil.copytree(dst, srcf,
copy_function=self._cp, copy_function=self._cp,
ignore=ig) ignore=ign)
else: else:
shutil.copy2(dst, srcf) shutil.copy2(dst, srcf)
except shutil.Error as e: except shutil.Error as exc:
src = e.args[0][0][0] src = exc.args[0][0][0]
why = e.args[0][0][2] why = exc.args[0][0][2]
self.log.err('importing \"{}\" failed: {}'.format(src, why)) self.log.err('importing \"{}\" failed: {}'.format(src, why))
return True return True
@@ -218,8 +228,8 @@ class Importer:
dfs = self.conf.get_dotfile_by_dst(dst) dfs = self.conf.get_dotfile_by_dst(dst)
if not dfs: if not dfs:
return False return False
for df in dfs: for dotfile in dfs:
profiles = self.conf.get_profiles_by_dotfile_key(df.key) profiles = self.conf.get_profiles_by_dotfile_key(dotfile.key)
profiles = [x.key for x in profiles] profiles = [x.key for x in profiles]
if self.profile in profiles and \ if self.profile in profiles and \
not self.conf.get_dotfile_by_src_dst(src, dst): not self.conf.get_dotfile_by_src_dst(src, dst):