From fcf7def3376fccffce290dd235ac31d71150814f Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Wed, 8 Sep 2021 21:00:37 +0200 Subject: [PATCH] Prototype openfica --- Makefile | 54 +++++-------------- openfisca_cli/__init__.py | 0 openfisca_cli/main.py | 9 ++++ openfisca_cli/make.py | 80 +++++++++++++++++++++++++++++ openfisca_cli/tasks.py | 44 ++++++++++++++++ openfisca_core/tools/test_runner.py | 5 +- setup.py | 1 + 7 files changed, 150 insertions(+), 43 deletions(-) create mode 100644 openfisca_cli/__init__.py create mode 100644 openfisca_cli/main.py create mode 100644 openfisca_cli/make.py create mode 100644 openfisca_cli/tasks.py diff --git a/Makefile b/Makefile index 30a0578f0d..2392c0086a 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,21 @@ doc = sed -n "/^$1/ { x ; p ; } ; s/\#\#/[⚙]/ ; s/\./.../ ; x" ${MAKEFILE_LIST} -## Same as `make test. -all: test +## Launch all `openfica` tasks. +all: \ + openfica-clean \ + openfica-check-syntax-errors \ + openfica-check-style \ + openfica-check-types \ + openfica-test \ + ; + +openfica-%: + @openfica make.$* ## Install project dependencies. install: @$(call doc,$@:) - @pip install --upgrade pip twine wheel + @pip install --upgrade invoke pip twine wheel @pip install --editable .[dev] --upgrade --use-deprecated=legacy-resolver ## Install openfisca-core for deployment and publishing. @@ -21,42 +30,3 @@ build: setup.py uninstall: @$(call doc,$@:) @pip freeze | grep -v "^-e" | sed "s/@.*//" | xargs pip uninstall -y - -## Delete builds and compiled python files. -clean: \ - $(shell ls -d * | grep "build\|dist") \ - $(shell find . -name "*.pyc") - @$(call doc,$@:) - @rm -rf $? - -## Compile python files to check for syntax errors. -check-syntax-errors: . - @$(call doc,$@:) - @python -m compileall -q $? - -## Run linters to check for syntax and style errors. -check-style: $(shell git ls-files "*.py") - @$(call doc,$@:) - @flake8 $? - -## Run code formatters to correct style errors. -format-style: $(shell git ls-files "*.py") - @$(call doc,$@:) - @autopep8 $? - -## Run static type checkers for type errors. -check-types: openfisca_core openfisca_web_api - @$(call doc,$@:) - @mypy $? - -## Run openfisca-core tests. -test: clean check-syntax-errors check-style check-types - @$(call doc,$@:) - @env PYTEST_ADDOPTS="${PYTEST_ADDOPTS} --cov=openfisca_core" pytest - -## Serve the openfisca Web API. -api: - @$(call doc,$@:) - @openfisca serve \ - --country-package openfisca_country_template \ - --extensions openfisca_extension_template diff --git a/openfisca_cli/__init__.py b/openfisca_cli/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/openfisca_cli/main.py b/openfisca_cli/main.py new file mode 100644 index 0000000000..1f204ced69 --- /dev/null +++ b/openfisca_cli/main.py @@ -0,0 +1,9 @@ +from invoke import Collection, Program +from . import make +from . import tasks + +namespace = Collection() +namespace.add_task(tasks.test) +namespace.add_task(tasks.serve) +namespace.add_collection(Collection.from_module(make)) +program = Program(namespace = namespace) diff --git a/openfisca_cli/make.py b/openfisca_cli/make.py new file mode 100644 index 0000000000..cdf944ca2a --- /dev/null +++ b/openfisca_cli/make.py @@ -0,0 +1,80 @@ +import sys + +import invoke + +from . import tasks + + +@invoke.task +def clean(context): + """Delete builds and compiled python files.""" + + info(clean) + + context.run("rm -rf build dist") + context.run("find . -name '*.pyc' | xargs rm -f") + + +@invoke.task +def check_syntax_errors(context): + """Compile python files to check for syntax errors.""" + + info(check_syntax_errors) + + context.run("python -m compileall -q .") + + +@invoke.task +def check_style(context): + """Run linters to check for syntax and style errors.""" + + info(check_style) + + context.run("flake8 openfisca_core openfisca_web_api") + + +@invoke.task +def format_style(context): + """Run code formatters to correct style errors.""" + + info(format_style) + + context.run("autopep8 openfisca_core openfisca_web_api") + + +@invoke.task +def check_types(context): + """Run static type checkers for type errors.""" + + info(check_types) + + context.run("mypy openfisca_core openfisca_web_api") + + +@invoke.task +def test(context, workers = None): + """Run openfisca-core tests.""" + + info(test) + + path = context.run("git ls-files 'tests/*.py'", hide = "out").stdout + sys.argv = sys.argv[0:1] + ["test"] + path.split() + tasks.test(context, path) + + +@invoke.task +def api(context): + """Serve the openfisca Web API.""" + + sys.argv[1] = "serve" + + tasks.serve( + context, + "openfisca_country_template", + "openfisca_extension_template", + ) + +def info(func): + doc = func.__doc__.split('\n')[0] + print(f"[⚙] {doc}..") + diff --git a/openfisca_cli/tasks.py b/openfisca_cli/tasks.py new file mode 100644 index 0000000000..84918d5510 --- /dev/null +++ b/openfisca_cli/tasks.py @@ -0,0 +1,44 @@ +import os +import sys + +import invoke + +from openfisca_core.scripts import openfisca_command +from openfisca_core.scripts.run_test import main as _test +from openfisca_web_api.scripts.serve import main as _serve + +@invoke.task( + help = { + "path": "Paths (files or directories) of tests to execute.", + }, + ) +def test(_, path, workers = None): + """Run OpenFisca tests. + + Examples: + $ openfica test "$(git ls-files 'tests/**/*.py')" + + """ + + # Pseudo-implementation of `openfisca test` + sys.argv = sys.argv[0:2] + path.split() + _test(openfisca_command.get_parser()) + + +@invoke.task( + optional = ["extensions"], + help = { + "country-package": "Country package to use.", + "extensions": "Extensions to load.", + }, + ) +def serve(_, country_package, extensions = None): + """Run the OpenFisca Web API. + + Examples: + $ openfica serve --country-package openfisca_country_template + + """ + + # Pseudo-implementation of `openfisca serve` + _serve(openfisca_command.get_parser()) diff --git a/openfisca_core/tools/test_runner.py b/openfisca_core/tools/test_runner.py index f6833c74bb..ab120ace6a 100644 --- a/openfisca_core/tools/test_runner.py +++ b/openfisca_core/tools/test_runner.py @@ -64,11 +64,14 @@ def run_tests(tax_benefit_system, paths, options = None): """ - argv = ["--capture", "no"] + argv = [] if options.get('pdb'): argv.append('--pdb') + if options.get('verbose'): + argv.append('--verbose') + if isinstance(paths, str): paths = [paths] diff --git a/setup.py b/setup.py index eb83761916..346790b1bc 100644 --- a/setup.py +++ b/setup.py @@ -62,6 +62,7 @@ 'console_scripts': [ 'openfisca=openfisca_core.scripts.openfisca_command:main', 'openfisca-run-test=openfisca_core.scripts.openfisca_command:main', + 'openfica = openfisca_cli.main:program.run', ], }, extras_require = {