-
Notifications
You must be signed in to change notification settings - Fork 0
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
[ADD] add 'OCA/repository' as category when sorting addons coming from OCA #15
Draft
AnizR
wants to merge
1
commit into
master
Choose a base branch
from
zra-add-sort-oca-repo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ | |||||
# SPDX-License-Identifier: MIT | ||||||
|
||||||
from pathlib import Path | ||||||
from re import DOTALL, sub | ||||||
from re import DOTALL, search, sub | ||||||
|
||||||
import click | ||||||
from click import command, option | ||||||
|
@@ -12,13 +12,25 @@ | |||||
from manifestoo_core.core_addons import is_core_ce_addon, is_core_ee_addon | ||||||
from manifestoo_core.metadata import addon_name_to_distribution_name | ||||||
from manifestoo_core.odoo_series import OdooSeries | ||||||
from mousebender import simple | ||||||
from packaging.specifiers import SpecifierSet | ||||||
from packaging.utils import parse_wheel_filename | ||||||
from platformdirs import user_cache_dir | ||||||
from requests import head | ||||||
from requests import get, head | ||||||
|
||||||
NAME_DEFAULT_CATEGORY = "Default" | ||||||
OCA_ADDONS_INDEX_URL = "https://wheelhouse.odoo-community.org/oca-simple/" | ||||||
REQUEST_TIMEOUT = 2 # s | ||||||
|
||||||
PYPI_SIMPLE_INDEX_URL = "https://pypi.org/simple/" | ||||||
PAGE_NOT_FOUND = 404 | ||||||
|
||||||
|
||||||
def _get_pypi_url(distribution_name: str, version: str) -> str: | ||||||
return f"https://pypi.org/pypi/{distribution_name}/{version}/json" | ||||||
|
||||||
|
||||||
oca_category_repo_not_found = "OCA" | ||||||
other_addons_category_cache = Cache(user_cache_dir("odoo-sort-manifest-depends", "Acsone")) | ||||||
|
||||||
|
||||||
|
@@ -42,8 +54,8 @@ def _get_addons_by_name(addons_dir: Path) -> dict[str, Addon]: | |||||
return local_addons | ||||||
|
||||||
|
||||||
def _identify_oca_addons(addon_names: list[str], odoo_series: OdooSeries) -> tuple[list[str], list[str]]: | ||||||
oca_addons, other_addons = [], [] | ||||||
def _identify_oca_addons(addon_names: list[str], odoo_series: OdooSeries) -> tuple[dict[str, list[str]], list[str]]: | ||||||
oca_addons_by_category, other_addons = {}, [] | ||||||
|
||||||
with other_addons_category_cache as cache: | ||||||
for addon_name in addon_names: | ||||||
|
@@ -53,17 +65,65 @@ def _identify_oca_addons(addon_names: list[str], odoo_series: OdooSeries) -> tup | |||||
distribution_name = addon_name_to_distribution_name(addon_name, odoo_series).replace("_", "-") | ||||||
res = head(f"{OCA_ADDONS_INDEX_URL}{distribution_name}", timeout=REQUEST_TIMEOUT) | ||||||
if res: | ||||||
category = "oca" | ||||||
category = get_oca_repository_name(addon_name, odoo_series) or oca_category_repo_not_found | ||||||
else: | ||||||
category = "other" | ||||||
cache[addon_name] = category | ||||||
|
||||||
if category == "oca": | ||||||
oca_addons.append(addon_name) | ||||||
else: | ||||||
if category == "other": | ||||||
other_addons.append(addon_name) | ||||||
else: | ||||||
oca_addons_by_category.setdefault(category, []).append(addon_name) | ||||||
|
||||||
return oca_addons_by_category, other_addons | ||||||
|
||||||
|
||||||
def get_oca_repository_name(addon_name: str, odoo_series: OdooSeries) -> str | None: | ||||||
specifier = SpecifierSet(f"=={odoo_series.value}.*") | ||||||
distribution_name = addon_name_to_distribution_name(addon_name, odoo_series) | ||||||
# get avaialble releases | ||||||
project_url = simple.create_project_url(PYPI_SIMPLE_INDEX_URL, distribution_name) | ||||||
response = get(project_url, headers={"Accept": simple.ACCEPT_JSON_V1}, timeout=REQUEST_TIMEOUT) | ||||||
if response.status_code == PAGE_NOT_FOUND: | ||||||
# project not found | ||||||
return None | ||||||
response.raise_for_status() | ||||||
data = response.text | ||||||
content_type = response.headers["Content-Type"] | ||||||
project_details = simple.parse_project_details(data, content_type, distribution_name) | ||||||
# find the first version that matches the requested Odoo version; | ||||||
# we assume all releases come from the same repo for a given Odoo series | ||||||
for file in project_details["files"]: | ||||||
if file.get("yanked"): | ||||||
continue | ||||||
filename = file["filename"] | ||||||
if not filename.endswith(".whl"): | ||||||
continue | ||||||
_, version, _, _ = parse_wheel_filename(filename) | ||||||
if specifier.contains(version, prereleases=True): | ||||||
# found a release that matches the requested Odoo version | ||||||
break | ||||||
else: | ||||||
# no release found that matches the requested Odoo version | ||||||
return None | ||||||
|
||||||
pypi_json_url = _get_pypi_url(distribution_name, version) | ||||||
response = get(pypi_json_url, timeout=REQUEST_TIMEOUT) | ||||||
response.raise_for_status() | ||||||
repo_url = response.json().get("info").get("home_page") | ||||||
|
||||||
if not repo_url: | ||||||
return None | ||||||
return search("OCA/.*", repo_url).group() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
When the function name is generic like that, I find it easier to have the module name here. I actually had to scroll up to understand what Also take care that re.search can return None. |
||||||
|
||||||
|
||||||
return oca_addons, other_addons | ||||||
def _add_oca_categories( | ||||||
categories: dict[str, list[str]], other: list[str], odoo_series: OdooSeries | ||||||
) -> tuple[dict[str, list[str]], list[str]]: | ||||||
oca_addons_by_category, other = _identify_oca_addons(other, odoo_series) | ||||||
for category, oca_addons in {key: sorted(value) for key, value in sorted(oca_addons_by_category.items())}.items(): | ||||||
categories[category] = oca_addons | ||||||
return categories, other | ||||||
|
||||||
|
||||||
def do_sorting(addons_dir: Path, odoo_version: str, project_name: str, *, oca_category: bool) -> None: | ||||||
|
@@ -121,10 +181,8 @@ def do_sorting(addons_dir: Path, odoo_version: str, project_name: str, *, oca_ca | |||||
"Odoo Enterprise": odoo_ee, | ||||||
} | ||||||
|
||||||
# Third party | ||||||
if oca_category: | ||||||
oca, other = _identify_oca_addons(other, odoo_series) | ||||||
categories["OCA"] = oca | ||||||
categories, other = _add_oca_categories(categories, other, odoo_series) | ||||||
|
||||||
categories["Third-party"] = other | ||||||
|
||||||
|
@@ -164,7 +222,9 @@ def do_sorting(addons_dir: Path, odoo_version: str, project_name: str, *, oca_ca | |||||
@option( | ||||||
"--oca-category", | ||||||
is_flag=True, | ||||||
help="Add category for third party addons coming from OCA", | ||||||
help="Add category for third party addons coming from OCA. " | ||||||
"The category is set as 'OCA/repository_name'. " | ||||||
"If the repository can not be identified, it falls into the default 'OCA' category.", | ||||||
) | ||||||
@option( | ||||||
"--reset-cache", | ||||||
|
@@ -181,5 +241,11 @@ def sort_manifest_deps( | |||||
) -> None: | ||||||
if reset_cache: | ||||||
other_addons_category_cache.clear() | ||||||
elif other_addons_category_cache: | ||||||
# Remove addons from cache that have 'oca_category_repo_not_found' as category | ||||||
with other_addons_category_cache as cache: | ||||||
# 'oca' is for retrocompatibility with cache created in versions < v1.4 | ||||||
cache.evict(oca_category_repo_not_found) | ||||||
cache.evict("oca") | ||||||
|
||||||
do_sorting(Path(local_addons_dir), odoo_version, project_name, oca_category=oca_category) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we could have the grouping by OCA repo optional, as I personally prefer to have a single category for OCA.