[~] Refactor

This commit is contained in:
Siarhei Siniak 2024-03-20 22:33:26 +03:00
parent d08c797ae7
commit b74512eddb

@ -69,6 +69,7 @@ def intercept_output(
transform_callback=None, transform_callback=None,
real_time=None, real_time=None,
timeout=None, timeout=None,
need_lines=None,
): ):
if real_time is None: if real_time is None:
real_time = False real_time = False
@ -80,8 +81,10 @@ def intercept_output(
t1 = select.poll() t1 = select.poll()
t1.register(current_subprocess.stdout, select.POLLIN) t1.register(current_subprocess.stdout, select.POLLIN)
print([current_subprocess, current_subprocess.poll()]) #print([current_subprocess, current_subprocess.poll()])
output = [] output = []
buffer = []
buffer_lines = []
last_data = None last_data = None
@ -100,6 +103,30 @@ def intercept_output(
t4 = current_subprocess.stdout.read(len(t3)) t4 = current_subprocess.stdout.read(len(t3))
last_data = t3 last_data = t3
output.append(t3) output.append(t3)
if need_lines:
buffer.append(t3)
if need_lines:
if len(buffer_lines) > 0:
yield dict(
aggegated=False,
line=buffer_lines[0],
)
del buffer_lines[0]
if b'\n' in t3:
t3_pos = t3.rfind(b'\n')
buffer_lines.extend([
o + b'\n'
for o in b''.join(
buffer[:-1] + [
t3[:t3_pos]
],
).splitlines()
])
del buffer[:-1]
buffer[0] = t3[t3_pos + 1:]
else:
yield dict( yield dict(
data=t3, data=t3,
aggregated=False, aggregated=False,
@ -109,6 +136,8 @@ def intercept_output(
t5 = transform_callback(t3) t5 = transform_callback(t3)
if not t5 is None: if not t5 is None:
t6 = t5 t6 = t5
if len(t6) > 0:
os.write(sys.stdout.fileno(), t6) os.write(sys.stdout.fileno(), t6)
elif real_time: elif real_time:
yield dict( yield dict(
@ -1426,8 +1455,14 @@ def pm_service(argv):
action = None action = None
with subprocess.Popen(['log', 'stream'], stdout=subprocess.PIPE) as p: with subprocess.Popen(['log', 'stream'], stdout=subprocess.PIPE) as p:
while True: for chunk in intercept_output(
line = p.stdout.readline().decode('utf-8') p,
return_aggregated=False,
need_lines=True,
transform_callback=lambda x: b'',
):
line = chunk['line'].decode('utf-8')
#p.stdout.readline().decode('utf-8')
cmd = None cmd = None
if 'powerd' in line: if 'powerd' in line:
cmd = line cmd = line