-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: set preferred Python version for dev environment (#245)
* DX: run GitHub workflows on Python version * DX: specify Python version in GitPod config * DX: update Python version in Conda environment * DX: update Python version in Read the Docs config * DX: upgrade to `ubuntu-22.04` in Read the Docs config * MAINT: use `Path.exists()` where possible
- Loading branch information
Showing
10 changed files
with
236 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ | |
"prereleased", | ||
"prettierignore", | ||
"prettierrc", | ||
"pyenv", | ||
"pyright", | ||
"pyupgrade", | ||
"redeboer", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
tasks: | ||
- init: pyenv local 3.8 | ||
- init: pip install -e .[dev] | ||
|
||
github: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Update the :file:`environment.yml` Conda environment file.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from ruamel.yaml.scalarstring import PlainScalarString | ||
|
||
from repoma.errors import PrecommitError | ||
from repoma.utilities import CONFIG_PATH | ||
from repoma.utilities.project_info import PythonVersion, get_constraints_file | ||
from repoma.utilities.yaml import create_prettier_round_trip_yaml | ||
|
||
if TYPE_CHECKING: | ||
from ruamel.yaml.comments import CommentedMap, CommentedSeq | ||
|
||
|
||
def main(python_version: PythonVersion) -> None: | ||
if not CONFIG_PATH.conda.exists(): | ||
return | ||
yaml = create_prettier_round_trip_yaml() | ||
conda_env: CommentedMap = yaml.load(CONFIG_PATH.conda) | ||
conda_deps: CommentedSeq = conda_env.get("dependencies", []) | ||
|
||
updated = _update_python_version(python_version, conda_deps) | ||
updated |= _update_pip_dependencies(python_version, conda_deps) | ||
if updated: | ||
yaml.dump(conda_env, CONFIG_PATH.conda) | ||
msg = f"Set the Python version in {CONFIG_PATH.conda} to {python_version}" | ||
raise PrecommitError(msg) | ||
|
||
|
||
def _update_python_version(version: PythonVersion, conda_deps: CommentedSeq) -> bool: | ||
idx = __find_python_dependency_index(conda_deps) | ||
expected = f"python=={version}.*" | ||
if idx is not None and conda_deps[idx] != expected: | ||
conda_deps[idx] = expected | ||
return True | ||
return False | ||
|
||
|
||
def _update_pip_dependencies(version: PythonVersion, conda_deps: CommentedSeq) -> bool: | ||
pip_deps = __get_pip_dependencies(conda_deps) | ||
if pip_deps is None: | ||
return False | ||
constraints_file = get_constraints_file(version) | ||
if constraints_file is None: | ||
expected_pip = "-e .[dev]" | ||
else: | ||
expected_pip = f"-c {constraints_file} -e .[dev]" | ||
if len(pip_deps) and pip_deps[0] != expected_pip: | ||
pip_deps[0] = PlainScalarString(expected_pip) | ||
return True | ||
return False | ||
|
||
|
||
def __find_python_dependency_index(dependencies: CommentedSeq) -> int | None: | ||
for i, dep in enumerate(dependencies): | ||
if not isinstance(dep, str): | ||
continue | ||
if dep.strip().startswith("python"): | ||
return i | ||
return None | ||
|
||
|
||
def __get_pip_dependencies(dependencies: CommentedSeq) -> CommentedSeq | None: | ||
for dep in dependencies: | ||
if not isinstance(dep, dict): | ||
continue | ||
pip_deps = dep.get("pip") | ||
if pip_deps is not None and isinstance(pip_deps, list): | ||
return pip_deps | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.