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 |
self.log.debug("aqui 2 %s" % _args)
|
morphbr@664
|
28 |
|
morphbr@664
|
29 |
_tmp_file = _args[1].split("/", 1)[1]
|
morphbr@664
|
30 |
|
morphbr@664
|
31 |
if _tmp_file.find("channel") >= 0:
|
morphbr@664
|
32 |
gmyth_dict["kind"] = "c"
|
morphbr@664
|
33 |
gmyth_dict["cfile"] = _tmp_file.split("=")[1]
|
morphbr@664
|
34 |
else:
|
morphbr@664
|
35 |
gmyth_dict["kind"] = "f"
|
morphbr@664
|
36 |
gmyth_dict["cfile"] = _tmp_file
|
morphbr@664
|
37 |
|
morphbr@664
|
38 |
self.args["input"] = "-"
|
morphbr@664
|
39 |
return gmyth_dict
|
morphbr@664
|
40 |
# _setup_mythfilename
|
morphbr@664
|
41 |
|
morphbr@653
|
42 |
def _setup_mythfile(err):
|
morphbr@653
|
43 |
size = err.read(20).split("\n")[0].split("Size:")[1]
|
morphbr@653
|
44 |
flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
|
morphbr@653
|
45 |
fcntl.fcntl(err, fcntl.F_SETFL, flags)
|
morphbr@653
|
46 |
return size
|
morphbr@653
|
47 |
# _setup_mythfile
|
morphbr@653
|
48 |
|
morphbr@653
|
49 |
def _setup_gmythcat(self):
|
morphbr@653
|
50 |
gmyth_cat = utils.which("gmyth-cat")
|
morphbr@653
|
51 |
return [ utils.which("gmyth-cat"),
|
morphbr@664
|
52 |
"-h", self.args["gmyth-cat"]["backend"],
|
morphbr@664
|
53 |
"-p", self.args["gmyth-cat"]["port"],
|
morphbr@664
|
54 |
"-" + self.args["gmyth-cat"]["kind"],
|
morphbr@664
|
55 |
self.args["gmyth-cat"]["cfile"]
|
morphbr@653
|
56 |
]
|
morphbr@653
|
57 |
# _setup_gmythcat
|
morphbr@653
|
58 |
|
morphbr@653
|
59 |
def start_myth(self, outfd):
|
morphbr@653
|
60 |
opts = _setup_gmythcat(self)
|
morphbr@653
|
61 |
try:
|
morphbr@653
|
62 |
self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
|
morphbr@653
|
63 |
stderr=subprocess.PIPE,
|
morphbr@653
|
64 |
close_fds=True)
|
morphbr@653
|
65 |
except Exception, e:
|
morphbr@653
|
66 |
self.log.error("Error executing gmyth-cat: %s" % e)
|
morphbr@653
|
67 |
return False
|
morphbr@653
|
68 |
|
morphbr@653
|
69 |
if not self._run_mencoder(input=self.gmyth.stdout,
|
morphbr@653
|
70 |
output=subprocess.PIPE):
|
morphbr@653
|
71 |
return False
|
morphbr@653
|
72 |
|
morphbr@664
|
73 |
if self.args["gmyth-cat"]["kind"] == "f":
|
morphbr@653
|
74 |
try:
|
morphbr@653
|
75 |
size = _setup_mythfile(self.gmyth.stderr)
|
morphbr@653
|
76 |
self.log.debug("Size of file: %s" % size)
|
morphbr@653
|
77 |
except Exception, e:
|
morphbr@653
|
78 |
self.log.error("Problems getting size of file: %s" % e)
|
morphbr@653
|
79 |
return False
|
morphbr@653
|
80 |
|
morphbr@653
|
81 |
try:
|
morphbr@653
|
82 |
while self.proc and self.proc.poll() == None:
|
morphbr@653
|
83 |
r, w, x = select([self.gmyth.stderr, self.proc.stdout],
|
morphbr@653
|
84 |
[], [], 0)
|
morphbr@653
|
85 |
if self.proc.stdout in r:
|
morphbr@653
|
86 |
d = self.proc.stdout.read(4096)
|
morphbr@653
|
87 |
outfd.write(d)
|
morphbr@653
|
88 |
|
morphbr@653
|
89 |
if self.gmyth.stderr in r:
|
morphbr@653
|
90 |
partial = self.gmyth.stderr.read(50).split("\n")[-2]
|
morphbr@653
|
91 |
if partial != "":
|
morphbr@653
|
92 |
self.status = utils.progress_bar(self.log,
|
morphbr@653
|
93 |
int(partial),
|
morphbr@653
|
94 |
int(size), 50)
|
morphbr@653
|
95 |
|
morphbr@653
|
96 |
except IndexError, e:
|
morphbr@653
|
97 |
pass
|
morphbr@653
|
98 |
except Exception, e:
|
morphbr@653
|
99 |
self.log.error("Problems handling data: %s" % e)
|
morphbr@653
|
100 |
return False
|
morphbr@653
|
101 |
|
morphbr@653
|
102 |
self.log.info("Finished sending data")
|
morphbr@653
|
103 |
return True
|
morphbr@653
|
104 |
# _start_myth()
|