Skip to content

Commit 1a9bc3f

Browse files
committed
Generate pyproject.toml instead of setup.py
By default, this is done for branches 17.0+. The minimum version to generate pyproject.toml instead of setup.py is configurable with GEN_PYPROJECT_MIN_VERSION and there are two new switchable tasks: whool_init and gen_metapackage.
1 parent ed5f573 commit 1a9bc3f

File tree

8 files changed

+122
-13
lines changed

8 files changed

+122
-13
lines changed

Dockerfile

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ RUN set -x \
3737
&& python3 -m venv /ocamt \
3838
&& /ocamt/bin/pip install --no-cache-dir -U pip wheel
3939
RUN set -x \
40-
&& /ocamt/bin/pip install --no-cache-dir -e git+https://github.com/OCA/maintainer-tools@969238e47c07d0c40573acff81d170f63245d738#egg=oca-maintainers-tools \
40+
&& /ocamt/bin/pip install --no-cache-dir -e git+https://github.com/OCA/maintainer-tools@568cacd1d6eef453063a524a5ce63dcd49c7259b#egg=oca-maintainers-tools \
4141
&& ln -s /ocamt/bin/oca-gen-addons-table /usr/local/bin/ \
4242
&& ln -s /ocamt/bin/oca-gen-addon-readme /usr/local/bin/ \
43-
&& ln -s /ocamt/bin/oca-towncrier /usr/local/bin/
44-
RUN set -x \
45-
&& /ocamt/bin/pip install --no-cache-dir 'setuptools-odoo>=3.0.3' \
43+
&& ln -s /ocamt/bin/oca-gen-metapackage /usr/local/bin/ \
44+
&& ln -s /ocamt/bin/oca-towncrier /usr/local/bin/ \
4645
&& ln -s /ocamt/bin/setuptools-odoo-make-default /usr/local/bin/
4746

4847
# isolate from system python libraries

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"lxml",
3939
# for setuptools-odoo-make-default
4040
"setuptools-odoo",
41+
# for whool-init
42+
"whool",
4143
# packaging
4244
"packaging>=22",
4345
],

src/oca_github_bot/config.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def func_wrapper(*args, **kwargs):
5959
# Available tasks:
6060
# delete_branch,tag_approved,tag_ready_to_merge,gen_addons_table,
6161
# gen_addons_readme,gen_addons_icon,setuptools_odoo,merge_bot,tag_needs_review,
62-
# migration_issue_bot
62+
# migration_issue_bot,whool_init,gen_metapackage
6363
BOT_TASKS = os.environ.get("BOT_TASKS", "all").split(",")
6464

6565
BOT_TASKS_DISABLED = os.environ.get("BOT_TASKS_DISABLED", "").split(",")
@@ -143,4 +143,10 @@ def func_wrapper(*args, **kwargs):
143143
"build,pip,setuptools<58,wheel,setuptools-odoo,whool",
144144
).split(",")
145145

146+
# minimum Odoo series supported by the bot
146147
MAIN_BRANCH_BOT_MIN_VERSION = os.environ.get("MAIN_BRANCH_BOT_MIN_VERSION", "8.0")
148+
149+
# First Odoo Series for which the whool_init and gen_metapackage tasks are run on main
150+
# branches. For previous versions, the setuptools_odoo task is run and generates
151+
# setup.py instead of pyproject.toml.
152+
GEN_PYPROJECT_MIN_VERSION = os.environ.get("GEN_PYPROJECT_MIN_VERSION", "17.0")

src/oca_github_bot/github.py

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
import tempfile
99
from contextlib import contextmanager
10+
from pathlib import Path
1011

1112
import appdirs
1213
import github3
@@ -159,3 +160,13 @@ def git_get_head_sha(cwd):
159160

