From c0ba67a5661a9c9fa9344de4dca040895878ea80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sat, 23 Sep 2023 12:39:58 +0200 Subject: [PATCH] Add pre-commit hook to generate external dependencies --- .pre-commit-hooks.yaml | 7 ++++ setup.py | 4 ++ tools/gen_external_dependencies.py | 63 ++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tools/gen_external_dependencies.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 8aa5c4ecf..1a89f9ece 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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)$ diff --git a/setup.py b/setup.py index 46f9215b5..27d20ee30 100644 --- a/setup.py +++ b/setup.py @@ -38,6 +38,9 @@ "selenium", "twine", "wheel", + "pyproject_dependencies ; python_version>='3.7'", + "setuptools-odoo", # for oca-gen-external-dependencies + "whool", # for oca-gen-external-dependencies ], python_requires=">=3.6", classifiers=[ @@ -64,6 +67,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 = " diff --git a/tools/gen_external_dependencies.py b/tools/gen_external_dependencies.py new file mode 100644 index 000000000..3a73ea6b6 --- /dev/null +++ b/tools/gen_external_dependencies.py @@ -0,0 +1,63 @@ +#!/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(): + 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: + sys.exit(0) + + 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 + "--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()