Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue withpep517 packages #495

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions komodo/check_unused_package.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python

import argparse
import os
import sys
from typing import Dict

import yaml

from komodo.prettier import load_yaml
from komodo.yaml_file_types import ReleaseFile, RepositoryFile
Expand All @@ -11,7 +13,10 @@


def check_for_unused_package(
release_file: ReleaseFile, package_status_file: str, repository: RepositoryFile
release_file: ReleaseFile,
package_status_file: str,
repository: RepositoryFile,
builtin_python_versions: Dict[str, str],
):
package_status = load_yaml(package_status_file)
public_and_plugin_packages = [
Expand All @@ -20,9 +25,11 @@ def check_for_unused_package(
if package_status[pkg]["visibility"] in ("public", "private-plugin")
]
python_version = release_file.content["python"]
# For pypi we need to change '3.8.6-builtin' -> '3.8.6'
python_version = python_version[: python_version.rindex("-")]
dependencies = PypiDependencies(release_file.content, python_version=python_version)
full_python_version = builtin_python_versions[python_version]

dependencies = PypiDependencies(
release_file.content, python_version=full_python_version
)
for name, version in release_file.content.items():
metadata = repository.content.get(name, {}).get(version, {})
if metadata.get("source") != "pypi":
Expand Down Expand Up @@ -69,7 +76,11 @@ def main():
)

args = parser.parse_args()
check_for_unused_package(args.release_file, args.status_file, args.repo)
with open("builtin_python_versions.yml", "r", encoding="utf-8") as f:
builtin_python_versions = yaml.safe_load(f)
check_for_unused_package(
args.release_file, args.status_file, args.repo, builtin_python_versions
)


if __name__ == "__main__":
Expand Down
8 changes: 5 additions & 3 deletions komodo/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from collections import namedtuple

import yaml
from packaging.version import parse

from .pypi_dependencies import PypiDependencies
Expand Down Expand Up @@ -101,10 +102,11 @@ def lint(
}

python_version = release_file.content["python"]
# For pypi we need to change '3.8.6-builtin' -> '3.8.6'
python_version = python_version[: python_version.rindex("-")]
with open("builtin_python_versions.yml", "r", encoding="utf-8") as f:
full_python_version = yaml.safe_load(f)[python_version]

dependencies = PypiDependencies(
pypi_dependencies, python_version=python_version
pypi_dependencies, python_version=full_python_version
)
for name, version in release_file.content.items():
if (
Expand Down
28 changes: 24 additions & 4 deletions komodo/pypi_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ def dump_cache(self):
def _get_requirements(
self, package_name: str, package_version: str
) -> List[Requirement]:
"""
>>> from packaging.requirements import Requirement
>>> PypiDependencies(to_install={}, python_version="3.8.11")._get_requirements(
... "rips",
... "2024.3.0.2"
... )
[<Requirement('grpcio')>, <Requirement('protobuf')>, <Requirement('wheel')>]
"""
canonical = canonicalize_name(package_name)
if canonical in self._user_specified:
return self._user_specified[canonical]
Expand Down Expand Up @@ -161,10 +169,22 @@ def _get_requirements(
f"Got: {files}"
)
file = files[0]
if file.endswith(".whl"):
dist = pkginfo.Wheel(os.path.join(tmpdir, file))
else:
dist = pkginfo.SDist(os.path.join(tmpdir, file))
if not file.endswith(".whl"):
subprocess.check_output(
[
"pip",
"wheel",
"--use-pep517",
"--no-verify",
"--no-deps",
"--disable-pip-version-check",
"--no-python-version-warning",
file,
],
cwd=tmpdir,
)
file = [f for f in os.listdir(tmpdir) if f.endswith(".whl")][0]
dist = pkginfo.Wheel(os.path.join(tmpdir, file))
self.requirements[canonical][package_version] = dist.requires_dist

return [Requirement(r) for r in self.requirements[canonical][package_version]]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_check_unused_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
@pytest.mark.parametrize("repo, release, package_status", test_case)
def test_check_unused_package(repo, release, package_status, capsys, tmpdir):
package_status["python"] = {"visibility": "public"}
release["python"] = "3.8.6-builtin"
release["python"] = "3.8-builtin"

# Use tmpdir to create a temporary file for package status
package_status_file = tmpdir.join("package_status.yml")
Expand All @@ -97,6 +97,7 @@ def test_check_unused_package(repo, release, package_status, capsys, tmpdir):
release_file=release,
package_status_file=str(package_status_file),
repository=repo,
builtin_python_versions={"3.8-builtin": "3.8.6"},
)
assert sys_exit_info.value.code == 1
captured = capsys.readouterr()
Expand Down