Trying to adapt a Python 2.4 script for 3, it has a ton of Popen() instances that were working before and now I’m running into no end of issues. Code extract:
# Inspect and log an extract of dmesg
if no_errors_yet:
log_centered_whitespace(' dmesg ')
dmesg = Popen(('dmesg',), stdout=PIPE, stderr=STDOUT)
accumulating = False
for dmesg_line in dmesg.stdout:
if 'new high speed USB device' in dmesg_line:
dmesg_lines = [dmesg_line,]
accumulating = True
continue
if accumulating:
dmesg_lines.append(dmesg_line)
for dmesg_line in dmesg_lines:
log(dmesg_line)
log_exit_status(dmesg, 'dmesg')
# Inspect and log current filesystem status
if no_errors_yet:
log_centered_whitespace(' df ')
df = Popen(('df',), stdout=PIPE, stderr=STDOUT)
for df_line in df.stdout:
log(df_line)
find = df_line.find('/offsites')
if find >= 0 and preexisting_mount_point is None:
preexisting_mount_point = df_line[find:].strip()
if preexisting_mount_point:
log('Information: Found an already-mounted "offsites" mount point',
summary=True)
log('Information: using preexisting %s' % preexisting_mount_point,
summary=True)
log_exit_status(df, 'df')
Traceback (most recent call last):
File "./backup_staging3.py", line 211, in <module>
if 'new high speed USB device' in dmesg_line:
TypeError: a bytes-like object is required, not 'str'
If I try to cast the output as a str
like:
for df_line in str(df.stdout):
further down the line I get this as output from a logging function:
230823 14:22 df
230823 14:22 <
230823 14:22 _
230823 14:22 i
230823 14:22 o
230823 14:22 .
230823 14:22 B
230823 14:22 u
230823 14:22 f
230823 14:22 f
230823 14:22 e
230823 14:22 r
230823 14:22 e
230823 14:22 d
230823 14:22 R
230823 14:22 e
230823 14:22 a
230823 14:22 d
230823 14:22 e
230823 14:22 r
230823 14:22
230823 14:22 n
230823 14:22 a
230823 14:22 m
230823 14:22 e
230823 14:22 =
230823 14:22 6
230823 14:22 >
and there is a function that relies on the return code of the Popen()
object, so I’m not sure how to proceed with this.
def log_exit_status(popen_object: Popen, label, summary=False):
"""Record (and return) an exit status in this run's logs."""
exit_status = popen_object.wait()
if exit_status:
log_centered_whitespace(' %s exit status: %s ' % (label, exit_status), summary=True)
else:
log_centered_whitespace((' %s: ok ' % label), summary=summary)
return exit_status