gmyth-stream/server/0.2/plugins/transcoders/mencoder_lib/mythtv.py
author morphbr
Tue May 15 18:25:19 2007 +0100 (2007-05-15)
branchtrunk
changeset 653 3433df0d6ae3
child 664 464d715ddb7e
permissions -rw-r--r--
[svn r659] * GMyth-Stream
- created mencoder_lib to cleanup code
- fixed progress bar function
- get status by task
morphbr@653
     1
import os
morphbr@653
     2
import subprocess
morphbr@653
     3
import fcntl
morphbr@653
     4
morphbr@653
     5
import lib.utils as utils
morphbr@653
     6
import lib.server as server
morphbr@653
     7
morphbr@653
     8
from select import select
morphbr@653
     9
morphbr@653
    10
def _setup_mythfile(err):
morphbr@653
    11
    size = err.read(20).split("\n")[0].split("Size:")[1]
morphbr@653
    12
    flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
morphbr@653
    13
    fcntl.fcntl(err, fcntl.F_SETFL, flags)
morphbr@653
    14
    return size
morphbr@653
    15
# _setup_mythfile
morphbr@653
    16
morphbr@653
    17
def _setup_gmythcat(self):
morphbr@653
    18
    gmyth_cat = utils.which("gmyth-cat")
morphbr@653
    19
    return [ utils.which("gmyth-cat"),
morphbr@653
    20
            "-h", self.args["gmyth-cat"][0],
morphbr@653
    21
            "-p", self.args["gmyth-cat"][1],
morphbr@653
    22
            "-" + self.args["gmyth-cat"][2],
morphbr@653
    23
            self.args["gmyth-cat"][3]
morphbr@653
    24
           ]
morphbr@653
    25
# _setup_gmythcat
morphbr@653
    26
morphbr@653
    27
def start_myth(self, outfd):
morphbr@653
    28
    opts = _setup_gmythcat(self)
morphbr@653
    29
    try:
morphbr@653
    30
        self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
morphbr@653
    31
                                      stderr=subprocess.PIPE,
morphbr@653
    32
                                      close_fds=True)
morphbr@653
    33
    except Exception, e:
morphbr@653
    34
        self.log.error("Error executing gmyth-cat: %s" % e)
morphbr@653
    35
        return False
morphbr@653
    36
morphbr@653
    37
    if not self._run_mencoder(input=self.gmyth.stdout,
morphbr@653
    38
                              output=subprocess.PIPE):
morphbr@653
    39
        return False
morphbr@653
    40
morphbr@653
    41
    if self.args["gmyth-cat"][2] == "f":
morphbr@653
    42
        try:
morphbr@653
    43
            size = _setup_mythfile(self.gmyth.stderr)
morphbr@653
    44
            self.log.debug("Size of file: %s" % size)
morphbr@653
    45
        except Exception, e:
morphbr@653
    46
            self.log.error("Problems getting size of file: %s" % e)
morphbr@653
    47
            return False
morphbr@653
    48
morphbr@653
    49
    try:
morphbr@653
    50
        while self.proc and self.proc.poll() == None:
morphbr@653
    51
            r, w, x = select([self.gmyth.stderr, self.proc.stdout],
morphbr@653
    52
                             [], [], 0)
morphbr@653
    53
            if self.proc.stdout in r:
morphbr@653
    54
                d = self.proc.stdout.read(4096)
morphbr@653
    55
                outfd.write(d)
morphbr@653
    56
morphbr@653
    57
            if self.gmyth.stderr in r:
morphbr@653
    58
                partial = self.gmyth.stderr.read(50).split("\n")[-2]
morphbr@653
    59
                if partial != "":
morphbr@653
    60
                    self.status = utils.progress_bar(self.log,
morphbr@653
    61
                                                     int(partial),
morphbr@653
    62
                                                     int(size), 50)
morphbr@653
    63
morphbr@653
    64
    except IndexError, e:
morphbr@653
    65
        pass
morphbr@653
    66
    except Exception, e:
morphbr@653
    67
        self.log.error("Problems handling data: %s" % e)
morphbr@653
    68
        return False
morphbr@653
    69
morphbr@653
    70
    self.log.info("Finished sending data")
morphbr@653
    71
    return True
morphbr@653
    72
# _start_myth()