1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 15:39:43 +00:00

dir empty

This commit is contained in:
deadc0de6
2024-01-29 22:42:47 +01:00
committed by deadc0de
parent c3138fb2d7
commit 7326baa4f0
4 changed files with 22 additions and 7 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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