2
2
3
3
import functools
4
4
import os
5
- import tempfile
6
5
import urllib .parse
7
6
8
7
from pathlib import Path
9
8
from typing import TYPE_CHECKING
10
9
10
+ from poetry .core .packages .utils .link import Link
11
+
11
12
from poetry .inspection .info import PackageInfo
12
13
from poetry .inspection .info import PackageInfoError
13
14
from poetry .utils .helpers import download_file
18
19
if TYPE_CHECKING :
19
20
from poetry .core .packages .package import Package
20
21
22
+ from poetry .utils .cache import ArtifactCache
23
+
21
24
22
25
@functools .lru_cache (maxsize = None )
23
26
def _get_package_from_git (
@@ -53,6 +56,9 @@ def _get_package_from_git(
53
56
54
57
55
58
class DirectOrigin :
59
+ def __init__ (self , artifact_cache : ArtifactCache ) -> None :
60
+ self ._artifact_cache = artifact_cache
61
+
56
62
@classmethod
57
63
def get_package_from_file (cls , file_path : Path ) -> Package :
58
64
try :
@@ -70,17 +76,22 @@ def get_package_from_file(cls, file_path: Path) -> Package:
70
76
def get_package_from_directory (cls , directory : Path ) -> Package :
71
77
return PackageInfo .from_directory (path = directory ).to_package (root_dir = directory )
72
78
73
- @classmethod
74
- def get_package_from_url (cls , url : str ) -> Package :
79
+ def get_package_from_url (self , url : str ) -> Package :
75
80
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
+ ]
84
95
85
96
package ._source_type = "url"
86
97
package ._source_url = url
0 commit comments