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