Skip to content

Replace input prompt with configuration values. #10

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

Merged
merged 18 commits into from
Jan 26, 2022
Merged
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
46 changes: 0 additions & 46 deletions .conda/meta.yaml

This file was deleted.

1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

6 changes: 5 additions & 1 deletion .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
branches:
- '*'

concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true

jobs:

run-tests:
Expand Down Expand Up @@ -34,7 +38,7 @@ jobs:

- name: Run end-to-end tests.
shell: bash -l {0}
run: tox -e pytest -- -m end_to_end --cov=./ --cov-report=xml -n auto
run: tox -e pytest -- -m end_to_end --cov=./ --cov-report=xml

- name: Upload coverage reports of end-to-end tests.
if: runner.os == 'Linux' && matrix.python-version == '3.8'
Expand Down
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ repos:
args: ['--maxkb=100']
- id: check-merge-conflict
- id: check-yaml
exclude: meta.yaml
- id: debug-statements
- id: end-of-file-fixer
- repo: https://github.com/pre-commit/pygrep-hooks
Expand Down
15 changes: 14 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ reverse chronological order. Releases follow `semantic versioning
<https://anaconda.org/conda-forge/pytask-environment>`_.


0.0.5 - 2021-07-23
0.1.0 - 2022-01-25
------------------

- :gh:`10` replaces the input prompts with configuration values and flags, removes the
conda recipe, and abort simultaneously running builds.


0.0.6 - 2021-07-23
------------------

- :gh:`8` replaces versioneer with setuptools-scm.


0.0.5 - 2021-07-23
------------------

- :gh:`7` adds some pre-commit updates.


0.0.4 - 2021-03-05
------------------

Expand Down
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
prune .conda
prune tests

exclude *.rst
Expand All @@ -8,3 +7,5 @@ exclude tox.ini

include README.rst
include LICENSE

recursive-include _static *.png
28 changes: 21 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,30 @@ with
Usage
-----

If the user attempts to build the project and the Python version has been cached in the
database in a previous run, an invocation with a different environment will produce the
following command line output.
If the user attempts to build the project with ``pytask build`` and the Python version
has been cached in the database in a previous run, an invocation with a different
environment will produce the following command line output.

.. image:: _static/error.png

Running

.. code-block:: console

$ pytask build
Your Python environment seems to have changed. The Python version has
changed. The path to the Python executable has changed. Do you want
to continue with the current environment? [y/N]:
$ pytask --update-environment

will update the information on the environment.

To disable either checking the path or the version, set the following configuration to a
falsy value.

.. code-block:: ini

# Content of pytask.ini, setup.cfg, or tox.ini

check_python_version = false # true by default

check_environment = false # true by default


Future development
Expand Down
Binary file added _static/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ dependencies:
- pdbpp
- pre-commit
- pytest-cov
- pytest-xdist
63 changes: 0 additions & 63 deletions src/pytask_environment/collect.py

This file was deleted.

42 changes: 42 additions & 0 deletions src/pytask_environment/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import click
from _pytask.config import hookimpl
from _pytask.shared import convert_truthy_or_falsy_to_bool
from _pytask.shared import get_first_non_none_value


@hookimpl
def pytask_extend_command_line_interface(cli):
"""Extend the cli."""
cli.commands["build"].params.append(
click.Option(
["--update-environment"],
is_flag=True,
default=None,
help="Update the information on the environment stored in the database.",
)
)


@hookimpl
def pytask_parse_config(config, config_from_file, config_from_cli):
"""Parse the configuration."""
config["check_python_version"] = get_first_non_none_value(
config_from_file,
key="check_python_version",
default=True,
callback=convert_truthy_or_falsy_to_bool,
)

config["check_environment"] = get_first_non_none_value(
config_from_file,
key="check_environment",
default=True,
callback=convert_truthy_or_falsy_to_bool,
)

config["update_environment"] = get_first_non_none_value(
config_from_cli,
key="update_environment",
default=False,
callback=convert_truthy_or_falsy_to_bool,
)
89 changes: 89 additions & 0 deletions src/pytask_environment/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import sys

from _pytask.config import hookimpl
from _pytask.console import console
from pony import orm
from pytask_environment.database import Environment


_ERROR_MSG = """\
Aborted execution due to a bad state of the environment. Either switch to the correct \
environment or update the information on the environment using the --update-environment\
flag.
"""


@hookimpl(trylast=True)
def pytask_log_session_header(session) -> None:
"""Check environment and python version.

The solution is hacky. Exploit the first entry-point in the build process after the
database is created.

Check if the version and path of the Python interpreter have changed and if so, ask
the user whether she wants to proceed.

"""
__tracebackhide__ = True

# If no checks are requested, skip.
if (
not session.config["check_python_version"]
and not session.config["check_environment"]
):
return None

package = retrieve_package("python")

same_version = False if package is None else sys.version == package.version
same_path = False if package is None else sys.executable == package.path

# Bail out if everything is fine.
if same_version and same_path:
return None

msg = ""
if not same_version and session.config["check_python_version"]:
msg += "The Python version has changed "
if package is not None:
msg += f"from\n\n{package.version}\n\n"
msg += f"to\n\n{sys.version}\n\n"
if not same_path and session.config["check_environment"]:
msg += "The path to the Python interpreter has changed "
if package is not None:
msg += f"from\n\n{package.path}\n\n"
msg += f"to\n\n{sys.executable}."

if msg:
msg = "Your Python environment has changed. " + msg

if session.config["update_environment"] or package is None:
console.print("Updating the information in the database.")
create_or_update_state("python", sys.version, sys.executable)
elif not msg:
pass
else:
console.print()
raise Exception(msg + "\n\n" + _ERROR_MSG) from None


@orm.db_session
def retrieve_package(name):
"""Return booleans indicating whether the version or path of a package changed."""
try:
package = Environment[name]
except orm.ObjectNotFound:
package = None
return package


@orm.db_session
def create_or_update_state(name, version, path):
"""Create or update a state."""
try:
package_in_db = Environment[name]
except orm.ObjectNotFound:
Environment(name=name, version=version, path=path)
else:
package_in_db.version = version
package_in_db.path = path
6 changes: 4 additions & 2 deletions src/pytask_environment/plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Entry-point for the plugin."""
from _pytask.config import hookimpl
from pytask_environment import collect
from pytask_environment import config
from pytask_environment import database
from pytask_environment import logging


@hookimpl
def pytask_add_hooks(pm):
"""Register some plugins."""
pm.register(collect)
pm.register(logging)
pm.register(config)
pm.register(database)
Loading