gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py
author morphbr
Wed May 30 23:14:38 2007 +0100 (2007-05-30)
branchtrunk
changeset 723 f5f7abc760aa
parent 718 3fbcd3d9b2d1
child 728 6e54ca4a4e9a
permissions -rw-r--r--
[svn r729] * GMyth-Streamer
- More bug fixes ;-)
- Coding style fixes
- Inserted request_handler file
     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_mythfilename(self):
    11     # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
    12     try:
    13         _mysql = self.args["input"].split("@")[0].split(":")
    14     except IndexError, e:
    15         _mysql = ["mythtv", "mythtv"]
    16 
    17     try:
    18         _args = self.args["input"].split("@")[1].split(":")
    19     except IndexError, e:
    20         _args = self.args["input"].split(":")
    21 
    22     gmyth_dict = {}
    23     gmyth_dict["mysql"] = _mysql
    24     gmyth_dict["backend"] = _args[0]
    25     gmyth_dict["port"] = _args[1].split("/", 1)[0]
    26 
    27     _tmp_file = _args[1].split("/", 1)[1]
    28 
    29     if _tmp_file.find("channel") >= 0:
    30         gmyth_dict["kind"] = "c"
    31         gmyth_dict["cfile"] = _tmp_file.split("=")[1]
    32     else:
    33         gmyth_dict["kind"] = "f"
    34         gmyth_dict["cfile"] = _tmp_file
    35 
    36     self.args["input"] = "-"
    37     return gmyth_dict
    38 # _setup_mythfilename
    39 
    40 def _setup_mythfile(err):
    41     size = err.readline().split("Size:")[1]
    42     flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
    43     fcntl.fcntl(err, fcntl.F_SETFL, flags)
    44     return size
    45 # _setup_mythfile
    46 
    47 def _setup_gmythcat(self):
    48     gmyth_cat = utils.which("gmyth-cat")
    49     if self.args.has_key("gmyth-cat"):
    50         return [ utils.which("gmyth-cat"),
    51                  "-h", self.args["gmyth-cat"]["backend"],
    52                  "-p", self.args["gmyth-cat"]["port"],
    53                  "-" + self.args["gmyth-cat"]["kind"],
    54                  self.args["gmyth-cat"]["cfile"]
    55                  ]
    56     else:
    57         self.log.error(self.tid, "Error: URI error")
    58         return []
    59 # _setup_gmythcat
    60 
    61 def start_myth(self, outfd):
    62     opts = _setup_gmythcat(self)
    63     try:
    64         self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
    65                                       stderr=subprocess.PIPE,
    66                                       close_fds=True)
    67     except Exception, e:
    68         self.log.error(self.tid, "Error: gmyth-cat: %s" % e)
    69         return False
    70 
    71     if not self._run_mencoder(input=self.gmyth.stdout,
    72                               output=subprocess.PIPE):
    73         return False
    74 
    75     if self.args["gmyth-cat"]["kind"] == "f":
    76         try:
    77             size = _setup_mythfile(self.gmyth.stderr)
    78             self.log.debug(self.tid, "Info: Size of file: %s" % size)
    79         except Exception, e:
    80             self.log.error(self.tid, "Error: Problems getting size of"\
    81                            " file: %s" % e)
    82             return False
    83 
    84     try:
    85         while self.proc and self.proc.poll() == None:
    86             r, w, x = select([self.gmyth.stderr, self.proc.stdout],
    87                              [], [], 0)
    88             if self.proc.stdout in r:
    89                 d = self.proc.stdout.read(4096)
    90                 outfd.write(d)
    91 
    92             if self.gmyth.stderr in r:
    93                 partial = self.gmyth.stderr.read(50).split("\n")[-2]
    94                 if partial != "":
    95                     self.status = utils.progress_bar(int(partial),
    96                                                      int(size), 50)
    97 
    98     except IndexError, e:
    99         pass
   100     except Exception, e:
   101         self.log.error(self.tid, "Error: %s" % e)
   102         return False
   103 
   104     self.log.info(self.tid, "OK: Done")
   105     return True
   106 # _start_myth()