def test_open_as_container(self): # Create a container for testing pfio.set_root("posix") zip_file_name = "test" zip_file_path = zip_file_name + ".zip" # in the zip, the leading slash will be removed # TODO(tianqi): related to issue #61 dirname_zip = self.tmpdir.name.lstrip('/') + '/' file_name_zip = self.tmpfile_path.lstrip('/') first_level_dir = dirname_zip.split('/')[0] shutil.make_archive(zip_file_name, "zip", base_dir=self.tmpdir.name) with pfio.open_as_container(zip_file_path) as container: file_generator = container.list() file_list = list(file_generator) self.assertIn(first_level_dir, file_list) self.assertNotIn(file_name_zip, file_list) self.assertNotIn("", file_list) file_generator = container.list(dirname_zip) file_list = list(file_generator) self.assertNotIn(first_level_dir, file_list) self.assertIn(os.path.basename(file_name_zip), file_list) self.assertNotIn("", file_list) self.assertTrue(container.isdir(dirname_zip)) self.assertFalse(container.isdir(file_name_zip)) self.assertIsInstance(container.info(), str) with container.open(file_name_zip, "r") as f: self.assertEqual(f.read(), self.test_string_str) pfio.remove(zip_file_path)
def test_root_local_override(self): pfio.set_root('file://' + self.tmpdir.name) with pfio.open(self.tmpfile_name, "r") as fp: self.assertEqual(fp.read(), self.test_string_str) # override with full URI with open(__file__, "r") as my_script: with pfio.open('file://' + __file__) as fp: self.assertEqual(fp.read(), my_script.read().encode("utf-8"))
def test_set_root(self): # Set default context globally in this process pfio.set_root('posix') # Using the context to open local file with pfio.open(self.tmpfile_path, "r") as fp: self.assertEqual(fp.read(), self.test_string_str) pfio.set_root('file://' + self.tmpdir.name) with pfio.open(self.tmpfile_name, "r") as fp: self.assertEqual(fp.read(), self.test_string_str)
def test_root_fs_override(self): from pyarrow import hdfs hdfs_tmpfile = "tmpfile_hdfs" hdfs_file_string = "this is a test string for hdfs" conn = hdfs.connect() with conn.open(hdfs_tmpfile, "wb") as f: f.write(hdfs_file_string.encode('utf-8')) pfio.set_root("hdfs") with pfio.open(hdfs_tmpfile, "r") as fp: self.assertEqual(fp.read(), hdfs_file_string) # override with full URI with open(__file__, "r") as my_script: with pfio.open("file://" + __file__, "r") as fp: self.assertEqual(fp.read(), my_script.read()) with pfio.open(hdfs_tmpfile, "r") as fp: self.assertEqual(fp.read(), hdfs_file_string) conn.delete(hdfs_tmpfile) conn.close()