@@ -68,9 +68,16 @@ def get_python_version_from_env_tag(python_version: str):
6868 return python_version
6969
7070
71- def get_sdist_file (repos , candidate , python_version ):
71+ def fetch_and_extract_sdist (repos , candidate , python_version ):
7272 """
73- Return the sdist file for a candidate.
73+ Fetch and extract the source distribution (sdist) for the ``candidate`` Candidate
74+ from the `repos` list of PyPiRepository
75+ and a required ``python_version`` Python version.
76+ Return the directory location string where the sdist has been extracted.
77+ Return None if the sdist was not fetched either
78+ because does not exist in any of the ``repos`` or it does not work with
79+ the required ``python_version``.
80+ Raise an Exception if extraction fails.
7481 """
7582 sdist = utils_pypi .download_sdist (
7683 name = candidate .name ,
@@ -82,25 +89,23 @@ def get_sdist_file(repos, candidate, python_version):
8289 if not sdist :
8390 return
8491
85- sdist_file = None
86-
8792 if sdist .endswith (".tar.gz" ):
8893 sdist_file = sdist .rstrip (".tar.gz" )
8994 with tarfile .open (os .path .join (utils_pypi .CACHE_THIRDPARTY_DIR , sdist )) as file :
9095 file .extractall (
9196 os .path .join (utils_pypi .CACHE_THIRDPARTY_DIR , "extracted_sdists" , sdist_file )
9297 )
93- if sdist .endswith (".zip" ):
98+ elif sdist .endswith (".zip" ):
9499 sdist_file = sdist .rstrip (".zip" )
95100 with ZipFile (os .path .join (utils_pypi .CACHE_THIRDPARTY_DIR , sdist )) as zip :
96101 zip .extractall (
97102 os .path .join (utils_pypi .CACHE_THIRDPARTY_DIR , "extracted_sdists" , sdist_file )
98103 )
99104
100- if not sdist_file :
105+ else :
101106 raise Exception (f"Unable to extract sdist { sdist } " )
102107
103- return sdist_file
108+ return os . path . join ( utils_pypi . CACHE_THIRDPARTY_DIR , "extracted_sdists" , sdist_file , sdist_file )
104109
105110
106111class PythonInputProvider (AbstractProvider ):
@@ -212,32 +217,21 @@ def get_requirements_for_package_from_pypi_simple(self, candidate):
212217 if dep .scope == "install" :
213218 yield packaging .requirements .Requirement (str (dep .extracted_requirement ))
214219
215- sdist_file = get_sdist_file (
220+ sdist_file = fetch_and_extract_sdist (
216221 repos = self .repos , candidate = candidate , python_version = python_version
217222 )
218223
219224 if sdist_file :
220225 setup_py_path = os .path .join (
221- utils_pypi .CACHE_THIRDPARTY_DIR ,
222- "extracted_sdists" ,
223- sdist_file ,
224226 sdist_file ,
225227 "setup.py" ,
226228 )
227229 setup_cfg_path = os .path .join (
228- utils_pypi .CACHE_THIRDPARTY_DIR ,
229- "extracted_sdists" ,
230- sdist_file ,
231230 sdist_file ,
232231 "setup.cfg" ,
233232 )
234- pkg_info_path = os .path .join (
235- utils_pypi .CACHE_THIRDPARTY_DIR , "extracted_sdists" , sdist_file
236- )
233+ pkg_info_path = os .path .dirname (sdist_file )
237234 requirement_path = os .path .join (
238- utils_pypi .CACHE_THIRDPARTY_DIR ,
239- "extracted_sdists" ,
240- sdist_file ,
241235 sdist_file ,
242236 "requirements.txt" ,
243237 )
@@ -254,30 +248,31 @@ def get_requirements_for_package_from_pypi_simple(self, candidate):
254248 continue
255249
256250 deps = list (handler .parse (path ))
257- assert len (deps ) == 1 , handler
258- if not deps :
259- continue
251+ assert len (deps ) == 1
252+
260253 dependencies = deps [0 ].dependencies
261254 for dep in dependencies :
262255 if not dep .purl :
263256 continue
264257
258+ if dep .scope != "install" :
259+ continue
260+
265261 dep_purl = PackageURL .from_string (dep .purl )
266- if not (
267- dep .scope == "install"
268- and (
269- not (dep .is_resolved )
270- or (dep .is_resolved and dep_purl .name not in self .resolved_requirements )
271- )
272- ):
262+
263+ if self .is_dep_resolved_and_in_resolved_requirements (dep , dep_purl ):
273264 continue
265+
274266 if dep .is_resolved :
275- self .resolved_requirements = ( * self . resolved_requirements , dep_purl )
267+ self .resolved_requirements . append ( dep_purl )
276268 # skip the requirement starting with -- like
277269 # --editable, --requirement
278270 if not dep .extracted_requirement .startswith ("--" ):
279271 yield packaging .requirements .Requirement (str (dep .extracted_requirement ))
280272
273+ def is_dep_resolved_and_in_resolved_requirements (self , dep , dep_purl ):
274+ return dep .is_resolved and dep_purl .name in self .resolved_requirements
275+
281276 def get_requirements_for_package_from_pypi_json_api (self , purl ):
282277 """
283278 Return requirements for a package from the PyPI.org JSON API
@@ -460,7 +455,7 @@ def get_resolved_dependencies(
460455 ]
461456 resolver = Resolver (
462457 provider = PythonInputProvider (
463- environment = environment , repos = repos , resolved_requirements = tuple ( resolved_requirements )
458+ environment = environment , repos = repos , resolved_requirements = resolved_requirements
464459 ),
465460 reporter = BaseReporter (),
466461 )
0 commit comments