mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-07 17:59:17 +00:00
add ability to compare with patterns in dotfile (#57)
This commit is contained in:
@@ -8,6 +8,7 @@ handle the comparison of dotfiles and local deployment
|
||||
import os
|
||||
import shutil
|
||||
import filecmp
|
||||
import fnmatch
|
||||
|
||||
# local imports
|
||||
from dotdrop.logger import Logger
|
||||
@@ -16,45 +17,47 @@ import dotdrop.utils as utils
|
||||
|
||||
class Comparator:
|
||||
|
||||
def __init__(self, diffopts='', ignore=[], debug=False):
|
||||
def __init__(self, diffopts='', debug=False):
|
||||
self.diffopts = diffopts
|
||||
self.ignore = [os.path.expanduser(i) for i in ignore]
|
||||
self.debug = debug
|
||||
self.log = Logger()
|
||||
|
||||
def compare(self, left, right):
|
||||
def compare(self, left, right, ignore=[]):
|
||||
"""diff left (dotdrop dotfile) and right (deployed file)"""
|
||||
left = os.path.expanduser(left)
|
||||
right = os.path.expanduser(right)
|
||||
if self.debug:
|
||||
self.log.dbg('comparing {} and {}'.format(left, right))
|
||||
self.log.dbg('ignore pattern(s): {}'.format(ignore))
|
||||
if not os.path.isdir(left):
|
||||
return self._comp_file(left, right)
|
||||
return self._comp_dir(left, right)
|
||||
return self._comp_file(left, right, ignore)
|
||||
return self._comp_dir(left, right, ignore)
|
||||
|
||||
def _comp_file(self, left, right):
|
||||
def _comp_file(self, left, right, ignore):
|
||||
"""compare a file"""
|
||||
if left in self.ignore or right in self.ignore:
|
||||
if self._ignore([left, right], ignore):
|
||||
if self.debug:
|
||||
self.log.dbg('ignoring diff {} and {}'.format(left, right))
|
||||
return ''
|
||||
return self._diff(left, right)
|
||||
|
||||
def _comp_dir(self, left, right):
|
||||
def _comp_dir(self, left, right, ignore):
|
||||
"""compare a directory"""
|
||||
if left in self.ignore or right in self.ignore:
|
||||
if self._ignore([left, right], ignore):
|
||||
if self.debug:
|
||||
self.log.dbg('ignoring diff {} and {}'.format(left, right))
|
||||
return ''
|
||||
if self.debug:
|
||||
self.log.dbg('compare {} and {}'.format(left, right))
|
||||
ret = []
|
||||
comp = filecmp.dircmp(left, right, ignore=self.ignore)
|
||||
comp = filecmp.dircmp(left, right)
|
||||
# handle files only in deployed file
|
||||
for i in comp.left_only:
|
||||
if os.path.join(left, i) in self.ignore:
|
||||
if self._ignore([os.path.join(left, i)], ignore):
|
||||
continue
|
||||
ret.append('only in left: \"{}\"\n'.format(i))
|
||||
for i in comp.right_only:
|
||||
if os.path.join(right, i) in self.ignore:
|
||||
if self._ignore([os.path.join(right, i)], ignore):
|
||||
continue
|
||||
ret.append('only in right: \"{}\"\n'.format(i))
|
||||
|
||||
@@ -88,3 +91,15 @@ class Comparator:
|
||||
rshort = os.path.basename(right)
|
||||
diff = 'diff \"{}\":\n{}'.format(lshort, diff)
|
||||
return diff
|
||||
|
||||
def _ignore(self, paths, ignore):
|
||||
'''return True if any paths is ignored - not very efficient'''
|
||||
if not ignore:
|
||||
return False
|
||||
for p in paths:
|
||||
for i in ignore:
|
||||
if fnmatch.fnmatch(p, i):
|
||||
if self.debug:
|
||||
self.log.dbg('ignore match {}'.format(p))
|
||||
return True
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user