Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 19d1a5c

Browse files
committed
Fix issues with GCS path parsing.
1 parent 9e9e145 commit 19d1a5c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

tfrecorder/converter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _path_split(filepath: str) -> Tuple[str, str]:
8383

8484
if filepath.startswith(constants.GCS_PREFIX):
8585
_, path = filepath.split(constants.GCS_PREFIX)
86-
head, tail = os.path.split(path)
86+
head, tail = os.path.split(os.path.normpath(path))
8787
return constants.GCS_PREFIX + head, tail
8888

8989
return os.path.split(filepath)
@@ -138,7 +138,9 @@ def _read_image_directory(image_dir: str) -> pd.DataFrame:
138138
def _is_directory(input_data) -> bool:
139139
"""Returns True if `input_data` is a directory; False otherwise."""
140140

141-
return tf.io.gfile.isdir(input_data)
141+
# Note: First check will flag if user has the necessary credentials
142+
# to access the directory (if it is in GCS)
143+
return tf.io.gfile.exists(input_data) and tf.io.gfile.isdir(input_data)
142144

143145

144146
def _get_job_name(job_label: str = None) -> str:

tfrecorder/converter_test.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,24 @@
3838
# pylint: disable=protected-access
3939

4040

41-
class ClientTest(unittest.TestCase):
41+
class IsDirectoryTest(unittest.TestCase):
42+
"""Tests `_is_directory`."""
43+
44+
def test_local_ok(self):
45+
"""Test function returns True on local directory."""
46+
47+
with tempfile.TemporaryDirectory() as dirname:
48+
self.assertTrue(converter._is_directory(dirname))
49+
50+
def test_local_exists_but_not_dir(self):
51+
"""Test function returns False on local (non-directory) file."""
52+
53+
with tempfile.NamedTemporaryFile(prefix='test_', dir='/tmp') as f:
54+
self.assertFalse(converter._is_directory(f.name))
55+
56+
57+
# TODO(cezequiel): Refactor to per-function test case classes
58+
class MiscTest(unittest.TestCase):
4259
"""Misc tests for `client` module."""
4360

4461
def setUp(self):
@@ -88,11 +105,11 @@ def test_path_split(self):
88105
"""Tests `_path_split`."""
89106

90107
filename = 'image_file.jpg'
91-
dirpaths = ['/path/to/image/dir', 'gs://path/to/image/dir']
108+
dirpaths = ['/path/to/image/dir/', 'gs://path/to/image/dir/']
92109
for dir_ in dirpaths:
93110
filepath = os.path.join(dir_, filename)
94111
act_dirpath, act_filename = converter._path_split(filepath)
95-
self.assertEqual(act_dirpath, dir_)
112+
self.assertEqual(act_dirpath, dir_.rsplit('/', 1)[0])
96113
self.assertEqual(act_filename, filename)
97114

98115

0 commit comments

Comments
 (0)