def add_copy_spec_limit(self, fname, sizelimit=None, sub=None): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if not (fname and len(fname)): return False files = glob.glob(fname) files.sort() if len(files) == 0: return cursize = 0 limit_reached = False sizelimit *= 1024 * 1024 # in MB flog = None for flog in files: cursize += os.stat(flog)[ST_SIZE] if sizelimit and cursize > sizelimit: limit_reached = True break self.add_copy_spec(flog, sub) if flog == files[0] and limit_reached: flog_name = flog if sub: old, new = sub flog_name = flog.replace(old, new) strfile = flog_name.replace(os.path.sep, ".") + ".tailed" self.add_string_as_file(tail(flog, sizelimit), strfile) self.archive.add_link(os.path.join( os.path.relpath('/', os.path.dirname(flog)), 'sos_strings', self.name(), strfile), flog)
def add_copy_spec_limit(self, copyspec, sizelimit=None, tailit=True): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if not (copyspec and len(copyspec)): return False files = glob.glob(copyspec) files.sort() if len(files) == 0: return current_size = 0 limit_reached = False sizelimit *= 1024 * 1024 # in MB _file = None for _file in files: current_size += os.stat(_file)[ST_SIZE] if sizelimit and current_size > sizelimit: limit_reached = True break self.add_copy_spec(_file) if limit_reached and tailit: file_name = _file if file_name[0] == os.sep: file_name = file_name.lstrip(os.sep) strfile = file_name.replace(os.path.sep, ".") + ".tailed" self.add_string_as_file(tail(_file, sizelimit), strfile) rel_path = os.path.relpath('/', os.path.dirname(_file)) link_path = os.path.join(rel_path, 'sos_strings', self.name(), strfile) self.archive.add_link(link_path, _file)
def addCopySpecLimit(self, fname, sizelimit=None, sub=None): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if not (fname and len(fname)): return False files = glob.glob(fname) files.sort() cursize = 0 limit_reached = False sizelimit *= 1024 * 1024 # in MB flog = None for flog in files: cursize += os.stat(flog)[ST_SIZE] if sizelimit and cursize > sizelimit: limit_reached = True break self.addCopySpec(flog, sub) if flog == files[0] and limit_reached: flog_name = flog if sub: old, new = sub flog_name = flog.replace(old, new) self.addStringAsFile( tail(flog, sizelimit), flog_name.replace(os.path.sep, ".") + ".tailed")
def addCopySpecLimit(self, fname, sizelimit=None, sub=None): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if not (fname and len(fname)): return False files = glob.glob(fname) files.sort() cursize = 0 limit_reached = False sizelimit *= 1024 * 1024 # in MB flog = None for flog in files: cursize += os.stat(flog)[ST_SIZE] if sizelimit and cursize > sizelimit: limit_reached = True break self.addCopySpec(flog, sub) if flog == files[0] and limit_reached: flog_name = flog if sub: old, new = sub flog_name = flog.replace(old, new) self.addStringAsFile(tail(flog, sizelimit), flog_name.replace(os.path.sep, ".") + ".tailed")
def add_copy_spec_limit(self, fname, sizelimit=None, sub=None): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if not (fname and len(fname)): return False files = glob.glob(fname) files.sort() if len(files) == 0: return cursize = 0 limit_reached = False sizelimit *= 1024 * 1024 # in MB flog = None for flog in files: cursize += os.stat(flog)[ST_SIZE] if sizelimit and cursize > sizelimit: limit_reached = True break self.add_copy_spec(flog, sub) if flog == files[0] and limit_reached: flog_name = flog if sub: old, new = sub flog_name = flog.replace(old, new) strfile = flog_name.replace(os.path.sep, ".") + ".tailed" self.add_string_as_file(tail(flog, sizelimit), strfile) self.archive.add_link( os.path.join(os.path.relpath('/', os.path.dirname(flog)), 'sos_strings', self.name(), strfile), flog)
def test_add_string_from_file(self): self.copy_strings = [] testfile = tempfile.NamedTemporaryFile(dir=self.tmpdir, delete=False) testfile.write(six.b("*" * 1000)) testfile.flush() testfile.close() self.copy_strings.append((tail(testfile.name, 100), 'string_test.txt')) self.tf.add_string(self.copy_strings[0][0], 'tests/string_test.txt') self.tf.finalize('auto')
def add_copy_spec(self, copyspecs, sizelimit=None, tailit=True): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if sizelimit: sizelimit *= 1024 * 1024 # in MB if not copyspecs: return False if isinstance(copyspecs, six.string_types): copyspecs = [copyspecs] for copyspec in copyspecs: if not (copyspec and len(copyspec)): return False if self.use_sysroot(): copyspec = self.join_sysroot(copyspec) files = self._expand_copy_spec(copyspec) files.sort() if len(files) == 0: continue current_size = 0 limit_reached = False _file = None for _file in files: try: current_size += os.stat(_file)[stat.ST_SIZE] except (OSError, FileNotFoundError): self._log_info("failed to stat '%s'" % _file) if sizelimit and current_size > sizelimit: limit_reached = True break self._add_copy_paths([_file]) if limit_reached and tailit: file_name = _file if file_name[0] == os.sep: file_name = file_name.lstrip(os.sep) strfile = file_name.replace(os.path.sep, ".") + ".tailed" self.add_string_as_file(tail(_file, sizelimit), strfile) rel_path = os.path.relpath('/', os.path.dirname(_file)) link_path = os.path.join(rel_path, 'sos_strings', self.name(), strfile) self.archive.add_link(link_path, _file)
def test_tail_too_many(self): t = tail("tests/tail_test.txt", 200) expected = open("tests/tail_test.txt", "r").read() self.assertEquals(t, six.b(expected))
def test_tail(self): t = tail("tests/tail_test.txt", 10) self.assertEquals(t, six.b("last line\n"))
def test_tail_too_many(self): t = tail("tests/tail_test.txt", 200) expected = open("tests/tail_test.txt", "r").read() self.assertEquals(t, expected)
def test_tail(self): t = tail("tests/tail_test.txt", 10) self.assertEquals(t, "last line\n")
def add_copy_spec(self, copyspecs, sizelimit=None, tailit=True): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ sizelimit = sizelimit or self.get_option("log_size") if self.get_option('all_logs'): sizelimit = None if sizelimit: sizelimit *= 1024 * 1024 # in MB if not copyspecs: return False if isinstance(copyspecs, six.string_types): copyspecs = [copyspecs] for copyspec in copyspecs: if not (copyspec and len(copyspec)): return False if self.use_sysroot(): copyspec = self.join_sysroot(copyspec) files = self._expand_copy_spec(copyspec) if len(files) == 0: continue # Files hould be sorted in most-recently-modified order, so that # we collect the newest data first before reaching the limit. def getmtime(path): try: return os.path.getmtime(path) except OSError: return 0 files.sort(key=getmtime, reverse=True) current_size = 0 limit_reached = False _file = None for _file in files: try: current_size += os.stat(_file)[stat.ST_SIZE] except OSError: self._log_info("failed to stat '%s'" % _file) if sizelimit and current_size > sizelimit: limit_reached = True break self._add_copy_paths([_file]) if limit_reached and tailit and not _file_is_compressed(_file): file_name = _file if file_name[0] == os.sep: file_name = file_name.lstrip(os.sep) strfile = file_name.replace(os.path.sep, ".") + ".tailed" self.add_string_as_file(tail(_file, sizelimit), strfile) rel_path = os.path.relpath('/', os.path.dirname(_file)) link_path = os.path.join(rel_path, 'sos_strings', self.name(), strfile) self.archive.add_link(link_path, _file)
def add_copy_spec(self, copyspecs, sizelimit=None, tailit=True): """Add a file or glob but limit it to sizelimit megabytes. If fname is a single file the file will be tailed to meet sizelimit. If the first file in a glob is too large it will be tailed to meet the sizelimit. """ if self.get_option('all_logs'): sizelimit = None if sizelimit: sizelimit *= 1024 * 1024 # in MB if not copyspecs: return False if isinstance(copyspecs, six.string_types): copyspecs = [copyspecs] for copyspec in copyspecs: if not (copyspec and len(copyspec)): return False if self.use_sysroot(): copyspec = self.join_sysroot(copyspec) files = self._expand_copy_spec(copyspec) if len(files) == 0: continue # Files hould be sorted in most-recently-modified order, so that # we collect the newest data first before reaching the limit. def getmtime(path): try: return os.path.getmtime(path) except OSError: return 0 files.sort(key=getmtime, reverse=True) current_size = 0 limit_reached = False _file = None for _file in files: try: current_size += os.stat(_file)[stat.ST_SIZE] except OSError: self._log_info("failed to stat '%s'" % _file) if sizelimit and current_size > sizelimit: limit_reached = True break self._add_copy_paths([_file]) if limit_reached and tailit and not _file_is_compressed(_file): file_name = _file if file_name[0] == os.sep: file_name = file_name.lstrip(os.sep) strfile = file_name.replace(os.path.sep, ".") + ".tailed" self.add_string_as_file(tail(_file, sizelimit), strfile) rel_path = os.path.relpath('/', os.path.dirname(_file)) link_path = os.path.join(rel_path, 'sos_strings', self.name(), strfile) self.archive.add_link(link_path, _file)