Skip to content

Commit

Permalink
Provide venv init script in generated projects
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb committed Aug 31, 2024
1 parent 8f8c0f8 commit 6541e6b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 5 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions project/tools/initialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from helpers.copier import finish_task
from helpers.git import ensure_git
from helpers.venv import ensure_project_venv

if __name__ == "__main__":
try:
ensure_git()
venv = ensure_project_venv()
except Exception as err: # pylint: disable=broad-except
finish_task(
f"Failed initializing environment: {err}",
False,
extra=(
"No worries, just follow the manual steps documented here: "
"https://salt-extensions.github.io/salt-extension-copier/topics/creation.html#first-steps"
),
)
finish_task("Successfully initialized environment", True)
13 changes: 8 additions & 5 deletions tasks/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import sys
from pathlib import Path

from helpers.copier import finish_task
from helpers.git import ensure_git
from helpers.git import list_untracked
from helpers.pre_commit import run_pre_commit
from helpers.venv import ensure_project_venv
from task_helpers.pythonpath import project_tools

with project_tools():
from helpers.copier import finish_task
from helpers.git import ensure_git
from helpers.git import list_untracked
from helpers.pre_commit import run_pre_commit
from helpers.venv import ensure_project_venv

# Globs for files that should not be regenerated during updates if deleted.
# This functionality can be removed once _copier_conf.operation is available.
Expand Down
22 changes: 22 additions & 0 deletions tasks/task_helpers/pythonpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
from contextlib import contextmanager
from pathlib import Path


@contextmanager
def prepend_path(path):
sys.path.insert(0, str(path))
try:
yield
finally:
try:
sys.path.remove(str(path))
except ValueError:
pass


@contextmanager
def project_tools():
tools_path = Path(__file__).parent.parent.parent / "project/tools"
with prepend_path(tools_path):
yield

0 comments on commit 6541e6b

Please sign in to comment.