Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype openfica (openfica-tasks) #1050

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 12 additions & 42 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Empty file added openfisca_cli/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions openfisca_cli/main.py
Original file line number Diff line number Diff line change
@@ -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)
80 changes: 80 additions & 0 deletions openfisca_cli/make.py
Original file line number Diff line number Diff line change
@@ -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}..")

44 changes: 44 additions & 0 deletions openfisca_cli/tasks.py
Original file line number Diff line number Diff line change
@@ -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())
5 changes: 4 additions & 1 deletion openfisca_core/tools/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down