This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.py
More file actions
93 lines (73 loc) · 2.92 KB
/
Copy pathlogging.py
File metadata and controls
93 lines (73 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""This module contains everything related to logging."""
from __future__ import annotations
import sys
from pony import orm
from pytask import console
from pytask import hookimpl
from pytask import Session
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: 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
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
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 # noqa: TRY002
@orm.db_session
def retrieve_package(name: str) -> str | None:
"""Return booleans indicating whether the version or path of a package changed."""
try:
package = Environment[name] # type: ignore[type-arg, valid-type]
except orm.ObjectNotFound:
package = None # type: ignore[misc]
return package
@orm.db_session
def create_or_update_state(name: str, version: str, path: str) -> None:
"""Create or update a state."""
try:
package_in_db = Environment[name] # type: ignore[type-arg, valid-type]
except orm.ObjectNotFound:
Environment(name=name, version=version, path=path)
else:
package_in_db.version = version
package_in_db.path = path