def _extract_archive(arch, extract_filter=None): """Extract the members filtered by 'extract_filter' from archive 'arch' and return the path to the extracted files. """ _LOGGER.info('Extract archive {}'.format(arch)) if not os.path.exists(arch): raise exc.LocalFileNotFoundError('{} cannot be found.'.format(arch)) try: # extract the req. paths from the archive to a temp directory temp_dir = _temp_dir() with tarfile.open(arch) as arch_: if extract_filter: to_extract = extract_filter(arch_) else: to_extract = arch_.getmembers() arch_.extractall(path=temp_dir, members=to_extract) except KeyError as err: _LOGGER.error(err) raise exc.LocalFileNotFoundError( 'Error while extracting {}: {}'.format(arch, err)) return [os.path.join(temp_dir, f.name) for f in to_extract]
def _get_all(log_id): """Return a file-like object with all the log entries including the rotated ones. """ instance, uniq, logtype, component = log_id.split('/') with lc.LogContext(_LOGGER, '{}/{}'.format(instance, uniq)): rel_log_dir = _rel_log_dir_path(logtype, component) abs_log_dir = _abs_log_dir_path(tm_env, instance, uniq, rel_log_dir) _LOGGER.info('Check logs in {}'.format(abs_log_dir)) if os.path.exists(abs_log_dir): logs = glob.glob(os.path.join(abs_log_dir, '@*.s')) logs.append(os.path.join(abs_log_dir, 'current')) # alphanumerical sort results in chronological order # as per # https://skarnet.org/software/skalibs/libstddjb/tai.html return _concat_files(sorted(logs)) if uniq == 'running': raise exc.LocalFileNotFoundError( 'No log could be found for {}.'.format(log_id)) logs = _extract_archive(_archive_path( tm_env, logtype, instance, uniq), extract_filter=functools.partial( _arch_log_filter, rel_log_dir=rel_log_dir)) # alphanumerical sort results in chronological order # as per # https://skarnet.org/software/skalibs/libstddjb/tai.html return _concat_files(sorted(logs))
def _get(archive_id): """Get arch file path. """ instance, uniq, arch_type = archive_id.split('/') arch_path = _archive_path(tm_env, arch_type, instance, uniq) if not os.path.exists(arch_path): raise exc.LocalFileNotFoundError( '{} cannot be found.'.format(arch_path)) return arch_path
def _get_file(file_=None, arch=None, arch_extract=True, arch_extract_filter=None): """Return the file pointed by "file_" or extract the specified archive. Return file_ if the specified file exists or extract the files specified by 'arch_extract_filter' from 'arch' and return the path to the extracted file. """ if os.path.exists(file_): _LOGGER.info('Returning {}'.format(file_)) return file_ if not arch_extract: raise exc.LocalFileNotFoundError('{} cannot be found.'.format(file_)) extracted = _extract_archive(arch, arch_extract_filter)[0] _LOGGER.info('Returning {}'.format(extracted)) return extracted