Skip to content

Commit

Permalink
get plugins release date from PyPI
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedBasem20 committed Aug 10, 2023
1 parent dbc9574 commit 05f8aa4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
50 changes: 36 additions & 14 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,8 +15,8 @@
import urllib
from collections import OrderedDict
from datetime import datetime, timedelta
from functools import lru_cache
from typing import Optional
import json

import requests
import yaml
Expand All @@ -32,20 +34,38 @@

GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]

def get_last_fetched_version(plugin_name):
json_file_path = '../aiida-registry-app/src/plugins_metadata.json'

@lru_cache(maxsize=None)
def load_plugins_metadata(json_file_path):
"""Load the plugins file."""
try:
with open(json_file_path, 'r') as json_file:
current_data = json.load(json_file)
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
try:
current_version = current_data["plugins"][plugin_name]["metadata"]["version"]
return current_version

except KeyError:
print("no version for the plugin")

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
else:
return None


Expand Down Expand Up @@ -133,7 +153,7 @@ def get_git_commits_count(repo_name):


def complete_plugin_data(
plugin_data: dict, cur_data, fetch_pypi=True, fetch_pypi_wheel=True
plugin_data: dict, fetch_pypi=True, fetch_pypi_wheel=True
): # pylint: disable=too-many-branches,too-many-statements
"""Update plugin data dictionary.
Expand Down Expand Up @@ -209,11 +229,13 @@ 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'])
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("%d-%m-%Y")
plugin_data["metadata"]["release_date"] = datetime.today().strftime(
"%Y-%m-%d"
)
except KeyError:
print("no version for the plugin")

Expand Down Expand Up @@ -339,7 +361,7 @@ def fetch_metadata(filter_list=None, fetch_pypi=True, fetch_pypi_wheel=True):
plugins_raw_data: dict = yaml.safe_load(handle)

plugins_metadata = OrderedDict()

for plugin_name, plugin_data in sorted(plugins_raw_data.items()):
if filter_list and plugin_name not in filter_list:
continue
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 05f8aa4

Please sign in to comment.