def test_basic(ftp): host, port = ftp fs = FTPFileSystem(host, port) assert fs.ls('/', detail=False) == sorted( ['/' + f for f in os.listdir(here)]) out = fs.cat('/' + os.path.basename(__file__)) assert out == open(__file__, 'rb').read()
def ftp_writable(tmpdir): """ Fixture providing a writable FTP filesystem. """ pytest.importorskip("pyftpdlib") from fsspec.implementations.ftp import FTPFileSystem FTPFileSystem.clear_instance_cache() # remove lingering connections CachingFileSystem.clear_instance_cache() d = str(tmpdir) with open(os.path.join(d, "out"), "wb") as f: f.write(b"hello" * 10000) P = subprocess.Popen([ sys.executable, "-m", "pyftpdlib", "-d", d, "-u", "user", "-P", "pass", "-w" ]) try: time.sleep(1) yield "localhost", 2121, "user", "pass" finally: P.terminate() P.wait() try: shutil.rmtree(tmpdir) except Exception: pass
def test_readinto_with_multibyte(ftp_writable, tmpdir, dt): host, port, user, pw = ftp_writable ftp = FTPFileSystem(host=host, port=port, username=user, password=pw) with ftp.open("/out", "wb") as fp: arr = np.arange(10, dtype=dt) fp.write(arr.tobytes()) with ftp.open("/out", "rb") as fp: arr2 = np.empty_like(arr) fp.readinto(arr2) assert np.array_equal(arr, arr2)
def test_cat_get(ftp_writable, tmpdir): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw, block_size=500) fs.mkdir("/tmp") data = b"hello" * 500 fs.pipe("/tmp/myfile", data) assert fs.cat_file("/tmp/myfile") == data fn = os.path.join(tmpdir, "lfile") fs.get_file("/tmp/myfile", fn) assert open(fn, "rb").read() == data
def test_transaction(ftp_writable): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) fs.mkdir('/tmp') fn = '/tr' with fs.transaction: with fs.open(fn, 'wb') as f: f.write(b'not') assert not fs.exists(fn) assert fs.exists(fn) assert fs.cat(fn) == b'not'
def ftp_writable(tmpdir): FTPFileSystem.clear_instance_cache() # remove lingering connections d = str(tmpdir) with open(os.path.join(d, "out"), "wb") as f: f.write(b"hello" * 10000) P = subprocess.Popen([ sys.executable, "-m", "pyftpdlib", "-d", d, "-u", "user", "-P", "pass", "-w" ]) try: time.sleep(1) yield "localhost", 2121, "user", "pass" finally: P.terminate() P.wait() try: shutil.rmtree(tmpdir) except: pass
def test_write_big(ftp_writable, cache_type): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw, block_size=1000, cache_type=cache_type) fn = '/bigger' with fs.open(fn, 'wb') as f: f.write(b'o' * 500) assert not fs.exists(fn) f.write(b'o' * 1000) fs.invalidate_cache() assert fs.exists(fn) f.write(b'o' * 200) f.flush() assert f.buffer.tell() == 0 assert fs.info(fn)['size'] == 1700 assert fs.cat(fn) == b'o' * 1700
def test_write_big(ftp_writable, cache_type): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw, block_size=1000, cache_type=cache_type) fn = "/bigger" with fs.open(fn, "wb") as f: f.write(b"o" * 500) assert not fs.exists(fn) f.write(b"o" * 1000) fs.invalidate_cache() assert fs.exists(fn) f.write(b"o" * 200) f.flush() assert fs.info(fn)["size"] == 1700 assert fs.cat(fn) == b"o" * 1700
def test_write_small(ftp_writable): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) with fs.open("/out2", "wb") as f: f.write(b"oi") assert fs.cat("/out2") == b"oi"
def test_not_cached(ftp): host, port = ftp fs = FTPFileSystem(host, port) fs2 = FTPFileSystem(host, port) assert fs is not fs2
def test_basic(ftp): host, port = ftp fs = FTPFileSystem(host, port) assert fs.ls("/", detail=False) == sorted(os.listdir(here)) out = fs.cat("/" + os.path.basename(__file__)) assert out == open(__file__, "rb").read()
def test_rm_recursive(ftp_writable): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) fs.mkdir("/tmp/topdir") fs.mkdir("/tmp/topdir/underdir") fs.touch("/tmp/topdir/afile") fs.touch("/tmp/topdir/underdir/afile") with pytest.raises(ftplib.error_perm): fs.rmdir("/tmp/topdir") fs.rm("/tmp/topdir", recursive=True) assert not fs.exists("/tmp/topdir")
def test_mkdir(ftp_writable): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) with pytest.raises(ftplib.error_perm): fs.mkdir("/tmp/not/exist", create_parents=False) fs.mkdir("/tmp/not/exist") assert fs.exists("/tmp/not/exist") fs.makedirs("/tmp/not/exist", exist_ok=True) with pytest.raises(FileExistsError): fs.makedirs("/tmp/not/exist", exist_ok=False) fs.makedirs("/tmp/not/exist/inner/inner") assert fs.isdir("/tmp/not/exist/inner/inner")
def test_transaction_with_cache(ftp_writable, tmpdir): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) fs.mkdir("/tmp") fs.mkdir("/tmp/dir") assert "dir" in fs.ls("/tmp", detail=False) with fs.transaction: fs.rmdir("/tmp/dir") assert "dir" not in fs.ls("/tmp", detail=False) assert not fs.exists("/tmp/dir")
def test_transaction(ftp_writable): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) fs.mkdir("/tmp") fn = "/tr" with fs.transaction: with fs.open(fn, "wb") as f: f.write(b"not") assert not fs.exists(fn) assert fs.exists(fn) assert fs.cat(fn) == b"not" fs.rm(fn) assert not fs.exists(fn)
def test_write_small(ftp_writable): host, port, user, pw = ftp_writable fs = FTPFileSystem(host, port, user, pw) with fs.open('/out2', 'wb') as f: f.write(b'oi') assert fs.cat('/out2') == b'oi'