aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryn M. Reeves <bmr@redhat.com>2017-03-27 13:19:37 +0100
committerBryn M. Reeves <bmr@redhat.com>2017-03-27 13:23:20 +0100
commit3ae6267399e77bed7d15ac32564dde21ea877ad1 (patch)
tree831558cd17c3b1f08beb7400718adee84eaa4e83
parent4f7ea5d46f7531e8fdb8a58738a747f79a9c4841 (diff)
downloadsos-3ae6267399e77bed7d15ac32564dde21ea877ad1.tar.gz
[archive] accept an open File for add_file()
Allow subclasses of FileCacheArchive to accept either a path to a file to add, or an instance of an open File object, from whih data will be read. This will be used to covert logging FileHandler object to StreamHandler objects, avoiding creating bogus '<fdopen>' paths in the file system when the former are used with files that are created with os.fdopen(). Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r--sos/archive.py48
1 files changed, 32 insertions, 16 deletions
diff --git a/sos/archive.py b/sos/archive.py
index 1084fbbe..c2593c99 100644
--- a/sos/archive.py
+++ b/sos/archive.py
@@ -161,22 +161,38 @@ class FileCacheArchive(Archive):
dest = src
dest = self.dest_path(dest)
self._check_path(dest)
- try:
- shutil.copy(src, dest)
- except IOError as e:
- self.log_info("caught '%s' copying '%s'" % (e, src))
- try:
- shutil.copystat(src, dest)
- except OSError:
- # SELinux xattrs in /proc and /sys throw this
- pass
- try:
- stat = os.stat(src)
- os.chown(dest, stat.st_uid, stat.st_gid)
- except Exception as e:
- self.log_debug("caught '%s' setting ownership of '%s'" % (e, dest))
- self.log_debug("added '%s' to FileCacheArchive '%s'" %
- (src, self._archive_root))
+
+ # Handle adding a file from either a string respresenting
+ # a path, or a File object open for reading.
+ if not getattr(src, "read", None):
+ # path case
+ try:
+ shutil.copy(src, dest)
+ except IOError as e:
+ self.log_info("caught '%s' copying '%s'" % (e, src))
+ try:
+ shutil.copystat(src, dest)
+ except OSError:
+ # SELinux xattrs in /proc and /sys throw this
+ pass
+ try:
+ stat = os.stat(src)
+ os.chown(dest, stat.st_uid, stat.st_gid)
+ except Exception as e:
+ self.log_debug("caught '%s' setting ownership of '%s'"
+ % (e, dest))
+ file_name = "'%s'" % src
+ else:
+ # Open file case: first rewind the file to obtain
+ # everything written to it.
+ src.seek(0)
+ with open(dest, "w") as f:
+ for line in src:
+ f.write(line)
+ file_name = "open file"
+
+ self.log_debug("added %s to FileCacheArchive '%s'" %
+ (file_name, self._archive_root))
def add_string(self, content, dest):
src = dest