Created
November 19, 2013 01:41
-
-
Save mpkocher/7538784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
def get_parser(): | |
parser = argparse.ArgumentParser(version=__version__, | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('input_fofn', help="Path to bas.fofn", | |
type=validate_fofn) | |
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import logging | |
import functools | |
import time | |
log = logging.getLogger(__name__) | |
def _validate_resource(func, resource): | |
"""Validate the existence of a file/dir""" | |
if func(resource): | |
return os.path.abspath(resource) | |
else: | |
raise IOError("Unable to find {f}".format(f=resource)) | |
validate_file = functools.partial(_validate_resource, os.path.isfile) | |
validate_dir = functools.partial(_validate_resource, os.path.isdir) | |
validate_output_dir = functools.partial(_validate_resource, os.path.isdir) | |
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 | |
""" | |
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 fofn_to_files(fofn): | |
"""Util func to convert a bas/bax fofn file to a list of bas/bax files.""" | |
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): | |
raise IOError("Unable to find bas/bax file '{f}'".format(f=bas_file)) | |
if len(set(bas_files)) != len(bas_files): | |
raise IOError("Detected duplicate files in fofn. {n} unique, {m} total files in {f}".format(f=fofn, m=len(bas_files), n=len(set(bas_files)))) | |
return bas_files | |
else: | |
raise IOError("Unable to find FOFN {f}".format(f=fofn)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment