Skip to content
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
4 changes: 4 additions & 0 deletions ecosystem/cli/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def update_member_data(
dao = DAO(path=resources_dir)
for member in dao.get_all(member_id):
print(f"\n::group:: {member.name}️ ({member.name_id})")
if member.status == "Alumni":
print('member.status == "Alumni", so skip')
print("::endgroup::")
continue
for update_method_str in to_update:
print(f"Updating {update_method_str}️")
update_method = getattr(member, f"update_{update_method_str}")
Expand Down
83 changes: 75 additions & 8 deletions ecosystem/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .request import request_json


class PyPIData(JsonSerializable):
class PyPIData(JsonSerializable): # pylint: disable=too-many-public-methods
"""
The PyPI data related to a project
"""
Expand All @@ -43,6 +43,8 @@ class PyPIData(JsonSerializable):
"description",
"url",
"development_status",
"status",
"maintainers",
"requires_qiskit",
"compatible_with_qiskit_v1",
"compatible_with_qiskit_v2",
Expand All @@ -54,7 +56,6 @@ class PyPIData(JsonSerializable):
aliases = {
"version": "info.version",
"url": "info.package_url",
"license": "info.license_expression",
"description": "info.summary",
"development_status": "info.classifiers[?('Development Status' in @)]",
}
Expand All @@ -68,6 +69,7 @@ def __init__(self, package_name: str, **kwargs):
self.package_name = canonicalize_name(package_name, validate=True)
self._kwargs = kwargs or {}
self._pypi_json = None
self._pypi_simple_json = None
self._pypistats_json = None
self._all_qiskit_versions = None

Expand Down Expand Up @@ -103,15 +105,33 @@ def from_url(cls, pypi_project_url):

def update_json(self):
"""
Fetches remote json data from https://pypi.org/pypi/{self.package_name}/json
Fetches remote jsons data from:
- https://pypi.org/pypi/{self.package_name}/json
- https://pypi.org/simple/{self.package_name}/
- https://pypistats.org/api/packages/{self.package_name}/
"""
try:
self._pypi_json = request_json(f"pypi.org/pypi/{self.package_name}/json")
except EcosystemError:
pass
self._pypi_json = self.request_pypi()
self._pypi_simple_json = self.request_pypi_simple()
if self._pypistats_json is None:
self._pypistats_json = self.request_pypistats()

def request_pypi(self):
"""Fetches https://pypi.org/pypi/{self.package_name}/json"""
try:
return request_json(f"pypi.org/pypi/{self.package_name}/json")
except EcosystemError:
return None

def request_pypi_simple(self):
"""Fetches https://pypi.org/simple/{self.package_name}/"""
try:
return request_json(
f"https://pypi.org/simple/{self.package_name}/",
headers={"Accept": "application/vnd.pypi.simple.v1+json"},
)
except EcosystemError:
return None

def __getattr__(self, item):
if self._pypi_json:
if item in PyPIData.aliases:
Expand Down Expand Up @@ -167,7 +187,10 @@ def requires_qiskit(self):
if requirement.name == "qiskit":
if len(requirement.specifier):
self._kwargs["requires_qiskit"] = str(requirement.specifier)
else:
elif (
"requires_qiskit" not in self._kwargs
or self._kwargs["requires_qiskit"] != ">=0"
):
logger.warning(
"%s depends on qiskit but with empty specifier. "
'Forcing one, ">=0"',
Expand Down Expand Up @@ -333,3 +356,47 @@ def last_180_days_downloads(self):
"without_mirrors"
)
return self._kwargs.get("last_180_days_downloads")

@property
def license(self):
"""Package license"""
if self._pypi_json:
info_license = self._pypi_json.get("info", {}).get("license")
if info_license and len(info_license) < 50:
return info_license
info_license_expression = self._pypi_json.get("info", {}).get(
"license_expression"
)
if info_license_expression:
return info_license_expression
for classifier in self._pypi_json.get("info", {}).get("classifiers"):
if classifier.startswith("License :: "):
parts = [part.strip() for part in classifier.split("::")]
if len(parts) == 3:
return parts[2]
continue
return self._kwargs.get("license")

@property
def maintainers(self):
"""Package maintainers (or owners)"""
maintainers = []
if self._pypi_json:
ownership = self._pypi_json.get("ownership")
organization = ownership.get("organization")
if organization:
maintainers.append(f"https://pypi.org/org/{organization}/")
maintainers += [
f"https://pypi.org/user/{u['user']}/"
for u in ownership["roles"]
if u["role"] == "Owner"
]
return maintainers or self._kwargs.get("maintainers")

@property
def status(self):
"""Project status"""
project_status = None
if self._pypi_simple_json:
return self._pypi_simple_json.get("project-status", {}).get("status")
return project_status or self._kwargs.get("status")
18 changes: 13 additions & 5 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,26 @@ def test_from_url_rejects_invalid_pypi_urls(self):
def test_update_json_fetches_pypi_and_pypistats_data(self):
"""update_json stores PyPI and stats payloads."""
pypi_payload = {"info": {"version": "1.2.3"}}
pypi_simple_payload = {"project-status": {"status": "active"}}
stats_payload = {"recent_downloads": {"last_month": 10}}
pypi_data = PyPIData("banana-compiler")

with patch("ecosystem.pypi.request_json", return_value=pypi_payload) as request:
with patch.object(
PyPIData, "request_pypi", return_value=pypi_payload
) as request_pypi:
with patch.object(
PyPIData, "request_pypistats", return_value=stats_payload
) as request_stats:
pypi_data.update_json()
PyPIData, "request_pypi_simple", return_value=pypi_simple_payload
) as request_simple:
with patch.object(
PyPIData, "request_pypistats", return_value=stats_payload
) as request_stats:
pypi_data.update_json()

request.assert_called_once_with("pypi.org/pypi/banana-compiler/json")
request_pypi.assert_called_once_with()
request_simple.assert_called_once_with()
request_stats.assert_called_once_with()
self.assertEqual(pypi_payload, pypi_data.pypi_json)
self.assertEqual("active", pypi_data.status)
self.assertEqual(10, pypi_data.last_month_downloads)

def test_update_json_ignores_pypi_fetch_errors(self):
Expand Down