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"))
def get_dbpedia(data_dir): train_path = os.path.join(data_dir, 'dbpedia_csv/train.csv') test_path = os.path.join(data_dir, 'dbpedia_csv/test.csv') if not (gfile.Exists(train_path) and gfile.Exists(test_path)): archive_path = base.maybe_download('dbpedia_csv.tar.gz', data_dir, DBPEDIA_URL) tfile = tarfile.open(archive_path, 'r:*') tfile.extractall(data_dir) train = base.load_csv(train_path, np.int32, 0, has_header=False) test = base.load_csv(test_path, np.int32, 0, has_header=False) datasets = base.Datasets(train=train, validation=None, test=test) return datasets
def testMkDirsGlobAndRmDirs(self): self.assertFalse(gfile.Exists(self.tmp + "test_dir")) gfile.MkDir(self.tmp + "test_dir") self.assertTrue(gfile.Exists(self.tmp + "test_dir")) gfile.RmDir(self.tmp + "test_dir") self.assertFalse(gfile.Exists(self.tmp + "test_dir")) gfile.MakeDirs(self.tmp + "test_dir/blah0") gfile.MakeDirs(self.tmp + "test_dir/blah1") self.assertEqual( [self.tmp + "test_dir/blah0", self.tmp + "test_dir/blah1"], sorted(gfile.Glob(self.tmp + "test_dir/*"))) gfile.DeleteRecursively(self.tmp + "test_dir") self.assertFalse(gfile.Exists(self.tmp + "test_dir"))
def testErrors(self): self.assertRaises(OSError, lambda: gfile.RmDir(self.tmp + "dir_doesnt_exist")) self.assertRaises(OSError, lambda: gfile.Remove(self.tmp + "file_doesnt_exist")) gfile.MkDir(self.tmp + "error_dir") with gfile.GFile(self.tmp + "error_dir/file", "w"): pass # Create file self.assertRaises(OSError, lambda: gfile.Remove(self.tmp + "error_dir")) self.assertRaises(OSError, lambda: gfile.RmDir(self.tmp + "error_dir")) self.assertTrue(gfile.Exists(self.tmp + "error_dir")) gfile.DeleteRecursively(self.tmp + "error_dir") self.assertFalse(gfile.Exists(self.tmp + "error_dir"))
def maybe_download(filename, work_directory, source_url): """Download the data from source url, unless it's already here.""" if not gfile.Exists(work_directory): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): with tempfile.NamedTemporaryFile() as tmpfile: temp_file_name = tmpfile.name urllib.request.urlretrieve(source_url, temp_file_name) gfile.Copy(temp_file_name, filepath) with gfile.GFile(filepath) as f: size = f.Size() print('Successfully downloaded', filename, size, 'bytes.') return filepath
def testRename(self): gfile.MkDir(self.tmp + "dir1") gfile.MkDir(self.tmp + "dir2") with gfile.GFile(self.tmp + "file1", "w"): pass # Create file with gfile.GFile(self.tmp + "file2", "w"): pass # Create file # Dest file already exists, overwrite=False (default). self.assertRaises( OSError, lambda: gfile.Rename(self.tmp + "file1", self.tmp + "file2")) gfile.Rename(self.tmp + "file1", self.tmp + "file2", overwrite=True) self.assertFalse(gfile.Exists(self.tmp + "file1")) gfile.Rename(self.tmp + "file2", self.tmp + "newfile") self.assertTrue(gfile.Exists(self.tmp + "newfile")) gfile.Rename(self.tmp + "dir1", self.tmp + "dir2") self.assertFalse(gfile.Exists(self.tmp + "dir1")) gfile.Rename(self.tmp + "dir2", self.tmp + "newdir") self.assertTrue(gfile.Exists(self.tmp + "newdir"))
def maybe_download(filename, work_directory, source_url): """Download the data from source url, unless it's already here. Args: filename: string, name of the file in the directory. work_directory: string, path to working directory. source_url: url to download from if file doesn't exist. Returns: Path to resulting file. """ if not gfile.Exists(work_directory): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): with tempfile.NamedTemporaryFile() as tmpfile: temp_file_name = tmpfile.name urllib.request.urlretrieve(source_url, temp_file_name) gfile.Copy(temp_file_name, filepath) with gfile.GFile(filepath) as f: size = f.Size() print('Successfully downloaded', filename, size, 'bytes.') return filepath
def _write_with_backup(filename, content): if gfile.Exists(filename): gfile.Rename(filename, filename + '.old', overwrite=True) with gfile.Open(filename, 'w') as f: f.write(content)
def testExists(self): self.assertFalse(gfile.Exists(self.tmp + "test_exists")) with gfile.GFile(self.tmp + "test_exists", "w"): pass self.assertTrue(gfile.Exists(self.tmp + "test_exists"))