Skip to content

Commit

Permalink
ls: support versions (#509)
Browse files Browse the repository at this point in the history
`find` already supports this useful flag, and this PR is adding the same flag
to `ls`, same as in `s3fs.ls`.
  • Loading branch information
efiop authored Nov 16, 2022
1 parent 805d3fd commit b4eabc0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gcsfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,14 +815,26 @@ async def _glob(self, path, prefix="", **kwargs):
prefix = path[:ind].split("/")[-1]
return await super()._glob(path, prefix=prefix, **kwargs)

async def _ls(self, path, detail=False, prefix="", **kwargs):
async def _ls(self, path, detail=False, prefix="", versions=False, **kwargs):
"""List objects under the given '/{bucket}/{prefix} path."""
path = self._strip_protocol(path).rstrip("/")

if path in ["/", ""]:
out = await self._list_buckets()
else:
out = await self._list_objects(path, prefix=prefix)
out = []
for entry in await self._list_objects(
path, prefix=prefix, versions=versions
):
if versions:
out.append(
{
**entry,
"name": f"{entry['name']}#{entry['generation']}",
}
)
else:
out.append(entry)

if detail:
return out
Expand Down
16 changes: 16 additions & 0 deletions gcsfs/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,22 @@ def test_cp_versioned(gcs_versioned):
assert gcs_versioned.cat(b) == b"v1"


def test_ls_versioned(gcs_versioned):
import posixpath

with gcs_versioned.open(a, "wb") as wo:
wo.write(b"v1")
v1 = gcs_versioned.info(a)["generation"]
with gcs_versioned.open(a, "wb") as wo:
wo.write(b"v2")
v2 = gcs_versioned.info(a)["generation"]
dpath = posixpath.dirname(a)
assert {f"{a}#{v1}", f"{a}#{v2}"} == set(gcs_versioned.ls(dpath, versions=True))
assert {f"{a}#{v1}", f"{a}#{v2}"} == set(
entry["name"] for entry in gcs_versioned.ls(dpath, detail=True, versions=True)
)


def test_find_versioned(gcs_versioned):
with gcs_versioned.open(a, "wb") as wo:
wo.write(b"v1")
Expand Down

0 comments on commit b4eabc0

Please sign in to comment.