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

[IMP] add '--distinct-oca' flag to have a disinction for third party addons coming from OCA #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Options:
--odoo-version TEXT Project's Odoo version (e.g. 16.0) [required]
--project-name TEXT Name of the project, will be the name of
category of local addons (default: Local)
--distinct-oca Add disctinction for third party addons coming
AnizR marked this conversation as resolved.
Show resolved Hide resolved
AnizR marked this conversation as resolved.
Show resolved Hide resolved
from OCA
AnizR marked this conversation as resolved.
Show resolved Hide resolved
--help Show this message and exit.
```

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ classifiers = [
]
dependencies = [
"click",
"manifestoo_core",
"manifestoo_core>=1.1",
"diskcache",
"requests",
]

[project.urls]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen requirements generated by pip-deepfreeze
click==8.1.7
diskcache==5.6.3
manifestoo-core==1.7
packaging==24.1
72 changes: 65 additions & 7 deletions src/odoo_sort_manifest_depends/sort_manifest_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@

import click
from click import command, option
from diskcache import Cache
from manifestoo_core.addon import Addon, is_addon_dir
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 requests import head

NAME_DEFAULT_CATEGORY = "Default"
OCA_ADDON_URL = "https://wheelhouse.odoo-community.org/oca-simple/"
REQUEST_TIMEOUT = 2 # s

other_addons_category_cache = Cache("~/.cache/odoo-sort-manifest-depends")


def _generate_depends_sections(dict_depends_by_cateogry: dict[str, list[str]]) -> str:
Expand All @@ -34,7 +41,30 @@ def _get_addons_by_name(addons_dir: Path) -> dict[str, Addon]:
return local_addons


def do_sorting(addons_dir: Path, odoo_version: str, project_name: str) -> None:
def _identify_oca_addons(addon_names: list[str], odoo_series: OdooSeries) -> tuple[list[str], list[str]]:
oca_addons, other_addons = [], []

with other_addons_category_cache as cache:
for addon_name in addon_names:
if category := cache.get(addon_name):
if category == "oca":
oca_addons.append(addon_name)
else:
other_addons.append(addon_name)
else:
distribution_name = addon_name_to_distribution_name(addon_name.replace("_", "-"), odoo_series)
res = head(f"{OCA_ADDON_URL}{distribution_name}", timeout=REQUEST_TIMEOUT)
if res:
cache[addon_name] = "oca"
oca_addons.append(addon_name)
else:
cache[addon_name] = "other"
other_addons.append(addon_name)

return oca_addons, other_addons


def do_sorting(addons_dir: Path, odoo_version: str, project_name: str, *, oca_category: bool) -> None:
"""
Update manifest files to sort dependencies by type, category and then by name.
Expand All @@ -44,7 +74,7 @@ def do_sorting(addons_dir: Path, odoo_version: str, project_name: str) -> None:
The script will also exclude not installable addons from the dependencies.
"""
odoo_version = OdooSeries(odoo_version)
odoo_series = OdooSeries(odoo_version)

local_addons = _get_addons_by_name(addons_dir)

Expand All @@ -62,9 +92,9 @@ def do_sorting(addons_dir: Path, odoo_version: str, project_name: str) -> None:
dep_addon_obj.manifest.manifest_dict.get("category", NAME_DEFAULT_CATEGORY), []
)
addons_in_category.append(dep)
elif is_core_ce_addon(dep, odoo_version):
elif is_core_ce_addon(dep, odoo_series):
odoo_ce.append(dep)
elif is_core_ee_addon(dep, odoo_version):
elif is_core_ee_addon(dep, odoo_series):
odoo_ee.append(dep)
else:
other.append(dep)
Expand All @@ -83,12 +113,20 @@ def do_sorting(addons_dir: Path, odoo_version: str, project_name: str) -> None:
else:
local_categories[f"{project_name}/{cat}"] = sorted(addon_names)

# Odoo
categories = {
"Odoo Community": odoo_ce,
"Odoo Enterprise": odoo_ee,
"Third-party": other,
}

# Third party
if oca_category:
oca, other = _identify_oca_addons(other, odoo_series)
categories["OCA"] = oca

categories["Third-party"] = other

# Local
local_categories = dict(sorted(local_categories.items()))
categories.update(local_categories)

Expand Down Expand Up @@ -121,5 +159,25 @@ def do_sorting(addons_dir: Path, odoo_version: str, project_name: str) -> None:
help="Name of the project, will be the name of category of local addons (default: Local)",
default="Local",
)
def sort_manifest_deps(local_addons_dir: str, odoo_version: str, project_name: str) -> None:
do_sorting(Path(local_addons_dir), odoo_version, project_name)
@option(
"--oca-category",
is_flag=True,
help="Add category for third party addons coming from OCA",
)
@option(
"--reset-cache",
is_flag=True,
help="Purge cache used to identify OCA addons",
)
def sort_manifest_deps(
local_addons_dir: str,
odoo_version: str,
project_name: str,
*,
oca_category: bool = False,
reset_cache: bool = False,
) -> None:
if reset_cache:
other_addons_category_cache.clear()

do_sorting(Path(local_addons_dir), odoo_version, project_name, oca_category=oca_category)