Skip to content

Commit

Permalink
Add GPath.unlink to remove a file
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 347998283
  • Loading branch information
Conchylicultor authored and copybara-github committed Dec 17, 2020
1 parent b572fe9 commit e5545c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tensorflow_datasets/core/utils/gpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ def rmtree(self) -> None:
"""Remove the directory."""
tf.io.gfile.rmtree(self._path_str)

def unlink(self, missing_ok: bool = False) -> None:
"""Remove this file or symbolic link."""
try:
tf.io.gfile.remove(self._path_str)
except tf.errors.NotFoundError as e:
if missing_ok:
pass
else:
raise FileNotFoundError(str(e))

def open(
self,
mode: str = 'r',
Expand Down
21 changes: 20 additions & 1 deletion tensorflow_datasets/core/utils/gpath_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _norm_path(path: str):
'listdir',
'makedirs',
'mkdir',
# 'remove',
'remove',
'rename',
'rmtree',
# 'stat',
Expand All @@ -66,6 +66,7 @@ def _norm_path(path: str):
listdir=lambda p: origin_gfile.listdir(_norm_path(p)),
makedirs=lambda p: origin_gfile.makedirs(_norm_path(p)),
mkdir=lambda p: origin_gfile.mkdir(_norm_path(p)),
remove=lambda p: origin_gfile.remove(_norm_path(p)),
rename=lambda p1, p2, **kwargs: origin_gfile.rename( # pylint: disable=g-long-lambda
_norm_path(p1), _norm_path(p2), **kwargs
),
Expand Down Expand Up @@ -256,6 +257,24 @@ def test_read_write():
assert gpath.read_bytes() == b'def'


@pytest.mark.usefixtures('gcs_mocked_path')
def test_unlink():
path = gpathlib.PosixGPath('gs://bucket')
path.mkdir()

path = path / 'text.txt'

with pytest.raises(FileNotFoundError):
path.unlink()

path.unlink(missing_ok=True) # no-op if missing_ok=True

path.touch() # Path created
assert path.exists()
path.unlink() # Path deleted
assert not path.exists()


def test_mkdir(gcs_mocked_path: pathlib.Path):
g_path = gpathlib.PosixGPath('gs://bucket')
assert not g_path.exists()
Expand Down
4 changes: 4 additions & 0 deletions tensorflow_datasets/core/utils/type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def rmdir(self) -> None:
def rmtree(self) -> None:
"""Remove the directory, including all sub-files."""

@abc.abstractmethod
def unlink(self, missing_ok: bool = False) -> None:
"""Remove this file or symbolic link."""

def write_bytes(self, data: bytes) -> None:
"""Writes content as bytes."""
with self.open('wb') as f:
Expand Down

0 comments on commit e5545c4

Please sign in to comment.