gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py
author morphbr
Tue May 29 21:24:48 2007 +0100 (2007-05-29)
branchtrunk
changeset 718 3fbcd3d9b2d1
child 723 f5f7abc760aa
permissions -rw-r--r--
[svn r724] * GMyth-Streamer version 0.3 released
- Improved Log architecture;
- Creation of a history for the transcoder's actions
- Creation of an id for each transcoder instanciated
- Also wrapps default actions for python's default logger

- Created new functions to make use of this new Log architecture;
- serve_new_id
- serve_get_log
- serve_get_all_log

- _Lot_ of small bug fixes;

- Inserted header for all files;

- Splited files with too many lines (more than 1 class per file)
in more files;
morphbr@718
     1
import os
morphbr@718
     2
import subprocess
morphbr@718
     3
import fcntl
morphbr@718
     4
morphbr@718
     5
import lib.utils as utils
morphbr@718
     6
import lib.server as server
morphbr@718
     7
morphbr@718
     8
from select import select
morphbr@718
     9
morphbr@718
    10
def _setup_mythfilename(self):
morphbr@718
    11
    # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
morphbr@718
    12
    try:
morphbr@718
    13
        _mysql = self.args["input"].split("@")[0].split(":")
morphbr@718
    14
    except IndexError, e:
morphbr@718
    15
        _mysql = ["mythtv", "mythtv"]
morphbr@718
    16
morphbr@718
    17
    try:
morphbr@718
    18
        _args = self.args["input"].split("@")[1].split(":")
morphbr@718
    19
    except IndexError, e:
morphbr@718
    20
        _args = self.args["input"].split(":")
morphbr@718
    21
morphbr@718
    22
    gmyth_dict = {}
morphbr@718
    23
    gmyth_dict["mysql"] = _mysql
morphbr@718
    24
    gmyth_dict["backend"] = _args[0]
morphbr@718
    25
    gmyth_dict["port"] = _args[1].split("/", 1)[0]
morphbr@718
    26
morphbr@718
    27
    _tmp_file = _args[1].split("/", 1)[1]
morphbr@718
    28
morphbr@718
    29
    if _tmp_file.find("channel") >= 0:
morphbr@718
    30
        gmyth_dict["kind"] = "c"
morphbr@718
    31
        gmyth_dict["cfile"] = _tmp_file.split("=")[1]
morphbr@718
    32
    else:
morphbr@718
    33
        gmyth_dict["kind"] = "f"
morphbr@718
    34
        gmyth_dict["cfile"] = _tmp_file
morphbr@718
    35
morphbr@718
    36
    self.args["input"] = "-"
morphbr@718
    37
    return gmyth_dict
morphbr@718
    38
# _setup_mythfilename
morphbr@718
    39
morphbr@718
    40
def _setup_mythfile(err):
morphbr@718
    41
    size = err.readline().split("Size:")[1]
morphbr@718
    42
    flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
morphbr@718
    43
    fcntl.fcntl(err, fcntl.F_SETFL, flags)
morphbr@718
    44
    return size
morphbr@718
    45
# _setup_mythfile
morphbr@718
    46
morphbr@718
    47
def _setup_gmythcat(self):
morphbr@718
    48
    gmyth_cat = utils.which("gmyth-cat")
morphbr@718
    49
    if self.args.has_key("gmyth-cat"):
morphbr@718
    50
        return [ utils.which("gmyth-cat"),
morphbr@718
    51
                 "-h", self.args["gmyth-cat"]["backend"],
morphbr@718
    52
                 "-p", self.args["gmyth-cat"]["port"],
morphbr@718
    53
                 "-" + self.args["gmyth-cat"]["kind"],
morphbr@718
    54
                 self.args["gmyth-cat"]["cfile"]
morphbr@718
    55
                 ]
morphbr@718
    56
    else:
morphbr@718
    57
        self.log.error(self.tid, "URI error")
morphbr@718
    58
        return []
morphbr@718
    59
# _setup_gmythcat
morphbr@718
    60
morphbr@718
    61
def start_myth(self, outfd):
morphbr@718
    62
    opts = _setup_gmythcat(self)
morphbr@718
    63
    try:
morphbr@718
    64
        self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
morphbr@718
    65
                                      stderr=subprocess.PIPE,
morphbr@718
    66
                                      close_fds=True)
morphbr@718
    67
    except Exception, e:
morphbr@718
    68
        self.log.error(self.tid, "Error executing gmyth-cat: %s" % e)
morphbr@718
    69
        return False
morphbr@718
    70
morphbr@718
    71
    if not self._run_mencoder(input=self.gmyth.stdout,
morphbr@718
    72
                              output=subprocess.PIPE):
morphbr@718
    73
        return False
morphbr@718
    74
morphbr@718
    75
    if self.args["gmyth-cat"]["kind"] == "f":
morphbr@718
    76
        try:
morphbr@718
    77
            size = _setup_mythfile(self.gmyth.stderr)
morphbr@718
    78
            self.log.debug(self.tid, "Size of file: %s" % size)
morphbr@718
    79
        except Exception, e:
morphbr@718
    80
            self.log.error(self.tid, "Problems getting size of file: %s" % e)
morphbr@718
    81
            return False
morphbr@718
    82
morphbr@718
    83
    try:
morphbr@718
    84
        while self.proc and self.proc.poll() == None:
morphbr@718
    85
            r, w, x = select([self.gmyth.stderr, self.proc.stdout],
morphbr@718
    86
                             [], [], 0)
morphbr@718
    87
            if self.proc.stdout in r:
morphbr@718
    88
                d = self.proc.stdout.read(4096)
morphbr@718
    89
                outfd.write(d)
morphbr@718
    90
morphbr@718
    91
            if self.gmyth.stderr in r:
morphbr@718
    92
                partial = self.gmyth.stderr.read(50).split("\n")[-2]
morphbr@718
    93
                if partial != "":
morphbr@718
    94
                    self.status = utils.progress_bar(self.log,
morphbr@718
    95
                                                     int(partial),
morphbr@718
    96
                                                     int(size), 50)
morphbr@718
    97
morphbr@718
    98
    except IndexError, e:
morphbr@718
    99
        pass
morphbr@718
   100
    except Exception, e:
morphbr@718
   101
        self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
   102
        return False
morphbr@718
   103
morphbr@718
   104
    self.log.info(self.tid, "Finished sending data")
morphbr@718
   105
    return True
morphbr@718
   106
# _start_myth()