22
33import functools
44import os
5- import tempfile
65import urllib .parse
76
87from pathlib import Path
98from typing import TYPE_CHECKING
109
10+ from poetry .core .packages .utils .link import Link
11+
1112from poetry .inspection .info import PackageInfo
1213from poetry .inspection .info import PackageInfoError
1314from poetry .utils .helpers import download_file
1819if TYPE_CHECKING :
1920 from poetry .core .packages .package import Package
2021
22+ from poetry .utils .cache import ArtifactCache
23+
2124
2225@functools .lru_cache (maxsize = None )
2326def _get_package_from_git (
@@ -53,6 +56,9 @@ def _get_package_from_git(
5356
5457
5558class DirectOrigin :
59+ def __init__ (self , artifact_cache : ArtifactCache ) -> None :
60+ self ._artifact_cache = artifact_cache
61+
5662 @classmethod
5763 def get_package_from_file (cls , file_path : Path ) -> Package :
5864 try :
@@ -70,17 +76,22 @@ def get_package_from_file(cls, file_path: Path) -> Package:
7076 def get_package_from_directory (cls , directory : Path ) -> Package :
7177 return PackageInfo .from_directory (path = directory ).to_package (root_dir = directory )
7278
73- @classmethod
74- def get_package_from_url (cls , url : str ) -> Package :
79+ def get_package_from_url (self , url : str ) -> Package :
7580 file_name = os .path .basename (urllib .parse .urlparse (url ).path )
76- with tempfile .TemporaryDirectory () as temp_dir :
77- dest = Path (temp_dir ) / file_name
78- download_file (url , dest )
79- package = cls .get_package_from_file (dest )
80-
81- package .files = [
82- {"file" : file_name , "hash" : "sha256:" + get_file_hash (dest )}
83- ]
81+ link = Link (url )
82+ artifact = self ._artifact_cache .get_cached_archive_for_link (link , strict = True )
83+
84+ if not artifact :
85+ artifact = (
86+ self ._artifact_cache .get_cache_directory_for_link (link ) / file_name
87+ )
88+ artifact .parent .mkdir (parents = True , exist_ok = True )
89+ download_file (url , artifact )
90+
91+ package = self .get_package_from_file (artifact )
92+ package .files = [
93+ {"file" : file_name , "hash" : "sha256:" + get_file_hash (artifact )}
94+ ]
8495
8596 package ._source_type = "url"
8697 package ._source_url = url
0 commit comments