Skip to content

Commit

Permalink
Issue #50: Internal (not in pypi) packages not ignored, even when ins…
Browse files Browse the repository at this point in the history
…tructed to
  • Loading branch information
Nekmo committed Oct 28, 2023
1 parent e203743 commit b9fe9e5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pip_rating/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def get_global_rating_score(self):
final_global_rating_score = None
packages = dict(self.get_packages()).values()
for package in packages:
if package.rating is None:
continue
global_rating_score = package.rating.get_global_rating_score()
if final_global_rating_score is None:
final_global_rating_score = global_rating_score
Expand Down
6 changes: 6 additions & 0 deletions pip_rating/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def __init__(self, directory: str):
super().__init__(f"Missing requirements file in {directory}")


class RequirementsRatingMissingPackage(RequirementsRatingError):
def __init__(self, package_name: str):
self.package_name = package_name
super().__init__(f"Missing package {package_name}")


def catch(fn):
def wrap(*args, **kwargs):
console = Console(stderr=True)
Expand Down
8 changes: 6 additions & 2 deletions pip_rating/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from anytree import Node

from pip_rating.exceptions import RequirementsRatingMissingPackage
from pip_rating.rating import PackageRating, PackageRatingJson
from pip_rating.sources.audit import Audit, Vulnerability
from pip_rating.sources.pypi import Pypi
Expand Down Expand Up @@ -61,8 +62,11 @@ def get_audit(self, node: Node) -> "Audit":
return Audit(self.name, node.version)

@cached_property
def rating(self) -> "PackageRating":
return PackageRating(self)
def rating(self) -> Optional["PackageRating"]:
try:
return PackageRating(self)
except RequirementsRatingMissingPackage:
return None

def get_node_from_parent(
self, from_package: Optional["Package"] = None
Expand Down
9 changes: 7 additions & 2 deletions pip_rating/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"D": os.environ.get("PIP_RATING_BADGE_D_COLOR") or "#FFAF00",
"E": os.environ.get("PIP_RATING_BADGE_E_COLOR") or "#FF5F00",
"F": os.environ.get("PIP_RATING_BADGE_F_COLOR") or "#E05D44",
"?": os.environ.get("PIP_RATING_BADGE_F_COLOR") or "#262424",
}
PIP_RATING_BADGES = {
"flat": BADGE_FLAT_SVG,
Expand Down Expand Up @@ -126,8 +127,10 @@ def colorize_score(score: Union["ScoreBase", int]) -> str:
return f"[bold bright_black]{score}[/bold bright_black]"


def colorize_rating(score: Union["ScoreBase", int]) -> "RatingLetter":
def colorize_rating(score: Union["ScoreBase", int, None]) -> "RatingLetter":
"""Colorize the rating."""
if score is None:
return RatingLetter("?", 0, "bright_black")
for rating_letter in RATING_LETTERS:
if max(0, int(score)) >= rating_letter.score:
return rating_letter
Expand All @@ -137,6 +140,8 @@ def colorize_rating_package(
package: "Package", parent_package: Optional["Package"] = None
) -> str:
"""Colorize the rating of the package."""
if package.rating is None:
return "[bold bright_black]?[/bold bright_black]"
colorized_rating = colorize_rating(package.rating.get_rating_score(parent_package))
colorized_global_rating = colorize_rating(
package.rating.get_global_rating_score(parent_package)
Expand Down Expand Up @@ -306,7 +311,7 @@ def show_results(self, dependencies: "Dependencies", format_name: str = "text"):
def show_packages_results(self, dependencies: "Dependencies"):
global_rating_score = self.get_global_rating_score(dependencies)
for package in dependencies.packages.values():
if package.name not in dependencies.req_file:
if package.name not in dependencies.req_file or not package.rating:
continue
package_global_rating_score = package.rating.get_global_rating_score()
package_global_rating_score_letter = colorize_rating(
Expand Down
8 changes: 7 additions & 1 deletion pip_rating/sources/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import requests

from pip_rating.exceptions import RequirementsRatingMissingPackage
from pip_rating.sources.base import SourceBase

URL = "https://pypi.org/pypi/{package_name}/json"
Expand Down Expand Up @@ -114,5 +115,10 @@ def first_upload_iso_dt(self) -> Optional[str]:

def get_package(self) -> PypiPackage:
with requests.get(URL.format(package_name=self.package_name)) as response:
response.raise_for_status()
try:
response.raise_for_status()
except requests.HTTPError as e:
if e.response is not None and e.response.status_code == 404:
raise RequirementsRatingMissingPackage(self.package_name)
raise e
return response.json()

0 comments on commit b9fe9e5

Please sign in to comment.