Skip to content

Commit

Permalink
Continue using asyncio.
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Neidhart <[email protected]>
  • Loading branch information
netomi committed Nov 30, 2023
1 parent 47a3de4 commit 43aa0c5
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 158 deletions.
49 changes: 39 additions & 10 deletions src/python_inspector/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,21 @@ def resolve_dependencies(
pdt_output=pdt_output,
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
ignore_errors=ignore_errors,
verbose=verbose,
printer=printer
)

async def gather_pypi_data():
async def get_pypi_data(package):
if verbose:
printer(f" package '{package}'")

return await get_pypi_data_from_purl(
data = await get_pypi_data_from_purl(
package, repos=repos, environment=environment, prefer_source=prefer_source
)

if verbose:
printer(f" retrieved package '{package}'")

return data

if verbose:
printer(f"retrieve data from pypi:")

Expand Down Expand Up @@ -324,6 +328,8 @@ def resolve(
pdt_output: bool = False,
analyze_setup_py_insecurely: bool = False,
ignore_errors: bool = False,
verbose: bool = False,
printer=print
):
"""
Resolve dependencies given a ``direct_dependencies`` list of
Expand All @@ -350,6 +356,8 @@ def resolve(
pdt_output=pdt_output,
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
ignore_errors=ignore_errors,
verbose=verbose,
printer=printer
)

return resolved_dependencies, packages
Expand All @@ -364,6 +372,8 @@ def get_resolved_dependencies(
pdt_output: bool = False,
analyze_setup_py_insecurely: bool = False,
ignore_errors: bool = False,
verbose: bool = False,
printer=print
) -> Tuple[List[Dict], List[str]]:
"""
Return resolved dependencies of a ``requirements`` list of Requirement for
Expand All @@ -373,15 +383,34 @@ def get_resolved_dependencies(
Used the provided ``repos`` list of PypiSimpleRepository.
If empty, use instead the PyPI.org JSON API exclusively instead.
"""
provider = PythonInputProvider(
environment=environment,
repos=repos,
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
ignore_errors=ignore_errors,
)

async def gather_version_data():
async def get_version_data(name: str):
versions = await provider.fill_versions_for_package(name)

if verbose:
printer(f" retrieved versions for package '{name}'")

return versions

if verbose:
printer(f"versions:")

return await asyncio.gather(*[get_version_data(requirement.name) for requirement in requirements])

asyncio.run(gather_version_data())

resolver = Resolver(
provider=PythonInputProvider(
environment=environment,
repos=repos,
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
ignore_errors=ignore_errors,
),
provider=provider,
reporter=BaseReporter(),
)

resolver_results = resolver.resolve(requirements=requirements, max_rounds=max_rounds)
package_list = get_package_list(results=resolver_results)
if pdt_output:
Expand Down
36 changes: 20 additions & 16 deletions src/python_inspector/package_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ async def get_pypi_data_from_purl(
python_version = get_python_version_from_env_tag(python_version=environment.python_version)

valid_distribution_urls = []
sdist_url = get_sdist_download_url(purl=parsed_purl, repos=repos, python_version=python_version)
sdist_url = await get_sdist_download_url(purl=parsed_purl, repos=repos, python_version=python_version)
if sdist_url:
valid_distribution_urls.append(sdist_url)

# if prefer_source is True then only source distribution is used
# in case of no source distribution available then wheel is used
if not valid_distribution_urls or not prefer_source:
wheel_urls = list(
get_wheel_download_urls(
purl=parsed_purl,
repos=repos,
environment=environment,
python_version=python_version,
)
)
wheel_urls = \
[
item
for item in await get_wheel_download_urls(
purl=parsed_purl,
repos=repos,
environment=environment,
python_version=python_version,
)
]
wheel_url = choose_single_wheel(wheel_urls)
if wheel_url:
valid_distribution_urls.insert(0, wheel_url)
Expand Down Expand Up @@ -142,38 +144,40 @@ def get_pypi_codeview_url(project_urls: Dict) -> Optional[str]:
return code_view_url


def get_wheel_download_urls(
async def get_wheel_download_urls(
purl: PackageURL,
repos: List[PypiSimpleRepository],
environment: Environment,
python_version: str,
) -> Iterable[str]:
) -> List[str]:
"""
Return a list of download urls for the given purl.
"""
download_urls = []
for repo in repos:
for wheel in utils_pypi.get_supported_and_valid_wheels(
for wheel in await utils_pypi.get_supported_and_valid_wheels(
repo=repo,
name=purl.name,
version=purl.version,
environment=environment,
python_version=python_version,
):
yield wheel.download_url
download_urls.append(await wheel.download_url)
return download_urls


def get_sdist_download_url(
async def get_sdist_download_url(
purl: PackageURL, repos: List[PypiSimpleRepository], python_version: str
) -> str:
"""
Return a list of download urls for the given purl.
"""
for repo in repos:
sdist = utils_pypi.get_valid_sdist(
sdist = await utils_pypi.get_valid_sdist(
repo=repo,
name=purl.name,
version=purl.version,
python_version=python_version,
)
if sdist:
return sdist.download_url
return await sdist.download_url
Loading

0 comments on commit 43aa0c5

Please sign in to comment.