forked from cycodehq/cycode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_executor.py
More file actions
33 lines (25 loc) · 941 Bytes
/
Copy pathshell_executor.py
File metadata and controls
33 lines (25 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import subprocess
from typing import List, Optional, Union
import click
from cycode.cyclient import logger
_SUBPROCESS_DEFAULT_TIMEOUT_SEC = 60
def shell(
command: Union[str, List[str]], timeout: int = _SUBPROCESS_DEFAULT_TIMEOUT_SEC, execute_in_shell=False
) -> Optional[str]:
logger.debug(f'Executing shell command: {command}')
try:
result = subprocess.run(
command,
timeout=timeout,
shell=execute_in_shell,
check=True,
capture_output=True,
)
return result.stdout.decode('UTF-8').strip()
except subprocess.CalledProcessError as e:
logger.debug(f'Error occurred while running shell command. Exception: {e.stderr}')
except subprocess.TimeoutExpired:
raise click.Abort(f'Command "{command}" timed out')
except Exception as e:
raise click.ClickException(f'Unhandled exception: {e}')
return None