sskorol commited on
Commit
1556b80
·
verified ·
1 Parent(s): 7315cd5

Shell tool improvements

Browse files

Made shell output more concise.

Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -29,7 +29,6 @@ def execute_shell_command(
29
  Dictionary containing stdout, stderr, and return_code
30
  """
31
  try:
32
- # Run the command with specified parameters
33
  process = subprocess.Popen(
34
  command,
35
  stdout=subprocess.PIPE,
@@ -39,27 +38,34 @@ def execute_shell_command(
39
  universal_newlines=True
40
  )
41
 
42
- # Get command output with optional timeout
43
  stdout, stderr = process.communicate(timeout=timeout)
44
  return_code = process.returncode
45
 
46
- return {
47
- "stdout": stdout,
48
- "stderr": stderr,
49
- "return_code": str(return_code) # Convert to string for consistency
50
- }
 
 
 
 
 
 
 
 
51
 
52
  except subprocess.TimeoutExpired:
53
  process.kill()
54
  return {
55
  "stdout": "",
56
- "stderr": f"Command timed out after {timeout} seconds",
57
  "return_code": "-1"
58
  }
59
  except Exception as e:
60
  return {
61
  "stdout": "",
62
- "stderr": f"Error executing command: {str(e)}",
63
  "return_code": "-1"
64
  }
65
 
 
29
  Dictionary containing stdout, stderr, and return_code
30
  """
31
  try:
 
32
  process = subprocess.Popen(
33
  command,
34
  stdout=subprocess.PIPE,
 
38
  universal_newlines=True
39
  )
40
 
 
41
  stdout, stderr = process.communicate(timeout=timeout)
42
  return_code = process.returncode
43
 
44
+ # Format output more concisely
45
+ if return_code == 0:
46
+ return {
47
+ "stdout": stdout.strip(),
48
+ "stderr": "",
49
+ "return_code": "0"
50
+ }
51
+ else:
52
+ return {
53
+ "stdout": "",
54
+ "stderr": stderr.strip(),
55
+ "return_code": str(return_code)
56
+ }
57
 
58
  except subprocess.TimeoutExpired:
59
  process.kill()
60
  return {
61
  "stdout": "",
62
+ "stderr": f"Timeout after {timeout}s",
63
  "return_code": "-1"
64
  }
65
  except Exception as e:
66
  return {
67
  "stdout": "",
68
+ "stderr": str(e),
69
  "return_code": "-1"
70
  }
71