Skip to content

Commit

Permalink
Merge pull request #6 from openzim/enhance_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 authored Feb 5, 2024
2 parents b3a821f + 631f953 commit 32bf990
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 36 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ dynamic = ["authors", "classifiers", "keywords", "license", "version", "urls"]
additional-keywords = ["awesome"] # some additional keywords
kind = "scraper" # indicate this is a scraper, so that additional keywords are added

# Additional author #1
[[tool.hatch.metadata.hooks.openzim-metadata.additional-authors]]
name="Bob"
email="[email protected]"

# Additional author #2
[[tool.hatch.metadata.hooks.openzim-metadata.additional-authors]]
name="Alice"
email="[email protected]"

# Enable the hatch-openzim build hook to install files (e.g. JS libs) at build time.
[tool.hatch.build.hooks.openzim-build]
toml-config = "openzim.toml" # optional location of the configuration file
Expand All @@ -52,13 +62,15 @@ NOTA: the `dependencies` attribute is not specific to our hook(s), it is a gener

| Variable | Required | Description |
|---|---|---|
| `additional-authors` | N | List of authors that will be appended to the automatic one |
| `additional-keywords` | N | List of keywords that will be appended to the automatic ones |
| `kind` | N | If set to `scraper`, scrapers keywords will be automatically added as well |
| `organization` | N | Override organization (otherwise detected from Github repository to set author and keyword appropriately). Case-insentive. Supported values are `openzim`, `kiwix` and `offspot` |
| `preserve-authors` | N | Boolean indicating that we do not want to set `authors` metadata but use the ones of `pyproject.toml` |
| `preserve-classifiers` | N | Boolean indicating that we do not want to set `classifiers` metadata but use the ones of `pyproject.toml` |
| `preserve-keywords` | N | Boolean indicating that we do not want to set `keywords` metadata but use the ones of `pyproject.toml` |
| `preserve-license` | N | Boolean indicating that we do not want to set `license` metadata but use the one of `pyproject.toml` |
| `preserve-urls` | N | Boolean indicating that we do not want to set `urls` metadata but use the ones of `pyproject.toml` |
| `additional_keywords` | N | List of keywords that will be appended to the automatically added ones |
| `kind` | N | If set to `scraper`, scrapers keywords will be automatically added as well |

### Behavior

Expand Down
2 changes: 1 addition & 1 deletion src/hatch_openzim/files_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _process_execute_after(base_target_dir: Path, actions: List[str]):
logger.info(f" Executing '{action}'")
process = subprocess.run(
action,
shell=True, # noqa: S602
shell=True, # noqa: S602 # nosec: B602
cwd=base_target_dir,
text=True,
check=True,
Expand Down
22 changes: 16 additions & 6 deletions src/hatch_openzim/metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from hatch_openzim.utils import get_github_project_homepage, get_python_versions
from hatch_openzim.utils import get_github_info, get_python_versions


def update(root: str, config: dict, metadata: dict):
Expand All @@ -23,19 +23,29 @@ def update(root: str, config: dict, metadata: dict):
"openzim metadata hook."
)

github_info = get_github_info(git_config_path=Path(root) / ".git/config")

organization = config.get("organization", github_info.organization)

if not config.get("preserve-urls", False):
metadata["urls"] = {
"Donate": "https://www.kiwix.org/en/support-us/",
"Homepage": get_github_project_homepage(
git_config_path=Path(root) / ".git/config"
),
"Homepage": github_info.homepage,
}

if not config.get("preserve-authors", False):
metadata["authors"] = [{"name": "Kiwix", "email": "[email protected]"}]
if str(organization).lower() in ("kiwix", "offspot"):
authors = [{"name": "Kiwix", "email": "[email protected]"}]
else:
authors = [{"name": "openZIM", "email": "[email protected]"}]
authors.extend(config.get("additional-authors", []))
metadata["authors"] = authors

