Skip to content

Commit

Permalink
add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertdeng123 committed Nov 8, 2024
1 parent 8c3c767 commit 7345c49
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ NOTE: service-name is an optional parameter. If not provided, devservices will a
- `devservices list-dependencies <service-name>`: List all dependencies for a service and whether they are enabled/disabled.
- `devservices update` Update devservices to the latest version.
- `devservices purge`: Purge the local devservices cache.

## Debug Mode

To enable debug mode, set the `DEBUG` environment variable to `true`:

```
export DEBUG=true
```
1 change: 1 addition & 0 deletions devservices/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"extensions.partialClone": "true",
"core.sparseCheckout": "true",
}
DEBUG = os.getenv("DEBUG", "false").lower() == "true"

DOCKER_COMPOSE_DOWNLOAD_URL = "https://github.com/docker/compose/releases/download"
DEVSERVICES_DOWNLOAD_URL = "https://github.com/getsentry/devservices/releases/download"
Expand Down
8 changes: 7 additions & 1 deletion devservices/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from devservices.configs.service_config import load_service_config_from_file
from devservices.configs.service_config import RemoteConfig
from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEBUG
from devservices.constants import DEPENDENCY_CONFIG_VERSION
from devservices.constants import DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR
Expand Down Expand Up @@ -436,4 +437,9 @@ def _has_remote_config(remote_config: RemoteConfig | None) -> TypeGuard[RemoteCo
def _run_command(
cmd: list[str], cwd: str, stdout: int | TextIO | None = subprocess.DEVNULL
) -> None:
subprocess.run(cmd, cwd=cwd, check=True, stdout=stdout, stderr=subprocess.DEVNULL)
if DEBUG:
subprocess.run(cmd, cwd=cwd, check=True)

Check warning on line 441 in devservices/utils/dependencies.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/dependencies.py#L441

Added line #L441 was not covered by tests
else:
subprocess.run(
cmd, cwd=cwd, check=True, stdout=stdout, stderr=subprocess.DEVNULL
)
61 changes: 57 additions & 4 deletions devservices/utils/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import platform
import re
import subprocess
import sys
from collections.abc import Callable
from typing import cast

from packaging import version

from devservices.configs.service_config import load_service_config_from_file
from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEBUG
from devservices.constants import DEPENDENCY_CONFIG_VERSION
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR_KEY
Expand Down Expand Up @@ -269,11 +271,62 @@ def run_docker_compose_command(
cmd_outputs = []
for cmd in docker_compose_commands:
try:
cmd_outputs.append(
subprocess.run(
cmd, check=True, capture_output=True, text=True, env=current_env
if DEBUG:
# Use Popen for real-time output streaming when debug is True
process = subprocess.Popen(
cmd,
env=current_env,
stdout=subprocess.PIPE,

Check warning on line 279 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L279

Added line #L279 was not covered by tests
stderr=subprocess.PIPE,
text=True,
bufsize=1,
universal_newlines=True,
)

stdout_data = []
stderr_data = []

# Read stdout and stderr in real-time
while True:

Check warning on line 290 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L289-L290

Added lines #L289 - L290 were not covered by tests
stdout_line = process.stdout.readline() if process.stdout else ""
stderr_line = process.stderr.readline() if process.stderr else ""

if stdout_line:
print(stdout_line.rstrip())

Check warning on line 295 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L293-L295

Added lines #L293 - L295 were not covered by tests
stdout_data.append(stdout_line)
if stderr_line:
print(stderr_line.rstrip(), file=sys.stderr)
stderr_data.append(stderr_line)

if (
not stdout_line

Check warning on line 302 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L297-L302

Added lines #L297 - L302 were not covered by tests
and not stderr_line
and process.poll() is not None

Check warning on line 304 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L304

Added line #L304 was not covered by tests
):
break

if process.returncode != 0:
raise subprocess.CalledProcessError(

Check warning on line 309 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L309

Added line #L309 was not covered by tests
process.returncode,
cmd,
"".join(stdout_data),

Check warning on line 312 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L311-L312

Added lines #L311 - L312 were not covered by tests
"".join(stderr_data),
)

cmd_outputs.append(
subprocess.CompletedProcess(
args=cmd,
returncode=process.returncode,

Check warning on line 319 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L319

Added line #L319 was not covered by tests
stdout="".join(stdout_data),
stderr="".join(stderr_data),
)
)
else:
cmd_outputs.append(
subprocess.run(
cmd, check=True, capture_output=True, text=True, env=current_env
)
)
)
except subprocess.CalledProcessError as e:
raise DockerComposeError(
command=command,
Expand Down

0 comments on commit 7345c49

Please sign in to comment.