Skip to content

Commit

Permalink
Merge pull request #98 from saritasa-nest/feature/make-sure-django-ma…
Browse files Browse the repository at this point in the history
…nage-sets-up-settings

Make sure that `DJANGO_SETTINGS_MODULE` is set for manage invocations
  • Loading branch information
TheSuperiorStanislav authored Aug 14, 2024
2 parents a623458 + 10c8942 commit 4b06b87
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ We follow [Semantic Versions](https://semver.org/).
- Make `alembic.wait_for_database` as task
- Make `docker.up` check for compose file
- Make `pytest.run`, `celery.run` use `docker.up`
- Make sure that `DJANGO_SETTINGS_MODULE` is set for manage invocations

## 1.1.1

Expand Down
2 changes: 1 addition & 1 deletion saritasa_invocations/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PythonSettings:
"""Settings for python module."""

entry: str = "python"
docker_service: str = "web"
docker_service: str = "app"
docker_service_params: str = "--rm"
mypy_entry: str = "-m mypy"
pytest_entry: str = "-m pytest"
Expand Down
10 changes: 8 additions & 2 deletions saritasa_invocations/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def wait_for_database(context: invoke.Context) -> None:
python.run(
context,
command=(
f"{config.django.manage_file_path} " "wait_for_database --stable 0"
f"{config.django.manage_file_path} wait_for_database --stable 0"
),
env={
"DJANGO_SETTINGS_MODULE": config.django.settings_path,
},
)
wait_for_database._called = True # type: ignore

Expand All @@ -35,7 +38,7 @@ def wait_for_database(context: invoke.Context) -> None:
def manage(
context: invoke.Context,
command: str,
docker_params: str | None = None,
docker_params: str = "",
watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
) -> None:
"""Run `manage.py` command.
Expand All @@ -58,6 +61,9 @@ def manage(
docker_params=docker_params,
command=f"{config.django.manage_file_path} {command}",
watchers=watchers,
env={
"DJANGO_SETTINGS_MODULE": config.django.settings_path,
},
)


Expand Down
11 changes: 9 additions & 2 deletions saritasa_invocations/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def buildpack(

def docker_compose_run(
context: invoke.Context,
params: str | None,
container: str,
command: str,
params: str = "",
watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
env: dict[str, str] | None = None,
) -> None:
"""Run ``command`` using docker-compose.
Expand All @@ -61,11 +62,17 @@ def docker_compose_run(
container: Name of container to start
command: Command to run in started container
watchers: Automated responders to command
env: environmental variables for run
"""
compose_cmd = _config.Config.from_context(context).docker.compose_cmd
env_params = " ".join(
f"--env {env_key}={value}" for env_key, value in (env or {}).items()
)
context.run(
command=f"{compose_cmd} run {params or ''} {container} {command}",
command=(
f"{compose_cmd} run {params} {env_params} {container} {command}"
),
watchers=watchers,
)

Expand Down
9 changes: 9 additions & 0 deletions saritasa_invocations/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def run_docker(
command: str,
params: str | None = None,
watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
env: dict[str, str] | None = None,
) -> None:
"""Run command in `python` container."""
config = _config.Config.from_context(context)
Expand All @@ -49,6 +50,7 @@ def run_docker(
container=config.python.docker_service,
command=command,
watchers=watchers,
env=env,
)


Expand All @@ -57,6 +59,7 @@ def run_docker_python(
command: str,
params: str | None = None,
watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
env: dict[str, str] | None = None,
) -> None:
"""Run command using docker python interpreter."""
config = _config.Config.from_context(context)
Expand All @@ -65,19 +68,22 @@ def run_docker_python(
params=params,
command=f"{config.python.entry} {command}",
watchers=watchers,
env=env,
)


def run_local_python(
context: invoke.Context,
command: str,
watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
env: dict[str, str] | None = None,
) -> None:
"""Run command using local python interpreter."""
config = _config.Config.from_context(context)
context.run(
command=f"{config.python.entry} {command}",
watchers=watchers,
env=env,
)


Expand All @@ -87,6 +93,7 @@ def run(
command: str,
docker_params: str | None = None,
watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
env: dict[str, str] | None = None,
) -> None:
"""Execute python command."""
match get_python_env():
Expand All @@ -95,11 +102,13 @@ def run(
context=context,
command=command,
watchers=watchers,
env=env,
)
case PythonEnv.DOCKER:
run_docker_python(
context=context,
command=command,
params=docker_params,
watchers=watchers,
env=env,
)

0 comments on commit 4b06b87

Please sign in to comment.