From 05f8aa444cb662d00212ec5eb76b77452e797c10 Mon Sep 17 00:00:00 2001 From: AhmedBasem Date: Thu, 10 Aug 2023 21:51:00 +0300 Subject: [PATCH] get plugins release date from PyPI --- aiida_registry/fetch_metadata.py | 50 +++++++++++++++++++++++--------- aiida_registry/parse_pypi.py | 9 ++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/aiida_registry/fetch_metadata.py b/aiida_registry/fetch_metadata.py index 6a6fdd76..70fcc6c1 100644 --- a/aiida_registry/fetch_metadata.py +++ b/aiida_registry/fetch_metadata.py @@ -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 @@ -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 @@ -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 @@ -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. @@ -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") @@ -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 diff --git a/aiida_registry/parse_pypi.py b/aiida_registry/parse_pypi.py index 9d6d7634..973c313d 100644 --- a/aiida_registry/parse_pypi.py +++ b/aiida_registry/parse_pypi.py @@ -5,6 +5,7 @@ import json import tempfile import zipfile +from datetime import datetime from pathlib import Path from typing import NamedTuple, Optional @@ -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"),