Skip to content

Commit

Permalink
Merge pull request #772 from jessicamizzi/existerror
Browse files Browse the repository at this point in the history
Start of issue 718, file not existing clean up
  • Loading branch information
mr-c committed Mar 16, 2015
2 parents 7fe13cf + 8627ecd commit fd7a63d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2015-03-16 Jessica Mizzi <mizzijes@msu.edu>

* khmer/kfile.py: Added file not existing error for system exit
* tests/{test_scripts,test_functions}.py: Added tests for
check_file_status for file existence and force option

2015-03-15 Kevin Murray <spam@kdmurray.id.au> & Titus Brown <titus@idyll.org>

* tests/test_counting_hash.py: Skip get_raw_tables test if python doesn't
Expand Down
14 changes: 13 additions & 1 deletion khmer/kfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ def check_file_status(file_path, force):
AND if the file is NOT a fifo/block/named pipe then a warning is printed
and sys.exit(1) is called
"""
mode = None

if file_path is '-':
return
mode = os.stat(file_path).st_mode
try:
mode = os.stat(file_path).st_mode
except OSError:
print >>sys.stderr, "ERROR: Input file %s does not exist" % \
file_path

if not force:
print >>sys.stderr, "Exiting"
sys.exit(1)
else:
return

# block devices will be nonzero
if S_ISBLK(mode) or S_ISFIFO(mode):
return
Expand Down
19 changes: 19 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import collections
from khmer.utils import (check_is_pair, broken_paired_reader, check_is_left,
check_is_right)
from khmer.kfile import check_file_status


def test_forward_hash():
Expand Down Expand Up @@ -115,6 +116,24 @@ def test_extract_hashbits_info():
print >>sys.stderr, '...failed to remove {fn}'.format(fn)


def test_check_file_status_kfile():
fn = utils.get_temp_filename('thisfiledoesnotexist')
check_file_status_exited = False
try:
check_file_status(fn, False)
except SystemExit:
check_file_status_exited = True
assert check_file_status_exited


def test_check_file_status_kfile_force():
fn = utils.get_temp_filename('thisfiledoesnotexist')
try:
check_file_status(fn, True)
except OSError as e:
assert False


FakeFQRead = collections.namedtuple('Read', ['name', 'quality', 'sequence'])
FakeFastaRead = collections.namedtuple('Read', ['name', 'sequence'])

Expand Down
12 changes: 12 additions & 0 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2814,3 +2814,15 @@ def test_roundtrip_casava_format_2():
r = open(infile).read()
r2 = open(outfile).read()
assert r == r2, (r, r2)


def test_existance_failure():
expected_output = 'ERROR: Input file'

args = [utils.get_temp_filename('thisfiledoesnotexistatall')]

status, out, err = utils.runscript(
'extract-paired-reads.py', args, fail_ok=True)
assert status == 1

assert expected_output in err

0 comments on commit fd7a63d

Please sign in to comment.