mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-04 15:39:43 +00:00
dir empty
This commit is contained in:
@@ -22,7 +22,7 @@ from dotdrop.comparator import Comparator
|
|||||||
from dotdrop.importer import Importer
|
from dotdrop.importer import Importer
|
||||||
from dotdrop.utils import get_tmpdir, removepath, \
|
from dotdrop.utils import get_tmpdir, removepath, \
|
||||||
uniq_list, ignores_to_absolute, dependencies_met, \
|
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.linktypes import LinkTypes
|
||||||
from dotdrop.exceptions import YamlException, \
|
from dotdrop.exceptions import YamlException, \
|
||||||
UndefinedException, UnmetDependency, \
|
UndefinedException, UnmetDependency, \
|
||||||
@@ -726,7 +726,7 @@ def cmd_remove(opts):
|
|||||||
parent = os.path.dirname(dtpath)
|
parent = os.path.dirname(dtpath)
|
||||||
# remove any empty parent up to dotpath
|
# remove any empty parent up to dotpath
|
||||||
while parent != opts.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}\"'
|
msg = f'Remove empty dir \"{parent}\"'
|
||||||
if opts.safe and not LOG.ask(msg):
|
if opts.safe and not LOG.ask(msg):
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ filesystem tree for directories
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from dotdrop.utils import must_ignore
|
from dotdrop.utils import must_ignore, dir_empty
|
||||||
from dotdrop.logger import Logger
|
from dotdrop.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
@@ -38,14 +38,15 @@ class FTreeDir:
|
|||||||
fpath = os.path.join(root, file)
|
fpath = os.path.join(root, file)
|
||||||
if must_ignore([fpath], ignores=self.ignores,
|
if must_ignore([fpath], ignores=self.ignores,
|
||||||
debug=self.debug, strict=True):
|
debug=self.debug, strict=True):
|
||||||
|
self.log.dbg('ignoring file {fpath}')
|
||||||
continue
|
continue
|
||||||
self.log.dbg(f'added file to list of {self.path}: {fpath}')
|
self.log.dbg(f'added file to list of {self.path}: {fpath}')
|
||||||
self.entries.append(fpath)
|
self.entries.append(fpath)
|
||||||
for dname in dirs:
|
for dname in dirs:
|
||||||
dpath = os.path.join(root, dname)
|
dpath = os.path.join(root, dname)
|
||||||
subs = os.listdir(dpath)
|
if dir_empty(dpath):
|
||||||
if len(subs) < 1:
|
|
||||||
# ignore empty directory
|
# ignore empty directory
|
||||||
|
self.log.dbg('ignoring empty dir {dpath}')
|
||||||
continue
|
continue
|
||||||
# appending "/" allows to ensure pattern
|
# appending "/" allows to ensure pattern
|
||||||
# like "*/dir/*" will match the content of the directory
|
# like "*/dir/*" will match the content of the directory
|
||||||
@@ -53,10 +54,15 @@ class FTreeDir:
|
|||||||
dpath += os.path.sep
|
dpath += os.path.sep
|
||||||
if must_ignore([dpath], ignores=self.ignores,
|
if must_ignore([dpath], ignores=self.ignores,
|
||||||
debug=self.debug, strict=True):
|
debug=self.debug, strict=True):
|
||||||
|
self.log.dbg('ignoring dir {dpath}')
|
||||||
continue
|
continue
|
||||||
self.log.dbg(f'added dir to list of {self.path}: {dpath}')
|
self.log.dbg(f'added dir to list of {self.path}: {dpath}')
|
||||||
self.entries.append(dpath)
|
self.entries.append(dpath)
|
||||||
|
|
||||||
|
def get_entries(self):
|
||||||
|
"""return all entries"""
|
||||||
|
return self.entries
|
||||||
|
|
||||||
def compare(self, other):
|
def compare(self, other):
|
||||||
"""
|
"""
|
||||||
compare two trees and returns
|
compare two trees and returns
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ handle the un-installation of dotfiles
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
from dotdrop.logger import Logger
|
from dotdrop.logger import Logger
|
||||||
from dotdrop.utils import removepath
|
from dotdrop.utils import removepath, dir_empty
|
||||||
|
|
||||||
|
|
||||||
class Uninstaller:
|
class Uninstaller:
|
||||||
@@ -86,7 +86,7 @@ class Uninstaller:
|
|||||||
if not subret:
|
if not subret:
|
||||||
ret = False
|
ret = False
|
||||||
|
|
||||||
if not os.listdir(dirpath):
|
if dir_empty(dirpath):
|
||||||
# empty
|
# empty
|
||||||
self.log.dbg(f'remove empty dir {dirpath}')
|
self.log.dbg(f'remove empty dir {dirpath}')
|
||||||
if self.dry:
|
if self.dry:
|
||||||
|
|||||||
@@ -692,6 +692,15 @@ def pivot_path(path, newdir, striphome=False, logger=None):
|
|||||||
return new
|
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):
|
def is_bin_in_path(command):
|
||||||
"""
|
"""
|
||||||
check binary from command is in path
|
check binary from command is in path
|
||||||
|
|||||||
Reference in New Issue
Block a user