@@ -22,26 +22,36 @@ def get_project_root():
22
22
return project_root
23
23
24
24
def execute_command (command , log_file_path = None ):
25
- logger .info ("Executing command: %s" , command )
26
- # Open the process with stderr merged to stdout
25
+ logger .info ("Executing command: %s" , command .strip ())
26
+
27
+ # Open the log file if provided
28
+ log_file = log_file_path .open ('a' , encoding = 'utf-8' ) if log_file_path else None
29
+
30
+ # Launch process with combined stdout and stderr streams, and line buffering enabled.
27
31
process = subprocess .Popen (
28
32
command ,
29
33
shell = True ,
30
34
executable = '/bin/bash' ,
31
35
stdout = subprocess .PIPE ,
32
36
stderr = subprocess .STDOUT ,
33
37
text = True ,
34
- bufsize = 1 # Line-buffered output
38
+ encoding = 'utf-8' ,
39
+ bufsize = 1 # line buffered
35
40
)
36
-
37
- # Capture output in real time
41
+
42
+ # Iterate over each line as it becomes available
38
43
with process .stdout :
39
44
for line in iter (process .stdout .readline , '' ):
40
- logger . info ( line . strip ())
41
- if log_file_path :
42
- with open ( log_file_path , 'a' , encoding = 'utf-8' ) as log_file :
45
+ if line :
46
+ logger . info ( line . strip ())
47
+ if log_file :
43
48
log_file .write (line )
44
- process .wait ()
49
+ log_file .flush () # flush immediately for real-time logging
50
+ process .wait () # wait for the process to complete
51
+
52
+ if log_file :
53
+ log_file .close ()
54
+
45
55
return process
46
56
47
57
def command_prepare (command ):
0 commit comments