Skip to content

Commit

Permalink
Add pre-commit hook to generate external dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Sep 23, 2023
1 parent 969238e commit c9a3005
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@
pass_filenames: false
language: python
files: (__manifest__\.py|__openerp__\.py|__terp__\.py)$

- id: oca-gen-external-dependencies
name: Generate requirements.txt for an addons directory
entry: oca-gen-external-dependencies
language: python
pass_filenames: false
files: (__manifest__\.py|__openerp__\.py|__terp__\.py|setup\.py|pyproject\.toml)$
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"selenium",
"twine",
"wheel",
"setuptools-odoo", # for oca-gen-external-dependencies
"whool", # for oca-gen-external-dependencies
],
python_requires=">=3.6",
classifiers=[
Expand All @@ -64,6 +66,7 @@
"oca-publish-modules = tools.publish_modules:main",
"oca-gen-addon-readme = tools.gen_addon_readme:gen_addon_readme",
"oca-gen-addon-icon = tools.gen_addon_icon:gen_addon_icon",
"oca-gen-external-dependencies = tools.gen_external_dependencies:main",
"oca-towncrier = tools.oca_towncrier:oca_towncrier",
"oca-create-migration-issue = tools.create_migration_issue:main",
"oca-update-pre-commit-excluded-addons = "
Expand Down
59 changes: 59 additions & 0 deletions tools/gen_external_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
"""Generate requirements.txt with external dependencies of Odoo addons."""

import os
import subprocess
import sys
from itertools import chain
from pathlib import Path


def main():
projects = chain(
Path.glob(Path.cwd(), "*/pyproject.toml"),
Path.glob(Path.cwd(), "setup/*/setup.py"),
)

env = os.environ.copy()
env.update(
{
# for better performance
"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", # ignore uninstallable modules etc
"--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:
sys.exit(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()


if __name__ == "__main__":
main()

0 comments on commit c9a3005

Please sign in to comment.