160161
def git_get_current_branch(cwd):
161162
return check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd).strip()
163+
164+
165+
def git_commit_if_needed(glob_pattern, msg, cwd):
166+
files = [p.absolute() for p in Path(cwd).glob(glob_pattern)]
167+
if not files:
168+
return # no match nothing to commit
169+
check_call(["git", "add", *files], cwd=cwd)
170+
if call(["git", "diff", "--cached", "--quiet", "--exit-code"], cwd=cwd) == 0:
171+
return # nothing added
172+
return check_call(["git", "commit", "-m", msg], cwd=cwd)

src/oca_github_bot/tasks/main_branch_bot.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
GEN_ADDON_ICON_EXTRA_ARGS,
88
GEN_ADDON_README_EXTRA_ARGS,
99
GEN_ADDONS_TABLE_EXTRA_ARGS,
10+
GEN_PYPROJECT_MIN_VERSION,
1011
dist_publisher,
1112
switchable,
1213
)
13-
from ..github import git_push_if_needed, temporary_clone
14+
from ..github import git_commit_if_needed, git_push_if_needed, temporary_clone
1415
from ..manifest import get_odoo_series_from_branch
1516
from ..process import check_call
1617
from ..queue import getLogger, task
17-
from ..version_branch import is_main_branch_bot_branch
18+
from ..version_branch import is_main_branch_bot_branch, is_supported_main_branch
1819

1920
_logger = getLogger(__name__)
2021

@@ -73,6 +74,32 @@ def _setuptools_odoo_make_default(org, repo, branch, cwd):
7374
)
7475

7576