if not config.get("preserve-keywords", False):
keywords = ["kiwix"]
if str(organization).lower() in ("kiwix", "offspot"):
keywords = ["kiwix"]
else:
keywords = ["openzim"]
if config.get("kind", "") == "scraper":
keywords.extend(["zim", "offline"])
keywords.extend(config.get("additional-keywords", []))
Expand Down
12 changes: 11 additions & 1 deletion src/hatch_openzim/shared.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import logging
import os
import sys

DEFAULT_FORMAT = "%(name)s:%(levelname)s:%(message)s"

# create logger
logger = logging.getLogger("hatch_openzim")
logger.setLevel(logging.DEBUG)

# setup console logging
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter(DEFAULT_FORMAT))
log_level = logging.getLevelName(os.getenv("HATCH_OPENZIM_LOG_LEVEL", "INFO"))
logger.setLevel(log_level)
console_handler.setLevel(log_level)
logger.addHandler(console_handler)
21 changes: 14 additions & 7 deletions src/hatch_openzim/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import configparser
import re
from collections import namedtuple
from pathlib import Path
from typing import List

Expand All @@ -13,12 +14,16 @@
r"""(?P<repository>.*?)(?:.git)?$"""
)

DEFAULT_GITHUB_PROJECT_HOMEPAGE = "https://www.kiwix.org"
GithubInfo = namedtuple("GithubInfo", ["homepage", "organization", "repository"])

DEFAULT_GITHUB_INFO = GithubInfo(
homepage="https://www.kiwix.org", organization=None, repository=None
)


def get_github_project_homepage(git_config_path: Path, remote: str = "origin") -> str:
def get_github_info(git_config_path: Path, remote: str = "origin") -> GithubInfo:
if not git_config_path.exists() or not git_config_path.is_file():
return DEFAULT_GITHUB_PROJECT_HOMEPAGE
return DEFAULT_GITHUB_INFO

try:
config = configparser.ConfigParser()
Expand All @@ -27,13 +32,15 @@ def get_github_project_homepage(git_config_path: Path, remote: str = "origin") -
match = REMOTE_REGEXP.match(git_remote_url)
if not match:
raise Exception(f"Unexpected remote url: {git_remote_url}")
return (
f"https://github.com/{match.group('organization')}/"
f"{match.group('repository')}"
return GithubInfo(
homepage=f"https://github.com/{match.group('organization')}/"
f"{match.group('repository')}",
organization=match.group("organization"),
repository=match.group("repository"),
)
except Exception as exc:
logger.error("Failed to read Github URL", exc_info=exc)
return DEFAULT_GITHUB_PROJECT_HOMEPAGE
return DEFAULT_GITHUB_INFO


def get_python_versions(requires_python: str) -> List[str]:
Expand Down
54 changes: 50 additions & 4 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def test_metadata_nominal(metadata):
metadata=metadata,
)

