-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_command.py
More file actions
64 lines (57 loc) · 2.58 KB
/
run_command.py
File metadata and controls
64 lines (57 loc) · 2.58 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import subprocess
import time
def run_bash_command(command: str, logger, windows: bool = False, blastn : bool = False):
"""
Runs a command in the shell and logs the command, execution time, stdout, and stderr.
Args:
command (str): The command to be executed.
logger: The logger object used for logging.
windows (bool, optional): Specifies whether the environment is Windows (True) or UNIX (False).
Defaults to False.
Returns:
tuple: A tuple containing the result object and the elapsed time in seconds.
"""
if windows :
# Adjust the command for Windows environment
if not blastn :
full_command = ("wsl " + command).replace('\\', '/')
else :
full_command =command.replace('\\', '/')
else:
# Use "bash -l -c" to run the command within the context of the bash profile on UNIX systems
full_command = f'bash -l -c "{command}"'
logger.info(f"Running command: {full_command}")
start_time = time.time()
result = subprocess.run(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
elapsed_time = time.time() - start_time
logger.info(f"Command '{full_command}' took {elapsed_time:.2f} seconds to run.")
if result.stdout:
logger.info(result.stdout)
print(result.stdout)
if result.stderr:
logger.error("Error: " + result.stderr)
print(result.stderr)
return result, elapsed_time
def run_command(command: str, logger, windows: bool = False):
"""
Runs a command in the shell and logs the command, execution time, stdout, and stderr.
Args:
command (str): The command to be executed.
logger: The logger object used for logging.
windows (bool, optional): Specifies whether to run the command in Windows Subsystem for Linux (windows).
Defaults to False.
Returns:
tuple: A tuple containing the result object and the elapsed time in seconds.
"""
if windows :
command = ("wsl " + command).replace('\\','/')
logger.info(f"Running command: {command}")
start_time = time.time()
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
elapsed_time = time.time() - start_time
logger.info(f"Command '{command}' took {elapsed_time:.2f} seconds to run.")
if result.stdout:
logger.info(result.stdout)
if result.stderr:
logger.error("Error: " + result.stderr)
return result, elapsed_time