77+
@switchable("whool_init")
78+
def _whool_init(org, repo, branch, cwd):
79+
_logger.info(
80+
"generate pyproejct.toml with whool init in %s/%s@%s\n", org, repo, branch
81+
)
82+
whool_init_cmd = ["whool", "init"]
83+
check_call(whool_init_cmd, cwd=cwd)
84+
git_commit_if_needed("*/pyproject.toml", "[BOT] add pyproject.toml", cwd=cwd)
85+
86+
87+
@switchable("gen_metapackage")
88+
def _gen_metapackage(org, repo, branch, cwd):
89+
if not is_supported_main_branch(branch, min_version="15.0"):
90+
# We don't support branches < 15 because I don't want to worry about
91+
# the package name prefix. From 15.0 on, the package name is always prefixed
92+
# with "odoo-addons-".
93+
_logger.warning("gen_metapackage not supported for branch %s", branch)
94+
return
95+
_logger.info("oca-gen-metapackage in %s/%s@%s\n", org, repo, branch)
96+
gen_metapackage_cmd = ["oca-gen-metapackage", f"odoo-addons-{org.lower()}-{repo}"]
97+
check_call(gen_metapackage_cmd, cwd=cwd)
98+
git_commit_if_needed(
99+
"setup/_metapackage", "[BOT] add or update setup/_metapackage", cwd=cwd
100+
)
101+
102+
76103
def main_branch_bot_actions(org, repo, branch, cwd):
77104
"""
78105
Run main branch bot actions on a local git checkout.
@@ -86,8 +113,13 @@ def main_branch_bot_actions(org, repo, branch, cwd):
86113
_gen_addons_readme(org, repo, branch, cwd)
87114
# generate icon
88115
_gen_addons_icon(org, repo, branch, cwd)
89-
# generate/clean default setup.py
90-
_setuptools_odoo_make_default(org, repo, branch, cwd)
116+
if is_supported_main_branch(branch, min_version=GEN_PYPROJECT_MIN_VERSION):
117+
# generate pyproject.toml for addons and metapackage
118+
_whool_init(org, repo, branch, cwd)
119+
_gen_metapackage(org, repo, branch, cwd)
120+
else:
121+
# generate/clean default setup.py and metapackage
122+
_setuptools_odoo_make_default(org, repo, branch, cwd)
91123

92124

93125
@task()

src/oca_github_bot/version_branch.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@
1717
)
1818

1919

20-
def is_main_branch_bot_branch(branch_name):
20+
def is_supported_main_branch(branch_name, min_version=None):
2121
if not ODOO_VERSION_RE.match(branch_name):
2222
return False
23-
return version.parse(branch_name) >= version.parse(
24-
config.MAIN_BRANCH_BOT_MIN_VERSION
23+
branch_version = version.parse(branch_name)
24+
if min_version and branch_version < version.parse(min_version):
25+
return False
26+
return True
27+
28+
29+
def is_main_branch_bot_branch(branch_name):
30+
return is_supported_main_branch(
31+
branch_name, min_version=config.MAIN_BRANCH_BOT_MIN_VERSION
2532
)
2633

2734

tests/test_git.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33

44
import re
55
import subprocess
6+
from pathlib import Path
67

7-
from oca_github_bot.github import git_get_current_branch, git_get_head_sha
8+
from oca_github_bot.github import (
9+
git_commit_if_needed,
10+
git_get_current_branch,
11+
git_get_head_sha,
12+
)
813

914

1015
def test_git_get_head_sha(git_clone):
@@ -16,3 +21,35 @@ def test_git_get_current_branch(git_clone):
1621
assert git_get_current_branch(git_clone) == "master"
1722
subprocess.check_call(["git", "checkout", "-b", "abranch"], cwd=git_clone)
1823
assert git_get_current_branch(git_clone) == "abranch"
24+
25+
26+
def test_git_commit_if_needed_no_change(tmp_path: Path) -> None:
27+
subprocess.check_call(["git", "init"], cwd=tmp_path)
28+
subprocess.check_call(
29+
["git", "config", "user.email", "[email protected]"], cwd=tmp_path
30+
)
31+
toto = tmp_path / "toto"
32+
toto.touch()
33+
git_commit_if_needed("toto", "initial commit", cwd=tmp_path)
34+
head_sha = git_get_head_sha(tmp_path)
35+
# no change
36+
git_commit_if_needed("toto", "no commit", cwd=tmp_path)
37+
assert git_get_head_sha(tmp_path) == head_sha
38+
# change in existing file
39+
toto.write_text("toto")
40+
git_commit_if_needed("toto", "toto changed", cwd=tmp_path)
41+
head_sha2 = git_get_head_sha(tmp_path)
42+
assert head_sha2 != head_sha
43+
# add subdirectory
44+
subdir = tmp_path / "subdir"
45+
subdir.mkdir()
46+
titi = subdir / "titi"
47+
titi.touch()
48+
git_commit_if_needed("subdir", "titi added", cwd=tmp_path)
49+
head_sha3 = git_get_head_sha(tmp_path)
50+
assert head_sha3 != head_sha2
51+
# add glob
52+
subdir.joinpath("pyproject.toml").touch()
53+
git_commit_if_needed("*/pyproject.toml", "pyproject.toml added", cwd=tmp_path)
54+
head_sha4 = git_get_head_sha(tmp_path)
55+
assert head_sha4 != head_sha3

tests/test_version_branch.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Copyright (c) ACSONE SA/NV 2018
22
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
33

4+
import pytest
5+
46
from oca_github_bot.version_branch import (
57
is_main_branch_bot_branch,
68
is_merge_bot_branch,
79
is_protected_branch,
10+
is_supported_main_branch,
811
make_merge_bot_branch,
912
parse_merge_bot_branch,
1013
search_merge_bot_branch,
@@ -72,3 +75,15 @@ def test_search_merge_bot_branch():
7275
assert search_merge_bot_branch(text) == "12.0-ocabot-merge-pr-100-by-toto-bump-no"
7376
text = "blah blah more stuff"
7477
assert search_merge_bot_branch(text) is None
78+
79+
80+
@pytest.mark.parametrize(
81+
("branch_name", "min_version", "expected"),
82+
[
83+
("8.0", None, True),
84+
("8.0", "8.0", True),
85+
("8.0", "9.0", False),
86+
],
87+
)
88+
def test_is_supported_branch(branch_name, min_version, expected):
89+
assert is_supported_main_branch(branch_name, min_version) is expected

0 commit comments

Comments
 (0)