TensorFlow: add gfile.Copy implementation and test. Fixes #1102.

Change: 114712488
This commit is contained in:
Vijay Vasudevan 2016-02-15 16:50:56 -08:00
parent 02ab1dde36
commit bf08a419e5
2 changed files with 45 additions and 0 deletions

View File

@ -420,3 +420,21 @@ def Stat(path): # pylint: disable=invalid-name
filestat = collections.namedtuple('FileStat', ['mtime'])
filestat.mtime = statinfo.st_mtime
return filestat
def Copy(oldpath, newpath, overwrite=False):
"""Copy a file.
Args:
oldpath: string; a pathname of a file.
newpath: string; a pathname to which the file will be copied.
overwrite: boolean; if false, it is an error for newpath to be
occupied by an existing file.
Raises:
OSError: If "newpath" is occupied by an existing file and overwrite=False,
or any error thrown by shutil.copy.
"""
if not overwrite and Exists(newpath):
raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), newpath)
shutil.copy(oldpath, newpath)

View File

@ -194,5 +194,32 @@ class FunctionTests(_BaseTest, googletest.TestCase):
gfile.Rename(self.tmp + "dir2", self.tmp + "newdir")
self.assertTrue(gfile.Exists(self.tmp + "newdir"))
def testCopy(self):
gfile.MkDir(self.tmp + "dir1")
gfile.MkDir(self.tmp + "dir2")
with gfile.GFile(self.tmp + "dir1/file1", "w"):
pass # Create file
with gfile.GFile(self.tmp + "dir2/file2", "w"):
pass # Create file
# Dest file already exists, overwrite=False (default).
self.assertRaises(
OSError, lambda: gfile.Copy(self.tmp + "dir1/file1",
self.tmp + "dir2/file2"))
# Overwrite succeeds
gfile.Copy(self.tmp + "dir1/file1", self.tmp + "dir2/file2",
overwrite=True)
self.assertTrue(gfile.Exists(self.tmp + "dir2/file2"))
# Normal copy.
gfile.Rename(self.tmp + "dir1/file1", self.tmp + "dir2/file1")
self.assertTrue(gfile.Exists(self.tmp + "dir2/file1"))
# Normal copy to non-existent dir
self.assertRaises(OSError,
lambda: gfile.Rename(self.tmp + "dir1/file1",
self.tmp + "newdir/file1"))
if __name__ == "__main__":
googletest.main()