Skip to content

Commit

Permalink
refactor: don't walk directories recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwint committed Dec 31, 2023
1 parent ea2bafb commit 3e1814e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
25 changes: 8 additions & 17 deletions s3pypi/storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import deque
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Optional
Expand Down Expand Up @@ -53,22 +52,14 @@ def build_root_index(self) -> Index:
return Index(dict.fromkeys(self._list_dirs()))

def _list_dirs(self) -> List[str]:
results = set()
root = f"{p}/" if (p := self.cfg.prefix) else ""
todo = deque([root])
while todo:
current = todo.popleft()
if children := [
prefix
for item in self.s3.meta.client.get_paginator("list_objects_v2")
.paginate(Bucket=self.cfg.bucket, Delimiter="/", Prefix=current)
.search("CommonPrefixes")
if item and (prefix := item.get("Prefix"))
]:
todo.extend(children)
else:
results.add(current[len(root) :])
return sorted(results)
prefix = f"{p}/" if (p := self.cfg.prefix) else ""
return [
d[len(prefix) :]
for item in self.s3.meta.client.get_paginator("list_objects_v2")
.paginate(Bucket=self.cfg.bucket, Delimiter="/", Prefix=prefix)
.search("CommonPrefixes")
if item and (d := item.get("Prefix"))
]

def put_index(self, directory: str, index: Index) -> None:
self._object(directory, self.index_name).put(
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,4 @@ def test_list_dirs(boto3_session, s3_bucket):
cfg = S3Config(bucket=s3_bucket.name)
s = S3Storage(boto3_session, cfg)

assert s._list_dirs() == [
"AA/one/",
"AA/three/",
"AA/two/",
"BBBB/xxx/",
"BBBB/yyy/",
]
assert s._list_dirs() == ["AA/", "BBBB/"]

0 comments on commit 3e1814e

Please sign in to comment.