Skip to content

Commit ef77028

Browse files
committed
Add debug statements to docker command execution
1 parent b99010a commit ef77028

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

dependency_resolver/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def resolve_host_dependencies(
2828
Returns:
2929
JSON string containing all discovered dependencies
3030
"""
31-
executor = HostExecutor()
31+
executor = HostExecutor(debug=debug)
3232
orchestrator = Orchestrator(debug=debug, skip_system_scope=skip_system_scope, venv_path=venv_path)
3333
dependencies = orchestrator.resolve_dependencies(executor, working_dir)
3434
formatter = OutputFormatter(debug=debug)
@@ -59,7 +59,7 @@ def resolve_docker_dependencies(
5959
Returns:
6060
JSON string containing all discovered dependencies
6161
"""
62-
executor = DockerExecutor(container_identifier)
62+
executor = DockerExecutor(container_identifier, debug=debug)
6363
orchestrator = Orchestrator(debug=debug, skip_system_scope=skip_system_scope, venv_path=venv_path)
6464
dependencies = orchestrator.resolve_dependencies(executor, working_dir, only_container_info)
6565
formatter = OutputFormatter(debug=debug)
@@ -98,7 +98,7 @@ def resolve_docker_dependencies_as_dict(
9898
if not container_identifier:
9999
raise ValueError("Container identifier is required")
100100

101-
executor = DockerExecutor(container_identifier)
101+
executor = DockerExecutor(container_identifier, debug=debug)
102102
orchestrator = Orchestrator(debug=debug, skip_system_scope=skip_system_scope, venv_path=venv_path)
103103
return orchestrator.resolve_dependencies(executor, working_dir, only_container_info)
104104

@@ -129,11 +129,11 @@ def resolve_dependencies_as_dict(
129129
"""
130130
executor: EnvironmentExecutor
131131
if environment_type == "host":
132-
executor = HostExecutor()
132+
executor = HostExecutor(debug=debug)
133133
elif environment_type == "docker":
134134
if not environment_identifier:
135135
raise ValueError("Docker environment requires container identifier")
136-
executor = DockerExecutor(environment_identifier)
136+
executor = DockerExecutor(environment_identifier, debug=debug)
137137
else:
138138
raise ValueError(f"Unsupported environment type: {environment_type}")
139139

dependency_resolver/__main__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,17 @@ def validate_arguments(
9797
sys.exit(1)
9898

9999

100-
def create_executor(environment_type: str, environment_identifier: str | None) -> EnvironmentExecutor:
100+
def create_executor(
101+
environment_type: str, environment_identifier: str | None, debug: bool = False
102+
) -> EnvironmentExecutor:
101103
"""Create executor based on environment type."""
102104
if environment_type == "host":
103-
return HostExecutor()
105+
return HostExecutor(debug=debug)
104106
elif environment_type == "docker":
105107
if environment_identifier is None:
106108
print("Error: Docker environment requires container identifier", file=sys.stderr)
107109
sys.exit(1)
108-
return DockerExecutor(environment_identifier)
110+
return DockerExecutor(environment_identifier, debug=debug)
109111
else:
110112
print(f"Error: Unsupported environment type: {environment_type}", file=sys.stderr)
111113
sys.exit(1)
@@ -118,7 +120,7 @@ def main() -> None:
118120
validate_arguments(args.environment_type, args.environment_identifier, args.only_container_info)
119121

120122
try:
121-
executor = create_executor(args.environment_type, args.environment_identifier)
123+
executor = create_executor(args.environment_type, args.environment_identifier, debug=args.debug)
122124
orchestrator = Orchestrator(
123125
debug=args.debug, skip_system_scope=args.skip_system_scope, venv_path=args.venv_path
124126
)

dependency_resolver/detectors/pip_detector.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _find_venv_path(self, executor: EnvironmentExecutor, working_dir: Optional[s
166166
# Fallback: System-wide search (only in container environments)
167167
if not isinstance(executor, HostExecutor):
168168
if self.debug:
169-
print("DEBUG: pip_detector performing system-wide pyvenv.cfg search in container environment")
169+
print("pip_detector performing system-wide pyvenv.cfg search in container environment")
170170

171171
stdout, _, exit_code = executor.execute_command(
172172
"find /opt /home /usr/local -name 'pyvenv.cfg' -type f 2>/dev/null | head -1"
@@ -176,12 +176,14 @@ def _find_venv_path(self, executor: EnvironmentExecutor, working_dir: Optional[s
176176
pyvenv_cfg_path = stdout.strip()
177177
venv_path = os.path.dirname(pyvenv_cfg_path)
178178
if self.debug:
179-
print(f"DEBUG: pip_detector found pyvenv.cfg at {pyvenv_cfg_path}, venv_path: {venv_path}")
179+
print(f"pip_detector found pyvenv.cfg at {pyvenv_cfg_path}, venv_path: {venv_path}")
180180
self._cached_venv_path = venv_path
181181
self._venv_path_searched = True
182182
return venv_path
183183

184184
# No venv found
185+
if self.debug:
186+
print("pip_detector did not found any venv location!")
185187
self._cached_venv_path = None
186188
self._venv_path_searched = True
187189
return None

dependency_resolver/executors/docker_executor.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class DockerExecutor(EnvironmentExecutor):
1616
preserving original exceptions with 'from' clause for debugging.
1717
"""
1818

19-
def __init__(self, container_identifier: str):
19+
def __init__(self, container_identifier: str, debug: bool = False):
2020
"""Initialize Docker executor."""
2121
if docker is None:
2222
raise ImportError(
2323
"Docker package is required for Docker functionality. Please install it with: pip install docker"
2424
)
2525

26+
self.debug = debug
27+
2628
try:
2729
self.client = docker.from_env()
2830
self.container = self.client.containers.get(container_identifier)
@@ -32,6 +34,9 @@ def __init__(self, container_identifier: str):
3234
f"Container '{container_identifier}' is not running (status: {self.container.status})"
3335
)
3436

37+
if self.debug:
38+
print(f"Connected to Docker container: {container_identifier}")
39+
3540
except docker.errors.NotFound as exc:
3641
raise RuntimeError(f"Container '{container_identifier}' not found") from exc
3742
except docker.errors.APIError as e:
@@ -42,6 +47,10 @@ def execute_command(self, command: str, working_dir: Optional[str] = None) -> tu
4247
4348
Returns actual command exit code on success, or 1 for execution environment failures.
4449
"""
50+
if self.debug:
51+
workdir_info = f" (workdir: {working_dir})" if working_dir else ""
52+
print(f"Executing docker command: sh -c '{command}'{workdir_info}")
53+
4554
try:
4655
# First, try with sh
4756
result = self.container.exec_run(
@@ -50,6 +59,9 @@ def execute_command(self, command: str, working_dir: Optional[str] = None) -> tu
5059
stdout = result.output.decode("utf-8") if result.output else ""
5160
stderr = ""
5261

62+
if self.debug:
63+
print(f"Docker command exit code: {result.exit_code}")
64+
5365
return stdout, stderr, result.exit_code
5466

5567
except docker.errors.APIError as e:
@@ -63,16 +75,26 @@ def execute_command(self, command: str, working_dir: Optional[str] = None) -> tu
6375

6476
def _execute_command_direct(self, command: str, working_dir: Optional[str] = None) -> tuple[str, str, int]:
6577
"""Fallback: execute simple commands directly without shell."""
78+
if self.debug:
79+
workdir_info = f" (workdir: {working_dir})" if working_dir else ""
80+
print(f"Fallback: executing docker command directly: {command}{workdir_info}")
81+
6682
try:
6783
# Handle only the simple cases we actually use
6884
cmd_parts = DockerExecutor._parse_simple_command(command)
6985
if not cmd_parts:
7086
return "", f"Command too complex for direct execution (no shell available): {command}", 1
7187

88+
if self.debug:
89+
print(f"Parsed command parts: {cmd_parts}")
90+
7291
result = self.container.exec_run(cmd=cmd_parts, stdout=True, stderr=True, tty=False, workdir=working_dir)
7392
stdout = result.output.decode("utf-8") if result.output else ""
7493
stderr = ""
7594

95+
if self.debug:
96+
print(f"Direct execution exit code: {result.exit_code}")
97+
7698
return stdout, stderr, result.exit_code
7799

78100
except docker.errors.APIError as e:

dependency_resolver/executors/host_executor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
class HostExecutor(EnvironmentExecutor):
99
"""Executor for running commands on the host system."""
1010

11+
def __init__(self, debug: bool = False):
12+
"""Initialize Host executor."""
13+
self.debug = debug
14+
1115
def execute_command(self, command: str, working_dir: Optional[str] = None) -> tuple[str, str, int]:
1216
"""Execute a command on the host system.
1317

0 commit comments

Comments
 (0)