diff --git a/dotdrop/dotfile.py b/dotdrop/dotfile.py index 0c6813f..f033aad 100644 --- a/dotdrop/dotfile.py +++ b/dotdrop/dotfile.py @@ -24,7 +24,7 @@ class Dotfile(DictParser): link=LinkTypes.NOLINK, noempty=False, cmpignore=None, upignore=None, instignore=None, template=True, chmod=None, - ignore_missing_in_dotdrop=False, dir_as_block=False): + ignore_missing_in_dotdrop=False, dir_as_block=None): """ constructor @key: dotfile key @@ -56,7 +56,7 @@ class Dotfile(DictParser): self.template = template self.chmod = chmod self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop - self.dir_as_block = dir_as_block + self.dir_as_block = dir_as_block or [] if self.link != LinkTypes.NOLINK and \ ( @@ -100,7 +100,7 @@ class Dotfile(DictParser): value['noempty'] = value.get(cls.key_noempty, False) value['template'] = value.get(cls.key_template, True) value['dir_as_block'] = value.get( - cls.key_dir_as_block, False) + cls.key_dir_as_block, []) # remove old entries value.pop(cls.key_noempty, None) return value diff --git a/dotdrop/ftree.py b/dotdrop/ftree.py index e2a82f1..b3b88a9 100644 --- a/dotdrop/ftree.py +++ b/dotdrop/ftree.py @@ -18,12 +18,11 @@ class FTreeDir: directory tree for comparison """ - def __init__(self, path, ignores=None, - debug=False, dir_as_block=False): + def __init__(self, path, ignores=None, debug=False, dir_as_block=None): self.path = path self.ignores = ignores self.debug = debug - self.dir_as_block = dir_as_block + self.dir_as_block = dir_as_block or [] self.entries = [] self.log = Logger(debug=self.debug) if os.path.exists(path) and os.path.isdir(path): @@ -35,13 +34,12 @@ class FTreeDir: ignore empty directory test for ignore pattern """ - # if directory should be handled as a block - # just add the directory itself - if self.dir_as_block: - self.log.dbg( - f'handle as block: {self.path}') - self.entries.append(self.path) - return + import fnmatch + for pattern in self.dir_as_block: + if fnmatch.fnmatch(self.path, pattern): + self.log.dbg(f'dir_as_block match: {pattern} for {self.path}') + self.entries.append(self.path) + return for root, dirs, files in os.walk(self.path, followlinks=True): for file in files: diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 5fe2197..9519ed3 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -79,7 +79,7 @@ class Installer: def install(self, templater, src, dst, linktype, actionexec=None, noempty=False, ignore=None, is_template=True, - chmod=None, dir_as_block=False): + chmod=None, dir_as_block=None): """ install src to dst @@ -99,6 +99,7 @@ class Installer: - False, error_msg : error - False, None : ignored """ + dir_as_block = dir_as_block or [] if not src or not dst: # fake dotfile self.log.dbg('fake dotfile installed') @@ -134,6 +135,21 @@ class Installer: self.log.dbg(f'\"{src}\" is a directory: {isdir}') self.log.dbg(f'dir_as_block: {dir_as_block}') + import fnmatch + treat_as_block = any(fnmatch.fnmatch(src, pattern) for pattern in dir_as_block) + self.log.dbg(f'dir_as_block patterns: {dir_as_block}, treat_as_block: {treat_as_block}') + if treat_as_block: + self.log.dbg(f'handling directory {src} as a block for installation') + ret, err, ins = self._copy_dir(templater, src, dst, + actionexec=actionexec, + noempty=noempty, ignore=ignore, + is_template=is_template, + chmod=chmod, + dir_as_block=True) + if self.remove_existing_in_dir and ins: + self._remove_existing_in_dir(dst, ins) + return self._log_install(ret, err) + if linktype == LinkTypes.NOLINK: # normal file if isdir: