Skip to content

Commit

Permalink
Track the change of the recent release. (#249)
Browse files Browse the repository at this point in the history
get the release date from PYPI as the initial seed. And get the release date for other packages by tracking the change of the version between the last committed JSON file and the file to be constructed.

Co-authored-by: Leopold Talirz <[email protected]>
  • Loading branch information
AhmedBasem20 and ltalirz authored Aug 16, 2023
1 parent 6ddb7ce commit d19a075
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
46 changes: 46 additions & 0 deletions aiida_registry/fetch_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Data is primarily sourced from PyPI,
with a fallback to the repository build file (setup.json, setup.cfg, pyproject.toml).
"""
import json

# pylint: disable=consider-using-f-string
import os
import re
Expand All @@ -13,6 +15,7 @@
import urllib
from collections import OrderedDict
from datetime import datetime, timedelta
from functools import lru_cache
from typing import Optional

import requests
Expand All @@ -32,6 +35,39 @@
GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]


@lru_cache(maxsize=None)
def load_plugins_metadata(json_file_path):
"""Load the plugins file."""
try:
with open(json_file_path, "r", encoding="utf-8") as json_file:
return json.load(json_file)
except FileNotFoundError:
print(f"Error: The file '{json_file_path}' was not found.")
return None


def get_last_fetched_version(plugin_name):
"""
Get the last fetched version of the plugin.
Args:
plugin_name (str): Name of the plugin.
Returns:
str or None: Version of the plugin if available, or None if not found.
"""
json_file_path = "./aiida-registry-app/src/plugins_metadata.json"
metadata = load_plugins_metadata(json_file_path)

if metadata is not None:
try:
return metadata["plugins"][plugin_name]["metadata"]["version"]
except KeyError:
print("No version for the plugin")

return None


def get_hosted_on(url):
"""Get the hosting service from a URL."""
try:
Expand Down Expand Up @@ -192,6 +228,16 @@ def complete_plugin_data(
plugin_data["aiida_version"] = data.aiida_version
plugin_data["entry_points"] = data.entry_points

current_version = get_last_fetched_version(plugin_data["name"])
if current_version:
try:
if current_version != plugin_data["metadata"]["version"]:
plugin_data["metadata"]["release_date"] = datetime.today().strftime(
"%Y-%m-%d"
)
except KeyError:
print("no version for the plugin")

# ensure entry points are not None
plugin_data["entry_points"] = plugin_data.get("entry_points") or {}

Expand Down
9 changes: 9 additions & 0 deletions aiida_registry/parse_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import tempfile
import zipfile
from datetime import datetime
from pathlib import Path
from typing import NamedTuple, Optional

Expand Down Expand Up @@ -44,6 +45,14 @@ def get_pypi_metadata(package_name: str, parse_wheel=True) -> Optional[PypiData]
# get data from pypi JSON
pypi_info_data = pypi_data.get("info", {})

# Get the recent release date and convert it to YYYY-MM-DD format.
version = pypi_info_data["version"]
latest_version_release_date = pypi_data["releases"][version][0]["upload_time"]
release_date_format = "%Y-%m-%dT%H:%M:%S"
desired_date_format = "%Y-%m-%d"
original_date = datetime.strptime(latest_version_release_date, release_date_format)
desired_date_string = original_date.strftime(desired_date_format)
metadata["release_date"] = desired_date_string
# add required metadata
for key_from, key_to in (
("summary", "description"),
Expand Down

0 comments on commit d19a075

Please sign in to comment.