gmyth-stream/server/0.2/plugins/transcoders/mencoder_lib/mythtv.py
author melunko
Tue Aug 21 16:02:24 2007 +0100 (2007-08-21)
branchtrunk
changeset 814 8a6b143057d7
parent 687 6c22199ff33e
permissions -rw-r--r--
[svn r820] Added gmyth_scheduler_add_exception() function to remove only one schedule when all occurrences options has been used.
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@664
    10
def _setup_mythfilename(self):
morphbr@664
    11
    # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
morphbr@664
    12
    try:
morphbr@664
    13
        _mysql = self.args["input"].split("@")[0].split(":")
morphbr@664
    14
    except IndexError, e:
morphbr@664
    15
        _mysql = ["mythtv", "mythtv"]
morphbr@664
    16
morphbr@676
    17
    try:
morphbr@676
    18
        _args = self.args["input"].split("@")[1].split(":")
morphbr@676
    19
    except IndexError, e:
morphbr@676
    20
        _args = self.args["input"].split(":")
morphbr@664
    21
morphbr@664
    22
    gmyth_dict = {}
morphbr@664
    23
    gmyth_dict["mysql"] = _mysql
morphbr@664
    24
    gmyth_dict["backend"] = _args[0]
morphbr@664
    25
    gmyth_dict["port"] = _args[1].split("/", 1)[0]
morphbr@664
    26
morphbr@664
    27
    _tmp_file = _args[1].split("/", 1)[1]
morphbr@664
    28
morphbr@664
    29
    if _tmp_file.find("channel") >= 0:
morphbr@664
    30
        gmyth_dict["kind"] = "c"
morphbr@664
    31
        gmyth_dict["cfile"] = _tmp_file.split("=")[1]
morphbr@664
    32
    else:
morphbr@664
    33
        gmyth_dict["kind"] = "f"
morphbr@664
    34
        gmyth_dict["cfile"] = _tmp_file
morphbr@664
    35
morphbr@664
    36
    self.args["input"] = "-"
morphbr@664
    37
    return gmyth_dict
morphbr@664
    38
# _setup_mythfilename
morphbr@664
    39
morphbr@653
    40
def _setup_mythfile(err):
morphbr@687
    41
    size = err.readline().split("Size:")[1]
morphbr@653
    42
    flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
morphbr@653
    43
    fcntl.fcntl(err, fcntl.F_SETFL, flags)
morphbr@653
    44
    return size
morphbr@653
    45
# _setup_mythfile
morphbr@653
    46
morphbr@653
    47
def _setup_gmythcat(self):
morphbr@653
    48
    gmyth_cat = utils.which("gmyth-cat")
morphbr@717
    49
    if self.args.has_key("gmyth-cat"):
morphbr@717
    50
        return [ utils.which("gmyth-cat"),
morphbr@717
    51
                 "-h", self.args["gmyth-cat"]["backend"],
morphbr@717
    52
                 "-p", self.args["gmyth-cat"]["port"],
morphbr@717
    53
                 "-" + self.args["gmyth-cat"]["kind"],
morphbr@717
    54
                 self.args["gmyth-cat"]["cfile"]
morphbr@717
    55
                 ]
morphbr@717
    56
    else:
morphbr@717
    57
        self.log.error("URI error")
morphbr@717
    58
        return []
morphbr@653
    59
# _setup_gmythcat
morphbr@653
    60
morphbr@653
    61
def start_myth(self, outfd):
morphbr@653
    62
    opts = _setup_gmythcat(self)
morphbr@653
    63
    try:
morphbr@653
    64
        self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
morphbr@653
    65
                                      stderr=subprocess.PIPE,
morphbr@653
    66
                                      close_fds=True)
morphbr@653
    67
    except Exception, e:
morphbr@653
    68
        self.log.error("Error executing gmyth-cat: %s" % e)
morphbr@653
    69
        return False
morphbr@653
    70
morphbr@653
    71
    if not self._run_mencoder(input=self.gmyth.stdout,
morphbr@653
    72
                              output=subprocess.PIPE):
morphbr@653
    73
        return False
morphbr@653
    74
morphbr@664
    75
    if self.args["gmyth-cat"]["kind"] == "f":
morphbr@653
    76
        try:
morphbr@653
    77
            size = _setup_mythfile(self.gmyth.stderr)
morphbr@653
    78
            self.log.debug("Size of file: %s" % size)
morphbr@653
    79
        except Exception, e:
morphbr@653
    80
            self.log.error("Problems getting size of file: %s" % e)
morphbr@653
    81
            return False
morphbr@653
    82
morphbr@653
    83
    try:
morphbr@653
    84
        while self.proc and self.proc.poll() == None:
morphbr@653
    85
            r, w, x = select([self.gmyth.stderr, self.proc.stdout],
morphbr@653
    86
                             [], [], 0)
morphbr@653
    87
            if self.proc.stdout in r:
morphbr@653
    88
                d = self.proc.stdout.read(4096)
morphbr@653
    89
                outfd.write(d)
morphbr@653
    90
morphbr@653
    91
            if self.gmyth.stderr in r:
morphbr@653
    92
                partial = self.gmyth.stderr.read(50).split("\n")[-2]
morphbr@653
    93
                if partial != "":
morphbr@653
    94
                    self.status = utils.progress_bar(self.log,
morphbr@653
    95
                                                     int(partial),
morphbr@653
    96
                                                     int(size), 50)
morphbr@653
    97
morphbr@653
    98
    except IndexError, e:
morphbr@653
    99
        pass
morphbr@653
   100
    except Exception, e:
morphbr@653
   101
        self.log.error("Problems handling data: %s" % e)
morphbr@653
   102
        return False
morphbr@653
   103
morphbr@653
   104
    self.log.info("Finished sending data")
morphbr@653
   105
    return True
morphbr@653
   106
# _start_myth()