Skip to content

Commit ad7a970

Browse files
committed
raise exc in mange_fs if fs is not writeable
1 parent 20203d9 commit ad7a970

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
([#527](https://github.com/PyFilesystem/pyfilesystem2/pull/527)).
2020
- Optimized moving files between filesystems with syspaths.
2121
([#523](https://github.com/PyFilesystem/pyfilesystem2/pull/523)).
22-
- Fixed `move.move_file` to clean up the copy on the destination in case of errors.
22+
- Fixed `fs.move.move_file` to clean up the copy on the destination in case of errors.
23+
- `fs.opener.manage_fs` with `writeable=True` will now raise a `ResourceReadOnly`
24+
exception if the managed filesystem is not writeable.
2325

2426

2527
## [2.4.15] - 2022-02-07

fs/move.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@ def move_file(
7171
if _src_fs.hassyspath(src_path) and _dst_fs.hassyspath(dst_path):
7272
# if both filesystems have a syspath we create a new OSFS from a
7373
# common parent folder and use it to move the file.
74-
75-
# we have to raise ResourceReadOnly manually if a FS is read-only
76-
if _src_fs.getmeta().get("read_only", True):
77-
raise ResourceReadOnly(_src_fs, src_path)
78-
if _dst_fs.getmeta().get("read_only", True):
79-
raise ResourceReadOnly(_dst_fs, dst_path)
80-
8174
try:
8275
src_syspath = _src_fs.getsyspath(src_path)
8376
dst_syspath = _dst_fs.getsyspath(dst_path)
@@ -136,10 +129,10 @@ def move_dir(
136129
"""
137130

138131
def src():
139-
return manage_fs(src_fs, writeable=False)
132+
return manage_fs(src_fs, writeable=True)
140133

141134
def dst():
142-
return manage_fs(dst_fs, create=True)
135+
return manage_fs(dst_fs, writeable=True, create=True)
143136

144137
with src() as _src_fs, dst() as _dst_fs:
145138
with _src_fs.lock(), _dst_fs.lock():

fs/opener/registry.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from .base import Opener
1616
from .errors import UnsupportedProtocol, EntryPointError
17+
from ..errors import ResourceReadOnly
1718
from .parse import parse_fs_url
1819

1920
if typing.TYPE_CHECKING:
@@ -282,10 +283,18 @@ def manage_fs(
282283
"""
283284
from ..base import FS
284285

286+
def assert_writeable(fs):
287+
if fs.getmeta().get("read_only", True):
288+
raise ResourceReadOnly(path=str(fs_url))
289+
285290
if isinstance(fs_url, FS):
291+
if writeable:
292+
assert_writeable(fs_url)
286293
yield fs_url
287294
else:
288295
_fs = self.open_fs(fs_url, create=create, writeable=writeable, cwd=cwd)
296+
if writeable:
297+
assert_writeable(_fs)
289298
try:
290299
yield _fs
291300
finally:

0 commit comments

Comments
 (0)