1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 07:54:15 +00:00

add regex

This commit is contained in:
user
2025-05-19 22:00:43 +02:00
parent cc1ef5c001
commit fc96a94613
3 changed files with 28 additions and 14 deletions

View File

@@ -24,7 +24,7 @@ class Dotfile(DictParser):
link=LinkTypes.NOLINK, noempty=False, link=LinkTypes.NOLINK, noempty=False,
cmpignore=None, upignore=None, cmpignore=None, upignore=None,
instignore=None, template=True, chmod=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 constructor
@key: dotfile key @key: dotfile key
@@ -56,7 +56,7 @@ class Dotfile(DictParser):
self.template = template self.template = template
self.chmod = chmod self.chmod = chmod
self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop 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 \ if self.link != LinkTypes.NOLINK and \
( (
@@ -100,7 +100,7 @@ class Dotfile(DictParser):
value['noempty'] = value.get(cls.key_noempty, False) value['noempty'] = value.get(cls.key_noempty, False)
value['template'] = value.get(cls.key_template, True) value['template'] = value.get(cls.key_template, True)
value['dir_as_block'] = value.get( value['dir_as_block'] = value.get(
cls.key_dir_as_block, False) cls.key_dir_as_block, [])
# remove old entries # remove old entries
value.pop(cls.key_noempty, None) value.pop(cls.key_noempty, None)
return value return value

View File

@@ -18,12 +18,11 @@ class FTreeDir:
directory tree for comparison directory tree for comparison
""" """
def __init__(self, path, ignores=None, def __init__(self, path, ignores=None, debug=False, dir_as_block=None):
debug=False, dir_as_block=False):
self.path = path self.path = path
self.ignores = ignores self.ignores = ignores
self.debug = debug self.debug = debug
self.dir_as_block = dir_as_block self.dir_as_block = dir_as_block or []
self.entries = [] self.entries = []
self.log = Logger(debug=self.debug) self.log = Logger(debug=self.debug)
if os.path.exists(path) and os.path.isdir(path): if os.path.exists(path) and os.path.isdir(path):
@@ -35,13 +34,12 @@ class FTreeDir:
ignore empty directory ignore empty directory
test for ignore pattern test for ignore pattern
""" """
# if directory should be handled as a block import fnmatch
# just add the directory itself for pattern in self.dir_as_block:
if self.dir_as_block: if fnmatch.fnmatch(self.path, pattern):
self.log.dbg( self.log.dbg(f'dir_as_block match: {pattern} for {self.path}')
f'handle as block: {self.path}') self.entries.append(self.path)
self.entries.append(self.path) return
return
for root, dirs, files in os.walk(self.path, followlinks=True): for root, dirs, files in os.walk(self.path, followlinks=True):
for file in files: for file in files:

View File

@@ -79,7 +79,7 @@ class Installer:
def install(self, templater, src, dst, linktype, def install(self, templater, src, dst, linktype,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=None, is_template=True, ignore=None, is_template=True,
chmod=None, dir_as_block=False): chmod=None, dir_as_block=None):
""" """
install src to dst install src to dst
@@ -99,6 +99,7 @@ class Installer:
- False, error_msg : error - False, error_msg : error
- False, None : ignored - False, None : ignored
""" """
dir_as_block = dir_as_block or []
if not src or not dst: if not src or not dst:
# fake dotfile # fake dotfile
self.log.dbg('fake dotfile installed') 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'\"{src}\" is a directory: {isdir}')
self.log.dbg(f'dir_as_block: {dir_as_block}') 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: if linktype == LinkTypes.NOLINK:
# normal file # normal file
if isdir: if isdir: