Skip to content

Commit

Permalink
refactor: utils.render_simple_detail
Browse files Browse the repository at this point in the history
- remove the simple_detail_path as it is not used by any other caller
- calculate size without writting the file, only write when store is
  true

Signed-off-by: Kairo de Araujo <kairo@dearaujo.nl>
  • Loading branch information
kairoaraujo committed Oct 7, 2024
1 parent 86803ac commit bdc3309
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
14 changes: 3 additions & 11 deletions tests/unit/packaging/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@ def test_render_simple_detail(db_request, monkeypatch, jinja):
context = _valid_simple_detail_context(context)
expected_content = template.render(**context, request=db_request).encode("utf-8")

content_hash, path, size = render_simple_detail(project, db_request)
content_hash, size = render_simple_detail(project, db_request)

assert fakeblake2b.calls == [pretend.call(digest_size=32)]
assert fake_hasher.update.calls == [pretend.call(expected_content)]
assert fake_hasher.hexdigest.calls == [pretend.call()]

assert content_hash == "deadbeefdeadbeefdeadbeefdeadbeef"
assert path == (
f"{project.normalized_name}/deadbeefdeadbeefdeadbeefdeadbeef"
+ f".{project.normalized_name}.html"
)
assert size == len(expected_content)


Expand All @@ -93,7 +89,7 @@ def test_render_simple_detail_with_store(db_request, monkeypatch, jinja):
fakeblake2b = pretend.call_recorder(lambda *a, **kw: fake_hasher)
monkeypatch.setattr(hashlib, "blake2b", fakeblake2b)

expected_size = 42
expected_size = 225
fake_named_temporary_file = pretend.stub(
name="/tmp/wutang",
write=pretend.call_recorder(lambda data: expected_size),
Expand All @@ -117,7 +113,7 @@ def __exit__(self, type, value, traceback):
context = _valid_simple_detail_context(context)
expected_content = template.render(**context, request=db_request).encode("utf-8")

content_hash, path, size = render_simple_detail(project, db_request, store=True)
content_hash, size = render_simple_detail(project, db_request, store=True)

assert fake_named_temporary_file.write.calls == [pretend.call(expected_content)]
assert fake_named_temporary_file.flush.calls == [pretend.call()]
Expand Down Expand Up @@ -151,8 +147,4 @@ def __exit__(self, type, value, traceback):
]

assert content_hash == "deadbeefdeadbeefdeadbeefdeadbeef"
assert path == (
f"{project.normalized_name}/deadbeefdeadbeefdeadbeefdeadbeef"
+ f".{project.normalized_name}.html"
)
assert size == expected_size
4 changes: 2 additions & 2 deletions tests/unit/tuf/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_update_metadata(self, db_request, monkeypatch):

rstuf_url = "url"
index_digest = "digest"
index_size = 42
index_size = 255

db_request.registry.settings = {"rstuf.api_url": rstuf_url}

render = call_recorder(lambda *a, **kw: (index_digest, None, index_size))
render = call_recorder(lambda *a, **kw: (index_digest, index_size))
tuf.tasks.render_simple_detail = render

rstuf = tuf.services.RSTUFService.create_service(db_request)
Expand Down
14 changes: 8 additions & 6 deletions warehouse/packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ def render_simple_detail(project, request, store=False):
f"{project.normalized_name}/{content_hash}.{project.normalized_name}.html"
)

with tempfile.NamedTemporaryFile() as f:
simple_detail_size = f.write(content.encode("utf-8"))
f.flush()
simple_detail_size = len(content.encode("utf-8"))

if store:
storage = request.find_service(ISimpleStorage)
with tempfile.NamedTemporaryFile() as f:
f.write(content.encode("utf-8"))
f.flush()

if store:
storage = request.find_service(ISimpleStorage)
storage.store(
simple_detail_path,
f.name,
Expand All @@ -148,7 +150,7 @@ def render_simple_detail(project, request, store=False):
},
)

return (content_hash, simple_detail_path, simple_detail_size)
return (content_hash, simple_detail_size)


def _valid_simple_detail_context(context: dict) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion warehouse/tuf/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_metadata(request: Request, project_id: UUID):
# NOTE: We ignore the returned simple detail path with the content hash as
# infix. In TUF metadata the project name and hash are listed separately, so
# that there is only one entry per target file, even if the content changes.
digest, _, size = render_simple_detail(project, request, store=True)
digest, size = render_simple_detail(project, request, store=True)
payload = {
"targets": [
{
Expand Down

0 comments on commit bdc3309

Please sign in to comment.