From 7326baa4f0028d92d43350598641908e80fe145b Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 29 Jan 2024 22:42:47 +0100 Subject: [PATCH] dir empty --- dotdrop/dotdrop.py | 4 ++-- dotdrop/ftree.py | 12 +++++++++--- dotdrop/uninstaller.py | 4 ++-- dotdrop/utils.py | 9 +++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 2ba4bd1..ac5d4fc 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -22,7 +22,7 @@ from dotdrop.comparator import Comparator from dotdrop.importer import Importer from dotdrop.utils import get_tmpdir, removepath, \ uniq_list, ignores_to_absolute, dependencies_met, \ - adapt_workers, check_version, pivot_path + adapt_workers, check_version, pivot_path, dir_empty from dotdrop.linktypes import LinkTypes from dotdrop.exceptions import YamlException, \ UndefinedException, UnmetDependency, \ @@ -726,7 +726,7 @@ def cmd_remove(opts): parent = os.path.dirname(dtpath) # remove any empty parent up to dotpath while parent != opts.dotpath: - if os.path.isdir(parent) and not os.listdir(parent): + if os.path.isdir(parent) and dir_empty(parent): msg = f'Remove empty dir \"{parent}\"' if opts.safe and not LOG.ask(msg): break diff --git a/dotdrop/ftree.py b/dotdrop/ftree.py index 8b29c33..3878a42 100644 --- a/dotdrop/ftree.py +++ b/dotdrop/ftree.py @@ -9,7 +9,7 @@ filesystem tree for directories import os # local imports -from dotdrop.utils import must_ignore +from dotdrop.utils import must_ignore, dir_empty from dotdrop.logger import Logger @@ -38,14 +38,15 @@ class FTreeDir: fpath = os.path.join(root, file) if must_ignore([fpath], ignores=self.ignores, debug=self.debug, strict=True): + self.log.dbg('ignoring file {fpath}') continue self.log.dbg(f'added file to list of {self.path}: {fpath}') self.entries.append(fpath) for dname in dirs: dpath = os.path.join(root, dname) - subs = os.listdir(dpath) - if len(subs) < 1: + if dir_empty(dpath): # ignore empty directory + self.log.dbg('ignoring empty dir {dpath}') continue # appending "/" allows to ensure pattern # like "*/dir/*" will match the content of the directory @@ -53,10 +54,15 @@ class FTreeDir: dpath += os.path.sep if must_ignore([dpath], ignores=self.ignores, debug=self.debug, strict=True): + self.log.dbg('ignoring dir {dpath}') continue self.log.dbg(f'added dir to list of {self.path}: {dpath}') self.entries.append(dpath) + def get_entries(self): + """return all entries""" + return self.entries + def compare(self, other): """ compare two trees and returns diff --git a/dotdrop/uninstaller.py b/dotdrop/uninstaller.py index e6c3c5d..de5e2dd 100644 --- a/dotdrop/uninstaller.py +++ b/dotdrop/uninstaller.py @@ -7,7 +7,7 @@ handle the un-installation of dotfiles import os from dotdrop.logger import Logger -from dotdrop.utils import removepath +from dotdrop.utils import removepath, dir_empty class Uninstaller: @@ -86,7 +86,7 @@ class Uninstaller: if not subret: ret = False - if not os.listdir(dirpath): + if dir_empty(dirpath): # empty self.log.dbg(f'remove empty dir {dirpath}') if self.dry: diff --git a/dotdrop/utils.py b/dotdrop/utils.py index bd82824..3955ba7 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -692,6 +692,15 @@ def pivot_path(path, newdir, striphome=False, logger=None): return new +def dir_empty(path): + """return true if directory is empty""" + if not os.path.exists(path): + return True + if not os.path.isdir(path): + return True + return len(os.listdir(path)) < 1 + + def is_bin_in_path(command): """ check binary from command is in path