Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant check for "active" plugins on npe2api #1335

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 21 additions & 7 deletions data-workflows/plugin/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,39 @@ def _is_new_plugin(plugin_version_pair):

futures.wait(update_futures, return_when="ALL_COMPLETED")

def _mark_version_stale(name, version) -> None:
logger.info(f"Updating old plugin={name} version={version}")
put_plugin_metadata(
plugin=name,
version=version,
plugin_metadata_type=PluginMetadataType.PYPI,
)

# update for removed plugins and existing older version of plugins
for name, version in dynamo_latest_plugins.items():
if name != (normalized := pypi_adapter.normalize(name)):
logger.info(
f"Found non-normalized name, will be removed plugin={name} "
f"(normalized={normalized})"
)
_mark_version_stale(name, version)
continue

pypi_plugin_version = pypi_latest_plugins.get(name)
if pypi_plugin_version == version:
continue

if pypi_plugin_version is None and is_plugin_active(name, version):
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
logger.info(
f"Skipping marking plugin={name} version={version} stale as the "
f"plugin is still active in npe2api"
"plugin is still active in npe2api"
)
continue

logger.info(f"Updating old plugin={name} version={version}")
put_plugin_metadata(
plugin=name,
version=version,
plugin_metadata_type=PluginMetadataType.PYPI,
)
_mark_version_stale(name, version)

if pypi_plugin_version is None:
logger.info(f"Plugin={name} will be removed from hub")
zulip.plugin_no_longer_on_hub(name)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def setup_method(self, monkeypatch):

def _mocked_requests_get(self, *args, **kwargs):
if args[0] == "https://api.napari.org/api/plugins":
return MockResponse(content=json.dumps({name: version for name, version in plugins()}))
return MockResponse(
content=json.dumps({name: version for name, version in plugins()})
)
elif args[0] == "https://pypi.org/pypi/napari-demo/json":
return MockResponse(content=valid_pypi_data())
elif args[0] == "https://pypi.org/pypi/default-demo/json":
Expand All @@ -149,7 +151,9 @@ def _mocked_requests_get(self, *args, **kwargs):
return MockResponse(status_code=requests.codes.not_found)

def test_get_all_plugins(self):
expected = {plugin[0]: plugin[1] for plugin in plugins()}
expected = {
pypi_adapter.normalize(plugin[0]): plugin[1] for plugin in plugins()
}
assert expected == pypi_adapter.get_all_plugins()

@pytest.mark.parametrize(
Expand Down
13 changes: 12 additions & 1 deletion napari-hub-commons/src/nhcommons/utils/pypi_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@
logger = logging.getLogger(__name__)


def normalize(name: str) -> str:
"""
Normalize the plugin name to lowercase and replace special characters with hyphen.

https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
"""
return re.sub(r"[-_.]+", "-", name).lower()


def get_all_plugins() -> Dict[str, str]:
"""
Query npe2api to get all plugins.

Now we use the npe2api to get the list of plugins, which uses the public BigQuery pypi metadata
as a source of truth.

This also normalizes the plugins names to match PyPI.

The previous implementation was broken by anti-scraping changes to PyPI.
:returns: all plugin names and latest version
"""
logger.info("Getting all napari plugins from npe2api")
packages = get_request(_NPE2API_URL + "/plugins").json()
logger.info(f"Total number of napari plugins fetched={len(packages)}")
return packages
return {normalize(name): version for name, version in packages.items()}


def get_plugin_pypi_metadata(plugin: str, version: str) -> Dict[str, Any]:
Expand Down
Loading