assert metadata["authors"] == [{"email": "dev@kiwix.org", "name": "Kiwix"}]
assert metadata["authors"] == [{"email": "dev@openzim.org", "name": "openZIM"}]
assert metadata["classifiers"] == [
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
assert metadata["keywords"] == ["kiwix"]
assert metadata["keywords"] == ["openzim"]
assert metadata["license"] == {"text": "GPL-3.0-or-later"}
assert metadata["urls"] == {
"Donate": "https://www.kiwix.org/en/support-us/",
Expand Down Expand Up @@ -126,7 +126,53 @@ def test_metadata_additional_keywords(metadata):
metadata=metadata,
)
# we compare sets because order is not relevant
assert set(metadata["keywords"]) == {"kiwix", "keyword1", "keyword2"}
assert set(metadata["keywords"]) == {"openzim", "keyword1", "keyword2"}


def test_metadata_additional_authors(metadata):
config = {}
config["additional-authors"] = [{"email": "[email protected]", "name": "Some One"}]
update(
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
config=config,
metadata=metadata,
)
# we compare sets because order is not relevant
assert metadata["authors"] == [
{"email": "[email protected]", "name": "openZIM"},
{"email": "[email protected]", "name": "Some One"},
]


@pytest.mark.parametrize(
"organization, expected_result",
[
("kiwix", "kiwix"),
("Kiwix", "kiwix"),
("openzim", "openzim"),
("openZIM", "openzim"),
("offspot", "kiwix"),
("unknown", "openzim"),
(None, "openzim"),
],
)
def test_metadata_organization(organization, expected_result, metadata):
config = {}
if organization:
config["organization"] = organization
update(
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
config=config,
metadata=metadata,
)
if expected_result == "kiwix":
assert metadata["authors"] == [{"email": "[email protected]", "name": "Kiwix"}]
assert metadata["keywords"] == ["kiwix"]
elif expected_result == "openzim":
assert metadata["authors"] == [{"email": "[email protected]", "name": "openZIM"}]
assert metadata["keywords"] == ["openzim"]
else:
raise Exception(f"Unexpected expected result: {expected_result}")


def test_metadata_is_scraper(metadata):
Expand All @@ -138,4 +184,4 @@ def test_metadata_is_scraper(metadata):
metadata=metadata,
)
# we compare sets because order is not relevant
assert set(metadata["keywords"]) == {"kiwix", "offline", "zim"}
assert set(metadata["keywords"]) == {"openzim", "offline", "zim"}
38 changes: 23 additions & 15 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from hatch_openzim.utils import get_github_project_homepage, get_python_versions
from hatch_openzim.utils import GithubInfo, get_github_info, get_python_versions


@pytest.fixture
Expand All @@ -32,46 +32,55 @@ def _mock_git_config(git_origin_url: str, remote_name: str = "origin"):


@pytest.mark.parametrize(
"git_url, expected_homepage_url",
"git_url, expected_homepage_url, expected_organization, expected_repository",
[
(
"https://github.com/oneuser/onerepo.git",
"https://github.com/oneuser/onerepo",
"oneuser",
"onerepo",
),
(
"https://github.com/oneuser/onerepo",
"https://github.com/oneuser/onerepo",
"oneuser",
"onerepo",
),
(
"[email protected]:oneuser/one-repo.git",
"https://github.com/oneuser/one-repo",
"oneuser",
"one-repo",
),
],
)
def test_get_github_project_homepage_valid_url(
mock_git_config, git_url, expected_homepage_url
mock_git_config,
git_url,
expected_homepage_url,
expected_organization,
expected_repository,
):
with mock_git_config(git_url) as git_config_path:
assert (
get_github_project_homepage(git_config_path=git_config_path)
== expected_homepage_url
assert get_github_info(git_config_path=git_config_path) == GithubInfo(
homepage=expected_homepage_url,
organization=expected_organization,
repository=expected_repository,
)


def test_get_github_project_homepage_invalid_url(mock_git_config):
# Test the function with an invalid URL
with mock_git_config("http://github.com/oneuser/onerepo.git") as git_config_path:
assert (
get_github_project_homepage(git_config_path=git_config_path)
== "https://www.kiwix.org"
assert get_github_info(git_config_path=git_config_path) == GithubInfo(
homepage="https://www.kiwix.org", organization=None, repository=None
)


def test_get_github_project_missing_git_config():
# Test the function with an invalid URL
assert (
get_github_project_homepage(git_config_path=Path("i_m_not_here.config"))
== "https://www.kiwix.org"
assert get_github_info(git_config_path=Path("i_m_not_here.config")) == GithubInfo(
homepage="https://www.kiwix.org", organization=None, repository=None
)


Expand All @@ -80,9 +89,8 @@ def test_get_github_project_homepage_invalid_remote(mock_git_config):
with mock_git_config(
"https://github.com/oneuser/onerepo.git", remote_name="origin2"
) as git_config_path:
assert (
get_github_project_homepage(git_config_path=git_config_path)
== "https://www.kiwix.org"
assert get_github_info(git_config_path=git_config_path) == GithubInfo(
homepage="https://www.kiwix.org", organization=None, repository=None
)


Expand Down

0 comments on commit 32bf990

Please sign in to comment.