Files
whipper-gui/morituri/common/task.py
Samantha Baldwin d1ed80d62a argparse & logging (#92)
* introduce logcommand.Lager, Whipper(); use argparse for whipper image commands, stub logging

* update Lager docstring to mention config.Config()

* make incorrect subcommand and --version work on toplevel command

* migrate accurip show, expand Lager, do not attempt to return from Lager.__init__.

* migrate offset find, add Lager.error

* correct offset find drive symlink handling

* migrate drive

* change Lager.__init__(prog) to arg from kwarg

* but actually

* remove Whipper.usage

* add and use Lager.device_option() context manager

* help I married an axe murderer

* use unified options namespace for entire command tree

* migrate whipper cd without comprehensive config loading

* switch to logging module

- use logging instead of flog for non-extern modules
- use WHIPPER_DEBUG and WHIPPER_LOGFILE env variables

* convert self.log calls to logger.debug

* convert self.error calls to logger.error

* remove log.Loggable, use logger not logging

* Logging conversion continues

- Convert log.* calls to logger.*
- Remove morituri.common.log imports

* remove morituri.common.log from tests

* remove extern/flog, bare minimum Debug conversion

* update README for logging changes

* update soxi to use logging

* refactor Lager for more declarative subcommands

* Refactor Lager.device_option:

- inline into __init__
- throw IOError instead of Exception for missing drives
- remove CommandError checking in rip/main

* rename rip to whipper in rip.main

* convert rip.debug commands

* Rename logcommand.Lager to command.BaseCommand

- remove command.CommandError occurrences
- remove python-command external module

* remove submodules from README, update rclog formatter

* update minor ambiguity in readme for command invocation

* update version number to match setup.py

* remove gitmodules

* update version number in tests as well (boo)

* convert logger.error to logger.critical

* Change morituri.rip to morituri.command

- mv common.command to command.basecommand
- move TEMPLATES used only by rip.cd out of rip.common
- update entry point for command to command.main

* update basecommand documentation

* go pyflaking: import fixing

* replace self.stdout with sys.stdout

* remove BaseCommand.config, alphabetise imports

* convert self.stdXXX leftovers

* convert last getRootCommand to config.Config

* convert last getExceptionMessage's to str

* change musicbrainz useragent to whipper
2016-12-20 23:11:30 +01:00

154 lines
3.5 KiB
Python

# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
import signal
import subprocess
from morituri.extern import asyncsub
from morituri.extern.task import task, gstreamer
import logging
logger = logging.getLogger(__name__)
class SyncRunner(task.SyncRunner):
pass
class LoggableTask(task.Task):
pass
class LoggableMultiSeparateTask(task.MultiSeparateTask):
pass
class GstPipelineTask(gstreamer.GstPipelineTask):
pass
class PopenTask(task.Task):
"""
I am a task that runs a command using Popen.
"""
logCategory = 'PopenTask'
bufsize = 1024
command = None
cwd = None
def start(self, runner):
task.Task.start(self, runner)
try:
self._popen = asyncsub.Popen(self.command,
bufsize=self.bufsize,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True, cwd=self.cwd)
except OSError, e:
import errno
if e.errno == errno.ENOENT:
self.commandMissing()
raise
logger.debug('Started %r with pid %d', self.command,
self._popen.pid)
self.schedule(1.0, self._read, runner)
def _read(self, runner):
try:
read = False
ret = self._popen.recv()
if ret:
logger.debug("read from stdout: %s", ret)
self.readbytesout(ret)
read = True
ret = self._popen.recv_err()
if ret:
logger.debug("read from stderr: %s", ret)
self.readbyteserr(ret)
read = True
# if we read anything, we might have more to read, so
# reschedule immediately
if read and self.runner:
self.schedule(0.0, self._read, runner)
return
# if we didn't read anything, give the command more time to
# produce output
if self._popen.poll() is None and self.runner:
# not finished yet
self.schedule(1.0, self._read, runner)
return
self._done()
except Exception, e:
logger.debug('exception during _read(): %r', str(e))
self.setException(e)
self.stop()
def _done(self):
assert self._popen.returncode is not None, "No returncode"
if self._popen.returncode >= 0:
logger.debug('Return code was %d', self._popen.returncode)
else:
logger.debug('Terminated with signal %d',
-self._popen.returncode)
self.setProgress(1.0)
if self._popen.returncode != 0:
self.failed()
else:
self.done()
self.stop()
return
def abort(self):
logger.debug('Aborting, sending SIGTERM to %d', self._popen.pid)
os.kill(self._popen.pid, signal.SIGTERM)
# self.stop()
def readbytesout(self, bytes):
"""
Called when bytes have been read from stdout.
"""
pass
def readbyteserr(self, bytes):
"""
Called when bytes have been read from stderr.
"""
pass
def done(self):
"""
Called when the command completed successfully.
"""
pass
def failed(self):
"""
Called when the command failed.
"""
pass
def commandMissing(self):
"""
Called when the command is missing.
"""
pass