mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-09 22:49:15 +00:00
fix notemplate installation
This commit is contained in:
@@ -274,7 +274,7 @@ class Installer:
|
|||||||
self.log.dry('would remove {} and link to {}'.format(dst, src))
|
self.log.dry('would remove {} and link to {}'.format(dst, src))
|
||||||
return True, None
|
return True, None
|
||||||
if self.showdiff:
|
if self.showdiff:
|
||||||
self._diff_before_write(src, dst)
|
self._diff_before_write(src, dst, quiet=False)
|
||||||
msg = 'Remove "{}" for link creation?'.format(dst)
|
msg = 'Remove "{}" for link creation?'.format(dst)
|
||||||
if self.safe and not self.log.ask(msg):
|
if self.safe and not self.log.ask(msg):
|
||||||
err = 'ignoring "{}", link was not created'.format(dst)
|
err = 'ignoring "{}", link was not created'.format(dst)
|
||||||
@@ -337,7 +337,12 @@ class Installer:
|
|||||||
err = 'dotfile points to itself: {}'.format(dst)
|
err = 'dotfile points to itself: {}'.format(dst)
|
||||||
return False, err
|
return False, err
|
||||||
|
|
||||||
|
if not os.path.exists(src):
|
||||||
|
err = 'source dotfile does not exist: {}'.format(src)
|
||||||
|
return False, err
|
||||||
|
|
||||||
# handle the file
|
# handle the file
|
||||||
|
content = None
|
||||||
if template:
|
if template:
|
||||||
# template the file
|
# template the file
|
||||||
saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
|
saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
|
||||||
@@ -354,30 +359,20 @@ class Installer:
|
|||||||
if content is None:
|
if content is None:
|
||||||
err = 'empty template {}'.format(src)
|
err = 'empty template {}'.format(src)
|
||||||
return False, err
|
return False, err
|
||||||
if not os.path.exists(src):
|
ret, err = self._write(src, dst,
|
||||||
err = 'source dotfile does not exist: {}'.format(src)
|
content=content,
|
||||||
return False, err
|
actionexec=actionexec,
|
||||||
st = os.stat(src)
|
template=template)
|
||||||
ret, err = self._write(src, dst, content,
|
|
||||||
st.st_mode, actionexec=actionexec)
|
|
||||||
# build return values
|
|
||||||
if ret < 0:
|
|
||||||
# error
|
|
||||||
return False, err
|
|
||||||
if ret > 0:
|
|
||||||
# already exists
|
|
||||||
if self.debug:
|
|
||||||
self.log.dbg('ignoring {}'.format(dst))
|
|
||||||
return False, None
|
|
||||||
else:
|
|
||||||
# copy the file
|
|
||||||
try:
|
|
||||||
shutil.copyfile(src, dst)
|
|
||||||
shutil.copymode(src, dst)
|
|
||||||
except Exception as e:
|
|
||||||
return False, str(e)
|
|
||||||
ret = 0
|
|
||||||
|
|
||||||
|
# build return values
|
||||||
|
if ret < 0:
|
||||||
|
# error
|
||||||
|
return False, err
|
||||||
|
if ret > 0:
|
||||||
|
# already exists
|
||||||
|
if self.debug:
|
||||||
|
self.log.dbg('ignoring {}'.format(dst))
|
||||||
|
return False, None
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
# success
|
# success
|
||||||
if not self.dry and not self.comparing:
|
if not self.dry and not self.comparing:
|
||||||
@@ -435,22 +430,31 @@ class Installer:
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _fake_diff(self, dst, content):
|
def _fake_diff(self, dst, content):
|
||||||
"""fake diff by comparing file content with content"""
|
"""
|
||||||
|
fake diff by comparing file content with content
|
||||||
|
returns True if same
|
||||||
|
"""
|
||||||
cur = ''
|
cur = ''
|
||||||
with open(dst, 'br') as f:
|
with open(dst, 'br') as f:
|
||||||
cur = f.read()
|
cur = f.read()
|
||||||
return cur == content
|
return cur == content
|
||||||
|
|
||||||
def _write(self, src, dst, content, rights, actionexec=None):
|
def _write(self, src, dst, content=None,
|
||||||
"""write content to file
|
actionexec=None, template=True):
|
||||||
|
"""
|
||||||
|
copy dotfile / write content to file
|
||||||
return 0, None: for success,
|
return 0, None: for success,
|
||||||
1, None: when already exists
|
1, None: when already exists
|
||||||
-1, err: when error"""
|
-1, err: when error
|
||||||
|
content is always empty if template is False
|
||||||
|
and is to be ignored
|
||||||
|
"""
|
||||||
overwrite = not self.safe
|
overwrite = not self.safe
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry('would install {}'.format(dst))
|
self.log.dry('would install {}'.format(dst))
|
||||||
return 0, None
|
return 0, None
|
||||||
if os.path.lexists(dst):
|
if os.path.lexists(dst):
|
||||||
|
rights = os.stat(src).st_mode
|
||||||
samerights = False
|
samerights = False
|
||||||
try:
|
try:
|
||||||
samerights = os.stat(dst).st_mode == rights
|
samerights = os.stat(dst).st_mode == rights
|
||||||
@@ -459,15 +463,24 @@ class Installer:
|
|||||||
# broken symlink
|
# broken symlink
|
||||||
err = 'broken symlink {}'.format(dst)
|
err = 'broken symlink {}'.format(dst)
|
||||||
return -1, err
|
return -1, err
|
||||||
if self.diff and self._fake_diff(dst, content) and samerights:
|
diff = None
|
||||||
if self.debug:
|
if self.diff:
|
||||||
self.log.dbg('{} is the same'.format(dst))
|
diff = self._diff_before_write(src, dst,
|
||||||
return 1, None
|
content=content,
|
||||||
|
quiet=True)
|
||||||
|
if not diff and samerights:
|
||||||
|
if self.debug:
|
||||||
|
self.log.dbg('{} is the same'.format(dst))
|
||||||
|
return 1, None
|
||||||
if self.safe:
|
if self.safe:
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('change detected for {}'.format(dst))
|
self.log.dbg('change detected for {}'.format(dst))
|
||||||
if self.showdiff:
|
if self.showdiff:
|
||||||
self._diff_before_write(src, dst, content=content)
|
if diff is None:
|
||||||
|
diff = self._diff_before_write(src, dst,
|
||||||
|
content=content,
|
||||||
|
quiet=True)
|
||||||
|
self._print_diff(src, dst, diff)
|
||||||
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
||||||
self.log.warn('ignoring {}'.format(dst))
|
self.log.warn('ignoring {}'.format(dst))
|
||||||
return 1, None
|
return 1, None
|
||||||
@@ -482,24 +495,35 @@ class Installer:
|
|||||||
if not r:
|
if not r:
|
||||||
return -1, e
|
return -1, e
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('writes content to \"{}\"'.format(dst))
|
self.log.dbg('install dotfile to \"{}\"'.format(dst))
|
||||||
# re-check in case action created the file
|
# re-check in case action created the file
|
||||||
if self.safe and not overwrite and os.path.lexists(dst):
|
if self.safe and not overwrite and os.path.lexists(dst):
|
||||||
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
||||||
self.log.warn('ignoring {}'.format(dst))
|
self.log.warn('ignoring {}'.format(dst))
|
||||||
return 1, None
|
return 1, None
|
||||||
# write the file
|
|
||||||
try:
|
if template:
|
||||||
with open(dst, 'wb') as f:
|
# write content the file
|
||||||
f.write(content)
|
try:
|
||||||
except NotADirectoryError as e:
|
with open(dst, 'wb') as f:
|
||||||
err = 'opening dest file: {}'.format(e)
|
f.write(content)
|
||||||
return -1, err
|
shutil.copymode(src, dst)
|
||||||
os.chmod(dst, rights)
|
except NotADirectoryError as e:
|
||||||
|
err = 'opening dest file: {}'.format(e)
|
||||||
|
return -1, err
|
||||||
|
except Exception as e:
|
||||||
|
return -1, str(e)
|
||||||
|
else:
|
||||||
|
# copy file
|
||||||
|
try:
|
||||||
|
shutil.copyfile(src, dst)
|
||||||
|
shutil.copymode(src, dst)
|
||||||
|
except Exception as e:
|
||||||
|
return -1, str(e)
|
||||||
return 0, None
|
return 0, None
|
||||||
|
|
||||||
def _diff_before_write(self, src, dst, content=None):
|
def _diff_before_write(self, src, dst, content=None, quiet=False):
|
||||||
"""diff before writing when using --showdiff - not efficient"""
|
"""diff before writing"""
|
||||||
tmp = None
|
tmp = None
|
||||||
if content:
|
if content:
|
||||||
tmp = utils.write_to_tmpfile(content)
|
tmp = utils.write_to_tmpfile(content)
|
||||||
@@ -509,9 +533,11 @@ class Installer:
|
|||||||
if tmp:
|
if tmp:
|
||||||
utils.remove(tmp, quiet=True)
|
utils.remove(tmp, quiet=True)
|
||||||
|
|
||||||
# fake the output for readability
|
if not quiet:
|
||||||
if not diff:
|
self._print_diff(src, dst, diff)
|
||||||
return
|
return diff
|
||||||
|
|
||||||
|
def _print_diff(self, src, dst, diff):
|
||||||
self.log.log('diff \"{}\" VS \"{}\"'.format(dst, src))
|
self.log.log('diff \"{}\" VS \"{}\"'.format(dst, src))
|
||||||
self.log.emph(diff)
|
self.log.emph(diff)
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ def shell(cmd, debug=False):
|
|||||||
|
|
||||||
def diff(original, modified, raw=True,
|
def diff(original, modified, raw=True,
|
||||||
diff_cmd='', debug=False):
|
diff_cmd='', debug=False):
|
||||||
"""compare two files"""
|
"""compare two files, returns '' if same"""
|
||||||
if not diff_cmd:
|
if not diff_cmd:
|
||||||
diff_cmd = 'diff -r -u {0} {1}'
|
diff_cmd = 'diff -r -u {0} {1}'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user