Skip to content

Commit 11b3e8a

Browse files
authored
Replace input prompt with configuration values. (#10)
1 parent 940711a commit 11b3e8a

16 files changed

+335
-242
lines changed

.conda/meta.yaml

-46
This file was deleted.

.gitattributes

-1
This file was deleted.

.github/workflows/continuous-integration-workflow.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
branches:
88
- '*'
99

10+
concurrency:
11+
group: ${{ github.head_ref }}
12+
cancel-in-progress: true
13+
1014
jobs:
1115

1216
run-tests:
@@ -34,7 +38,7 @@ jobs:
3438

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

3943
- name: Upload coverage reports of end-to-end tests.
4044
if: runner.os == 'Linux' && matrix.python-version == '3.8'

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ repos:
66
args: ['--maxkb=100']
77
- id: check-merge-conflict
88
- id: check-yaml
9-
exclude: meta.yaml
109
- id: debug-statements
1110
- id: end-of-file-fixer
1211
- repo: https://github.com/pre-commit/pygrep-hooks

CHANGES.rst

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@ reverse chronological order. Releases follow `semantic versioning
77
<https://anaconda.org/conda-forge/pytask-environment>`_.
88

99

10-
0.0.5 - 2021-07-23
10+
0.1.0 - 2022-01-25
11+
------------------
12+
13+
- :gh:`10` replaces the input prompts with configuration values and flags, removes the
14+
conda recipe, and abort simultaneously running builds.
15+
16+
17+
0.0.6 - 2021-07-23
1118
------------------
1219

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

1522

23+
0.0.5 - 2021-07-23
24+
------------------
25+
26+
- :gh:`7` adds some pre-commit updates.
27+
28+
1629
0.0.4 - 2021-03-05
1730
------------------
1831

MANIFEST.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
prune .conda
21
prune tests
32

43
exclude *.rst
@@ -8,3 +7,5 @@ exclude tox.ini
87

98
include README.rst
109
include LICENSE
10+
11+
recursive-include _static *.png

README.rst

+21-7
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,30 @@ with
5757
Usage
5858
-----
5959

60-
If the user attempts to build the project and the Python version has been cached in the
61-
database in a previous run, an invocation with a different environment will produce the
62-
following command line output.
60+
If the user attempts to build the project with ``pytask build`` and the Python version
61+
has been cached in the database in a previous run, an invocation with a different
62+
environment will produce the following command line output.
63+
64+
.. image:: _static/error.png
65+
66+
Running
6367

6468
.. code-block:: console
6569
66-
$ pytask build
67-
Your Python environment seems to have changed. The Python version has
68-
changed. The path to the Python executable has changed. Do you want
69-
to continue with the current environment? [y/N]:
70+
$ pytask --update-environment
71+
72+
will update the information on the environment.
73+
74+
To disable either checking the path or the version, set the following configuration to a
75+
falsy value.
76+
77+
.. code-block:: ini
78+
79+
# Content of pytask.ini, setup.cfg, or tox.ini
80+
81+
check_python_version = false # true by default
82+
83+
check_environment = false # true by default
7084
7185
7286
Future development

_static/error.png

45.7 KB
Loading

environment.yml

-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ dependencies:
2424
- pdbpp
2525
- pre-commit
2626
- pytest-cov
27-
- pytest-xdist

src/pytask_environment/collect.py

-63
This file was deleted.

src/pytask_environment/config.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import click
2+
from _pytask.config import hookimpl
3+
from _pytask.shared import convert_truthy_or_falsy_to_bool
4+
from _pytask.shared import get_first_non_none_value
5+
6+
7+
@hookimpl
8+
def pytask_extend_command_line_interface(cli):
9+
"""Extend the cli."""
10+
cli.commands["build"].params.append(
11+
click.Option(
12+
["--update-environment"],
13+
is_flag=True,
14+
default=None,
15+
help="Update the information on the environment stored in the database.",
16+
)
17+
)
18+
19+
20+
@hookimpl
21+
def pytask_parse_config(config, config_from_file, config_from_cli):
22+
"""Parse the configuration."""
23+
config["check_python_version"] = get_first_non_none_value(
24+
config_from_file,
25+
key="check_python_version",
26+
default=True,
27+
callback=convert_truthy_or_falsy_to_bool,
28+
)
29+
30+
config["check_environment"] = get_first_non_none_value(
31+
config_from_file,
32+
key="check_environment",
33+
default=True,
34+
callback=convert_truthy_or_falsy_to_bool,
35+
)
36+
37+
config["update_environment"] = get_first_non_none_value(
38+
config_from_cli,
39+
key="update_environment",
40+
default=False,
41+
callback=convert_truthy_or_falsy_to_bool,
42+
)

src/pytask_environment/logging.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import sys
2+
3+
from _pytask.config import hookimpl
4+
from _pytask.console import console
5+
from pony import orm
6+
from pytask_environment.database import Environment
7+
8+
9+
_ERROR_MSG = """\
10+
Aborted execution due to a bad state of the environment. Either switch to the correct \
11+
environment or update the information on the environment using the --update-environment\
12+
flag.
13+
"""
14+
15+
16+
@hookimpl(trylast=True)
17+
def pytask_log_session_header(session) -> None:
18+
"""Check environment and python version.
19+
20+
The solution is hacky. Exploit the first entry-point in the build process after the
21+
database is created.
22+
23+
Check if the version and path of the Python interpreter have changed and if so, ask
24+
the user whether she wants to proceed.
25+
26+
"""
27+
__tracebackhide__ = True
28+
29+
# If no checks are requested, skip.
30+
if (
31+
not session.config["check_python_version"]
32+
and not session.config["check_environment"]
33+
):
34+
return None
35+
36+
package = retrieve_package("python")
37+
38+
same_version = False if package is None else sys.version == package.version
39+
same_path = False if package is None else sys.executable == package.path
40+
41+
# Bail out if everything is fine.
42+
if same_version and same_path:
43+
return None
44+
45+
msg = ""
46+
if not same_version and session.config["check_python_version"]:
47+
msg += "The Python version has changed "
48+
if package is not None:
49+
msg += f"from\n\n{package.version}\n\n"
50+
msg += f"to\n\n{sys.version}\n\n"
51+
if not same_path and session.config["check_environment"]:
52+
msg += "The path to the Python interpreter has changed "
53+
if package is not None:
54+
msg += f"from\n\n{package.path}\n\n"
55+
msg += f"to\n\n{sys.executable}."
56+
57+
if msg:
58+
msg = "Your Python environment has changed. " + msg
59+
60+
if session.config["update_environment"] or package is None:
61+
console.print("Updating the information in the database.")
62+
create_or_update_state("python", sys.version, sys.executable)
63+
elif not msg:
64+
pass
65+
else:
66+
console.print()
67+
raise Exception(msg + "\n\n" + _ERROR_MSG) from None
68+
69+
70+
@orm.db_session
71+
def retrieve_package(name):
72+
"""Return booleans indicating whether the version or path of a package changed."""
73+
try:
74+
package = Environment[name]
75+
except orm.ObjectNotFound:
76+
package = None
77+
return package
78+
79+
80+
@orm.db_session
81+
def create_or_update_state(name, version, path):
82+
"""Create or update a state."""
83+
try:
84+
package_in_db = Environment[name]
85+
except orm.ObjectNotFound:
86+
Environment(name=name, version=version, path=path)
87+
else:
88+
package_in_db.version = version
89+
package_in_db.path = path

src/pytask_environment/plugin.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Entry-point for the plugin."""
22
from _pytask.config import hookimpl
3-
from pytask_environment import collect
3+
from pytask_environment import config
44
from pytask_environment import database
5+
from pytask_environment import logging
56

67

78
@hookimpl
89
def pytask_add_hooks(pm):
910
"""Register some plugins."""
10-
pm.register(collect)
11+
pm.register(logging)
12+
pm.register(config)
1113
pm.register(database)

0 commit comments

Comments
 (0)