Skip to content

Commit

Permalink
Prototype openfica
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Sep 8, 2021
1 parent bdcad8e commit fcf7def
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 43 deletions.
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")

This comment has been minimized.

Copy link
@benoit-cty

benoit-cty Sep 10, 2021

Contributor

Does it work on Windows ?

This comment has been minimized.

Copy link
@benjello

benjello Sep 10, 2021

Member

If executed in a bash console (as the one available with git for example), yes !
But make is not easily available.

And I think you are right that we should use a more robust remover here maybe directly in python.

This comment has been minimized.

Copy link
@bonjourmauko

bonjourmauko Sep 10, 2021

Member

@benoit-cty I guess it does not, but agree with @benjello this could be done purely with Python I guess...

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

1 comment on commit fcf7def

@bonjourmauko
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way @benoit-cty this could be an inspiration for the pure-python parallelism: https://github.com/PyCQA/flake8/blob/281f3f8b43106023bf3fb160666384394031a9fd/src/flake8/checker.py

Please sign in to comment.