aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2016-05-19 15:18:29 -0400
committerMike Frysinger <vapier@gentoo.org>2016-05-19 15:39:22 -0400
commit67af25c7824102b93db0d4016d6a9e101bbf6c09 (patch)
treedff3874f120309122e68f1ddc4348fe9b94d9b39
parentchroot-functions: genkernel: robustify version parsing (diff)
downloadcatalyst-67af25c7824102b93db0d4016d6a9e101bbf6c09.tar.gz
catalyst-67af25c7824102b93db0d4016d6a9e101bbf6c09.tar.bz2
catalyst-67af25c7824102b93db0d4016d6a9e101bbf6c09.zip
clear_dir: make a bit more robust/flexible
Make it so clear_dir will recover from non-directory paths, and it will always call ensure_dirs even if the path didn't exist previously. Most callers want this behavior and call ensure_dirs themselves.
-rw-r--r--catalyst/base/stagebase.py9
-rw-r--r--catalyst/fileops.py28
-rw-r--r--catalyst/targets/livecd_stage2.py12
3 files changed, 24 insertions, 25 deletions
diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index b6dd08d6..6aa854be 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -18,7 +18,7 @@ from catalyst.base.targetbase import TargetBase
from catalyst.base.clearbase import ClearBase
from catalyst.base.genbase import GenBase
from catalyst.lock import LockDir, LockInUse
-from catalyst.fileops import ensure_dirs, pjoin
+from catalyst.fileops import ensure_dirs, pjoin, clear_dir
from catalyst.base.resume import AutoResume
if sys.version_info[0] >= 3:
@@ -821,13 +821,11 @@ class StageBase(TargetBase, ClearBase, GenBase):
cleanup_msg="Cleaning up invalid snapshot cache at \n\t"+\
self.settings["snapshot_cache_path"]+\
" (this can take a long time)..."
- cleanup_errmsg="Error removing existing snapshot cache directory."
if self.settings["snapshot_path_hash"]==snapshot_cache_hash:
log.info('Valid snapshot cache, skipping unpack of portage tree...')
unpack=False
else:
- cleanup_errmsg="Error removing existing snapshot directory."
cleanup_msg=\
'Cleaning up existing portage tree (this can take a long time)...'
unpack_info['destination'] = normpath(
@@ -847,10 +845,7 @@ class StageBase(TargetBase, ClearBase, GenBase):
self.snapcache_lock.write_lock()
if os.path.exists(target_portdir):
log.info('%s', cleanup_msg)
- cleanup_cmd = "rm -rf " + target_portdir
- log.info('unpack() cleanup_cmd = %s', cleanup_cmd)
- cmd(cleanup_cmd,cleanup_errmsg,env=self.env)
- ensure_dirs(target_portdir, mode=0o755)
+ clear_dir(target_portdir)
log.notice('Unpacking portage tree (this can take a long time) ...')
if not self.decompressor.extract(unpack_info):
diff --git a/catalyst/fileops.py b/catalyst/fileops.py
index 4fdc0448..5fbca26c 100644
--- a/catalyst/fileops.py
+++ b/catalyst/fileops.py
@@ -54,7 +54,8 @@ def ensure_dirs(path, gid=-1, uid=-1, mode=0o755, minimal=True,
return succeeded
-def clear_dir(target, mode=0o755, chg_flags=False, remove=False):
+def clear_dir(target, mode=0o755, chg_flags=False, remove=False,
+ clear_nondir=True):
'''Universal directory clearing function
@target: string, path to be cleared or removed
@@ -67,27 +68,36 @@ def clear_dir(target, mode=0o755, chg_flags=False, remove=False):
if not target:
log.debug('no target... returning')
return False
+
+ mystat = None
if os.path.isdir(target):
log.info('Emptying directory: %s', target)
# stat the dir, delete the dir, recreate the dir and set
# the proper perms and ownership
try:
log.debug('os.stat()')
- mystat=os.stat(target)
+ mystat = os.stat(target)
# There's no easy way to change flags recursively in python
if chg_flags and os.uname()[0] == "FreeBSD":
os.system("chflags -R noschg " + target)
log.debug('shutil.rmtree()')
shutil.rmtree(target)
- if not remove:
- log.debug('ensure_dirs()')
- ensure_dirs(target, mode=mode)
- os.chown(target, mystat[ST_UID], mystat[ST_GID])
- os.chmod(target, mystat[ST_MODE])
except Exception:
log.error('clear_dir failed', exc_info=True)
return False
- else:
- log.info('clear_dir failed: %s: is not a directory', target)
+ elif os.path.exists(target):
+ if clear_nondir:
+ os.unlink(clear_nondir)
+ else:
+ log.info('clear_dir failed: %s: is not a directory', target)
+ return False
+
+ if not remove:
+ log.debug('ensure_dirs()')
+ ensure_dirs(target, mode=mode)
+ if mystat:
+ os.chown(target, mystat[ST_UID], mystat[ST_GID])
+ os.chmod(target, mystat[ST_MODE])
+
log.debug('DONE, returning True')
return True
diff --git a/catalyst/targets/livecd_stage2.py b/catalyst/targets/livecd_stage2.py
index ea916b8a..a3b5e5e6 100644
--- a/catalyst/targets/livecd_stage2.py
+++ b/catalyst/targets/livecd_stage2.py
@@ -3,10 +3,8 @@ LiveCD stage2 target, builds upon previous LiveCD stage1 tarball
"""
# NOTE: That^^ docstring has influence catalyst-spec(5) man page generation.
-import os
-
-from catalyst.support import (normpath, file_locate, CatalystError, cmd)
-from catalyst.fileops import ensure_dirs
+from catalyst.support import (normpath, file_locate, CatalystError)
+from catalyst.fileops import clear_dir
from catalyst.base.stagebase import StageBase
@@ -46,11 +44,7 @@ class livecd_stage2(StageBase):
the final components needed to produce the iso image.
'''
super(livecd_stage2, self).set_target_path()
- if os.path.isdir(self.settings["target_path"]):
- cmd("rm -rf " + self.settings["target_path"],
- "Could not remove existing directory: " +
- self.settings["target_path"], env=self.env)
- ensure_dirs(self.settings["target_path"])
+ clear_dir(self.settings['target_path'])
def run_local(self):
# what modules do we want to blacklist?