Skip to content

Commit

Permalink
fix: path.owner() can raise when using filesystem adapter
Browse files Browse the repository at this point in the history
 - catch the error and return a None owner
  • Loading branch information
justindujardin committed May 23, 2020
1 parent 263c686 commit 2877b06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
23 changes: 12 additions & 11 deletions pathy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ def mv(from_location: str, to_location: str):
Move a blob or folder of blobs from one path to another.
"""
from_path: FluidPath = Pathy.fluid(from_location)
if not from_path.exists():
raise ValueError(f"from_path is not an existing Path or Pathy: {from_path}")
to_path: FluidPath = Pathy.fluid(to_location)

if from_path.is_file():
# Copy prefix from the source if the to_path has none.
#
# e.g. "cp ./file.txt gs://bucket-name/" writes "gs://bucket-name/file.txt"
if isinstance(to_path, Pathy) and to_path.prefix == "":
to_path = to_path / from_path
to_path.parent.mkdir(parents=True, exist_ok=True)
to_path.write_bytes(from_path.read_bytes())
from_path.unlink()
return

if from_path.is_dir():
to_path.mkdir(parents=True, exist_ok=True)
to_unlink = []
Expand All @@ -53,15 +63,6 @@ def mv(from_location: str, to_location: str):
blob.unlink()
if from_path.is_dir():
from_path.rmdir()
elif from_path.is_file():
# Copy prefix from the source if the to_path has none.
#
# e.g. "cp ./file.txt gs://bucket-name/" writes "gs://bucket-name/file.txt"
if isinstance(to_path, Pathy) and to_path.prefix == "":
to_path = to_path / from_path
to_path.parent.mkdir(parents=True, exist_ok=True)
to_path.write_bytes(from_path.read_bytes())
from_path.unlink()


@app.command()
Expand Down
9 changes: 8 additions & 1 deletion pathy/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@ def get_blob(self, blob_name: str) -> Optional[ClientBlobFS]:
if not native_blob.exists() or native_blob.is_dir():
return None
stat = native_blob.stat()
# path.owner() raises KeyError if the owner's UID isn't known
#
# https://docs.python.org/3/library/pathlib.html#pathlib.Path.owner
try:
owner = native_blob.owner()
except KeyError:
owner = None
return ClientBlobFS(
bucket=self,
owner=native_blob.owner(),
owner=owner,
name=blob_name,
raw=native_blob,
size=stat.st_size,
Expand Down

0 comments on commit 2877b06

Please sign in to comment.