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@653
|
49 |
return [ utils.which("gmyth-cat"),
|
morphbr@664
|
50 |
"-h", self.args["gmyth-cat"]["backend"],
|
morphbr@664
|
51 |
"-p", self.args["gmyth-cat"]["port"],
|
morphbr@664
|
52 |
"-" + self.args["gmyth-cat"]["kind"],
|
morphbr@664
|
53 |
self.args["gmyth-cat"]["cfile"]
|
morphbr@653
|
54 |
]
|
morphbr@653
|
55 |
# _setup_gmythcat
|
morphbr@653
|
56 |
|
morphbr@653
|
57 |
def start_myth(self, outfd):
|
morphbr@653
|
58 |
opts = _setup_gmythcat(self)
|
morphbr@653
|
59 |
try:
|
morphbr@653
|
60 |
self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
|
morphbr@653
|
61 |
stderr=subprocess.PIPE,
|
morphbr@653
|
62 |
close_fds=True)
|
morphbr@653
|
63 |
except Exception, e:
|
morphbr@653
|
64 |
self.log.error("Error executing gmyth-cat: %s" % e)
|
morphbr@653
|
65 |
return False
|
morphbr@653
|
66 |
|
morphbr@653
|
67 |
if not self._run_mencoder(input=self.gmyth.stdout,
|
morphbr@653
|
68 |
output=subprocess.PIPE):
|
morphbr@653
|
69 |
return False
|
morphbr@653
|
70 |
|
morphbr@664
|
71 |
if self.args["gmyth-cat"]["kind"] == "f":
|
morphbr@653
|
72 |
try:
|
morphbr@653
|
73 |
size = _setup_mythfile(self.gmyth.stderr)
|
morphbr@653
|
74 |
self.log.debug("Size of file: %s" % size)
|
morphbr@653
|
75 |
except Exception, e:
|
morphbr@653
|
76 |
self.log.error("Problems getting size of file: %s" % e)
|
morphbr@653
|
77 |
return False
|
morphbr@653
|
78 |
|
morphbr@653
|
79 |
try:
|
morphbr@653
|
80 |
while self.proc and self.proc.poll() == None:
|
morphbr@653
|
81 |
r, w, x = select([self.gmyth.stderr, self.proc.stdout],
|
morphbr@653
|
82 |
[], [], 0)
|
morphbr@653
|
83 |
if self.proc.stdout in r:
|
morphbr@653
|
84 |
d = self.proc.stdout.read(4096)
|
morphbr@653
|
85 |
outfd.write(d)
|
morphbr@653
|
86 |
|
morphbr@653
|
87 |
if self.gmyth.stderr in r:
|
morphbr@653
|
88 |
partial = self.gmyth.stderr.read(50).split("\n")[-2]
|
morphbr@653
|
89 |
if partial != "":
|
morphbr@653
|
90 |
self.status = utils.progress_bar(self.log,
|
morphbr@653
|
91 |
int(partial),
|
morphbr@653
|
92 |
int(size), 50)
|
morphbr@653
|
93 |
|
morphbr@653
|
94 |
except IndexError, e:
|
morphbr@653
|
95 |
pass
|
morphbr@653
|
96 |
except Exception, e:
|
morphbr@653
|
97 |
self.log.error("Problems handling data: %s" % e)
|
morphbr@653
|
98 |
return False
|
morphbr@653
|
99 |
|
morphbr@653
|
100 |
self.log.info("Finished sending data")
|
morphbr@653
|
101 |
return True
|
morphbr@653
|
102 |
# _start_myth()
|