Skip to content

Commit

Permalink
Add hash function to URLDependency
Browse files Browse the repository at this point in the history
This will enable URLDependency to compute the hash for the file at the
temporary path. The temporary path should be the path to the downloaded
file from `URLDependency.url`.
  • Loading branch information
dunkmann00 committed Nov 30, 2022
1 parent d98aa6e commit 0ef3768
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/poetry/core/packages/url_dependency.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from __future__ import annotations

import hashlib
import io

from typing import TYPE_CHECKING
from typing import Iterable
from urllib.parse import urlparse

from poetry.core.packages.dependency import Dependency


if TYPE_CHECKING:
from pathlib import Path


class URLDependency(Dependency):
def __init__(
self,
Expand Down Expand Up @@ -44,6 +52,14 @@ def url(self) -> str:
def directory(self) -> str | None:
return self._directory

def hash(self, temp_path: Path, hash_name: str = "sha256") -> str:
h = hashlib.new(hash_name)
with temp_path.open("rb") as fp:
for content in iter(lambda: fp.read(io.DEFAULT_BUFFER_SIZE), b""):
h.update(content)

return h.hexdigest()

@property
def base_pep_508_name(self) -> str:
requirement = self.pretty_name
Expand Down

0 comments on commit 0ef3768

Please sign in to comment.