-
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.
DX: add
.envrc
for Conda and Pixi environments (#354)
- Loading branch information
Showing
7 changed files
with
55 additions
and
2 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
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 @@ | ||
layout anaconda |
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
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,41 @@ | ||
"""Update the :file:`.envrc` file for `direnv <https://direnv.net/>`_.""" | ||
|
||
from __future__ import annotations | ||
|
||
from textwrap import dedent | ||
|
||
from compwa_policy.errors import PrecommitError | ||
from compwa_policy.utilities import CONFIG_PATH | ||
from compwa_policy.utilities.pyproject import Pyproject | ||
|
||
|
||
def main() -> None: | ||
if ( | ||
CONFIG_PATH.pixi_lock.exists() | ||
or CONFIG_PATH.pixi_toml.exists() | ||
or (CONFIG_PATH.pyproject.exists() and Pyproject.load().has_table("tool.pixi")) | ||
): | ||
_update_envrc(""" | ||
watch_file pixi.lock | ||
eval "$(pixi shell-hook)" | ||
""") | ||
elif CONFIG_PATH.conda.exists(): | ||
_update_envrc("layout anaconda") | ||
|
||
|
||
def _update_envrc(expected: str) -> None: | ||
expected = dedent(expected).strip() + "\n" | ||
existing = __get_existing_envrc() | ||
if existing == expected: | ||
return | ||
with open(".envrc", "w") as f: | ||
f.write(expected) | ||
msg = "Updated .envrc for direnv" | ||
raise PrecommitError(msg) | ||
|
||
|
||
def __get_existing_envrc() -> str | None: | ||
if not CONFIG_PATH.envrc.exists(): | ||
return None | ||
with open(CONFIG_PATH.envrc) as f: | ||
return f.read() |
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