Skip to content

Commit

Permalink
Provide progress of cdrdao read toc/table to console
Browse files Browse the repository at this point in the history
Forgot to schedule _read after starting cdrdao subprocess
  • Loading branch information
jtl999 authored and JoeLametta committed Dec 13, 2018
1 parent a6d33df commit ce5a8e7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
1 change: 0 additions & 1 deletion whipper/command/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def do(self):
utils.unmount_device(self.device)

# first, read the normal TOC, which is fast
logger.info("reading TOC...")
self.ittoc = self.program.getFastToc(self.runner, self.device)

# already show us some info based on this
Expand Down
87 changes: 51 additions & 36 deletions whipper/program/cdrdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,37 @@
_LEADOUT_RE = re.compile(
r"^Leadout AUDIO\s*[0-9]\s*[0-9]*:[0-9]*:[0-9]*\([0-9]*\)")


class ProgressParser:
tracks = 0
currentTrack = 0
oldline = '' # for leadout/final track number detection

def parse(self, line):
pass
cdrdao_m = _BEGIN_CDRDAO_RE.match(line)

if cdrdao_m:
logger.debug("RE: Begin cdrdao toc-read")

leadout_m = _LEADOUT_RE.match(line)

if leadout_m:
logger.debug("RE: Reached leadout")
last_track_m = _LAST_TRACK_RE.match(self.oldline)
if last_track_m:
self.tracks = last_track_m.group('track')

track_s = _TRACK_RE.search(line)
if track_s:
logger.debug("RE: Began reading track: %d"
% int(track_s.group('track')))
self.currentTrack = int(track_s.group('track'))

crc_s = _CRC_RE.search(line)
if crc_s:
sys.stdout.write("Track %d finished, found %d Q sub-channels with CRC errors\n" % (self.currentTrack, int(crc_s.group('channels'))) )

self.oldline = line


class ReadTOC_Task(task.Task):
Expand All @@ -53,6 +81,9 @@ def __init__(self, device, fast_toc=False, toc_path=None):
self.fast_toc = fast_toc
self.toc_path = toc_path
self._buffer = "" # accumulate characters
self._parser = ProgressParser()

self.fd, self.tocfile = tempfile.mkstemp(suffix=u'.cdrdao.read-toc.whipper.task')

def start(self, runner):
task.Task.start(self, runner)
Expand All @@ -68,39 +99,9 @@ def start(self, runner):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True)
def _read(self, runner):
ret = self._popen.recv_err()
if not ret:
if self._popen.poll() is not None:
self._done()
return
self.schedule(0.01, self._read, runner)
return
self._buffer += ret

# parse buffer into lines if possible, and parse them
if "\n" in self._buffer:
lines = self._buffer.split('\n')
if lines[-1] != "\n":
# last line didn't end yet
self._buffer = lines[-1]
del lines[-1]
else:
self._buffer = ""

for line in lines:
sys.stdout.write("%s\n" % line)

progress = float(num) / float(den)
if progress < 1.0:
self.setProgress(progress)

# 0 does not give us output before we complete, 1.0 gives us output
# too late

self._start_time = time.time()
self.schedule(1.0, self._read, runner)

self.schedule(0.01, self._read, runner)

def _read(self, runner):
ret = self._popen.recv_err()
if not ret:
Expand All @@ -113,7 +114,6 @@ def _read(self, runner):

# parse buffer into lines if possible, and parse them
if "\n" in self._buffer:

lines = self._buffer.split('\n')
if lines[-1] != "\n":
# last line didn't end yet
Expand All @@ -122,14 +122,17 @@ def _read(self, runner):
else:
self._buffer = ""
for line in lines:
sys.stdout.write("%s\n" % line)
self._parser.parse(line)
if (self._parser.currentTrack is not 0 and self._parser.tracks is not 0):
progress = float('%d' % self._parser.currentTrack) / float(self._parser.tracks)
if progress < 1.0:
self.setProgress(progress)

# 0 does not give us output before we complete, 1.0 gives us output
# too late
self.schedule(0.01, self._read, runner)

def _poll(self, runner):

if self._popen.poll() is None:
self.schedule(1.0, self._poll, runner)
return
Expand Down Expand Up @@ -189,3 +192,15 @@ def getCDRDAOVersion():
stopgap morituri-insanity compatibility layer
"""
return version()

def DetectCdr(device):
"""
Return whether cdrdao detects a CD-R for 'device'.
"""
cmd = [CDRDAO, 'disk-info', '-v1', '--device', device]
logger.debug("executing %r", cmd)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
if 'CD-R medium : n/a' in p.stdout.read():
return False
else:
return True

0 comments on commit ce5a8e7

Please sign in to comment.