forked from OCA/maintainer-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-commit hook to generate external dependencies
- Loading branch information
Showing
6 changed files
with
147 additions
and
0 deletions.
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import subprocess | ||
import sys | ||
import textwrap | ||
|
||
import pytest | ||
|
||
from tools.gen_external_dependencies import main as gen_external_dependencies | ||
|
||
from .utils import dir_changer | ||
|
||
|
||
def _make_addon(addons_dir, addon_name, depends, external_dependencies): | ||
addon_dir = addons_dir / addon_name | ||
addon_dir.mkdir() | ||
manifest = { | ||
"name": addon_name, | ||
"version": "16.0.1.0.0", | ||
"depends": depends, | ||
"external_dependencies": external_dependencies, | ||
} | ||
addon_dir.joinpath("__manifest__.py").write_text(repr(manifest)) | ||
addon_dir.joinpath("__init__.py").touch() | ||
|
||
|
||
@pytest.mark.skipif("sys.version_info < (3,7)") | ||
def test_gen_external_dependencies(tmp_path): | ||
... | ||
_make_addon( | ||
tmp_path, | ||
addon_name="addon1", | ||
depends=["mis_builder"], | ||
external_dependencies={"python": ["requests", "xlrd"]}, | ||
) | ||
_make_addon( | ||
tmp_path, | ||
addon_name="addon2", | ||
depends=[], | ||
external_dependencies={"python": ["requests", "pydantic>=2"]}, | ||
) | ||
with dir_changer(tmp_path): | ||
assert gen_external_dependencies() != 0 # no pyproject.toml | ||
subprocess.run([sys.executable, "-m", "whool", "init"], check=True) | ||
assert tmp_path.joinpath("addon1").joinpath("pyproject.toml").is_file() | ||
assert tmp_path.joinpath("addon2").joinpath("pyproject.toml").is_file() | ||
assert gen_external_dependencies() == 0 | ||
requirements_txt_path = tmp_path.joinpath("requirements.txt") | ||
assert requirements_txt_path.is_file() | ||
assert requirements_txt_path.read_text() == textwrap.dedent( | ||
"""\ | ||
# generated from manifests external_dependencies | ||
pydantic>=2 | ||
requests | ||
xlrd | ||
""" | ||
) |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
|
||
@contextmanager | ||
def dir_changer(path: Path) -> Iterator[None]: | ||
"""A context manager that changes the current working directory""" | ||
old_cwd = Path.cwd() | ||
os.chdir(path) | ||
try: | ||
yield | ||
finally: | ||
os.chdir(old_cwd) |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python3 | ||
"""Generate requirements.txt with external dependencies of Odoo addons.""" | ||
|
||
import os | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
|
||
def main() -> int: | ||
if sys.version_info < (3, 7): | ||
raise SystemExit("Python 3.7+ is required.") | ||
|
||
projects = [ | ||
*Path.glob(Path.cwd(), "*/pyproject.toml"), | ||
*Path.glob(Path.cwd(), "setup/*/setup.py"), | ||
] | ||
|
||
if not projects: | ||
return 1 | ||
|
||
env = os.environ.copy() | ||
env.update( | ||
{ | ||
# for better performance, since we are not interested in precise versions | ||
"WHOOL_POST_VERSION_STRATEGY_OVERRIDE": "none", | ||
"SETUPTOOLS_ODOO_POST_VERSION_STRATEGY_OVERRIDE": "none", | ||
} | ||
) | ||
|
||
result = subprocess.run( | ||
[ | ||
sys.executable, | ||
"-m", | ||
"pyproject_dependencies", | ||
"--no-isolation", # whool and setuptools Odoo must be preinstalled | ||
"--ignore-build-errors", | ||
"--name-filter", | ||
r"^(odoo$|odoo\d*-addon-)", # filter out odoo and odoo addons | ||
*projects, | ||
], | ||
env=env, | ||
check=False, | ||
stdout=subprocess.PIPE, | ||
text=True, | ||
) | ||
|
||
if result.returncode != 0: | ||
return result.returncode | ||
|
||
requirements = result.stdout | ||
|
||
requirements_path = Path("requirements.txt") | ||
if requirements: | ||
with requirements_path.open("w") as f: | ||
f.write("# generated from manifests external_dependencies\n") | ||
f.write(requirements) | ||
else: | ||
if requirements_path.exists(): | ||
requirements_path.unlink() | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |