def _extract_last_nlines(path, nlines=25): """Attempt to extract the last nlines from a file If the file is not found or there's an error parsing the file, an empty string is returned. """ try: n = nlines + 1 nfs_exists_check(path) with open(path, 'r') as f: s = f.readlines() return "".join(s[-n:]) except Exception as e: log.warn("Unable to extract stderr from {p}. {e}".format(p=path, e=e)) return ""
def _validate_resource(func, resource): """Validate the existence of a file/dir""" _ = nfs_exists_check(resource) if func(resource): return os.path.abspath(resource) else: raise IOError("Unable to find '{f}'".format(f=resource))
def fofn_to_files(fofn): """Util func to convert a bas/bax fofn file to a list of bas/bax files.""" _ = nfs_exists_check(fofn) if os.path.exists(fofn): with open(fofn, 'r') as f: bas_files = {line.strip() for line in f.readlines()} for bas_file in bas_files: if not os.path.isfile(bas_file): # try one more time to find the file by # performing an NFS refresh found = nfs_exists_check(bas_file) if not found: raise IOError("Unable to find bas/bax file '{f}'".format(f=bas_file)) return list(bas_files) else: raise IOError("Unable to find FOFN {f}".format(f=fofn))
def validate_fofn(fofn): """Validate existence of FOFN and files within the FOFN. :param fofn: (str) Path to File of file names. :raises: IOError if any file is not found. :return: (str) abspath of the input fofn """ _ = nfs_exists_check(fofn) if os.path.isfile(fofn): file_names = fofn_to_files(os.path.abspath(fofn)) log.debug("Found {n} files in FOFN {f}.".format(n=len(file_names), f=fofn)) return os.path.abspath(fofn) else: raise IOError("Unable to find {f}".format(f=fofn))
def trigger_nfs_refresh(ff): # keeping this for backward compatibility return nfs_exists_check(ff)