Derp .. take 2 ..

-------- Forwarded Message --------
Subject:        [PATCH] Add new 'clean_files' function to perform file
cleanups for */rm stages
Date:   Mon, 16 Jul 2018 02:13:09 +0100
From:   M. J. Everitt <[email protected]>
To:     [email protected]



It would appear that the */rm stages never actually cleaned up files -
there is provision for directory and symlink removal, but no code for
files. Added to this, any wildcards specified are ignored by the
os.file.exists() function. A new function seemed to be the sensible
solution to this issue, utilising the python 'glob.glob' function.

Bug: https://bugs.gentoo.org/661084

Signed-off-by: M. J. Everitt <[email protected]>
---
 catalyst/fileops.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/catalyst/fileops.py b/catalyst/fileops.py
index ab6a0c9f..5d1be762 100644
--- a/catalyst/fileops.py
+++ b/catalyst/fileops.py
@@ -11,6 +11,7 @@ functions for use throughout catalyst.
 '''
 
 import os
+import glob
 import shutil
 from stat import ST_UID, ST_GID, ST_MODE
 
@@ -70,6 +71,7 @@ def clear_dir(target, mode=0o755, chg_flags=False, remove=False,
 		return False
 
 	mystat = None
+	myglob = glob.glob(target)
 	if os.path.isdir(target) and not os.path.islink(target):
 		log.notice('Emptying directory: %s', target)
 		# stat the dir, delete the dir, recreate the dir and set
@@ -92,11 +94,14 @@ def clear_dir(target, mode=0o755, chg_flags=False, remove=False,
 		else:
 			log.info('clear_dir failed: %s: is not a directory', target)
 			return False
+	elif myglob:
+		return clear_files(myglob)
 	else:
 		log.debug("Condidtions not met to clear: %s", target)
 		log.debug("                       isdir: %s", os.path.isdir(target))
 		log.debug("                      islink: %s", os.path.islink(target))
 		log.debug("                      exists: %s", os.path.exists(target))
+		log.debug("                        glob: %d", len(myglob))
 
 	if not remove:
 		log.debug('ensure_dirs()')
@@ -109,6 +114,25 @@ def clear_dir(target, mode=0o755, chg_flags=False, remove=False,
 	return True
 
 
+def clear_files(targets):
+	'''Universal file clearing function
+
+	@targets: list of files to remove
+	@return boolean
+	'''
+	mySuccess = True
+	for myfile in targets:
+		log.debug("os.remove() %s", myfile)
+		try:
+			os.remove(myfile)
+		except OSError:
+			log.error('clear_files failed', exc_info=True)
+			mySuccess = False
+
+	log.debug('DONE, returning, success = %s', mySuccess)
+	return mySuccess
+
+
 def clear_path(target):
 	"""Nuke |target| regardless of it being a dir or file."""
 	clear_dir(target, remove=True)


Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to