From 2877b06562e4bb1d4767e9c297e2aee2fc1284ad Mon Sep 17 00:00:00 2001 From: justindujardin Date: Sat, 23 May 2020 13:01:23 -0700 Subject: [PATCH] fix: path.owner() can raise when using filesystem adapter - catch the error and return a None owner --- pathy/cli.py | 23 ++++++++++++----------- pathy/file.py | 9 ++++++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pathy/cli.py b/pathy/cli.py index b217d56..bf732e3 100644 --- a/pathy/cli.py +++ b/pathy/cli.py @@ -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 = [] @@ -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() diff --git a/pathy/file.py b/pathy/file.py index 6e6891a..59d6acf 100644 --- a/pathy/file.py +++ b/pathy/file.py @@ -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,