morphbr@718: import os morphbr@718: import subprocess morphbr@718: import fcntl morphbr@718: morphbr@718: import lib.utils as utils morphbr@718: import lib.server as server morphbr@718: morphbr@718: from select import select morphbr@718: morphbr@718: def _setup_mythfilename(self): morphbr@718: # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv morphbr@718: try: morphbr@718: _mysql = self.args["input"].split("@")[0].split(":") morphbr@718: except IndexError, e: morphbr@718: _mysql = ["mythtv", "mythtv"] morphbr@718: morphbr@718: try: morphbr@718: _args = self.args["input"].split("@")[1].split(":") morphbr@718: except IndexError, e: morphbr@718: _args = self.args["input"].split(":") morphbr@718: morphbr@718: gmyth_dict = {} morphbr@718: gmyth_dict["mysql"] = _mysql morphbr@718: gmyth_dict["backend"] = _args[0] morphbr@718: gmyth_dict["port"] = _args[1].split("/", 1)[0] morphbr@718: morphbr@718: _tmp_file = _args[1].split("/", 1)[1] morphbr@718: morphbr@718: if _tmp_file.find("channel") >= 0: morphbr@718: gmyth_dict["kind"] = "c" morphbr@718: gmyth_dict["cfile"] = _tmp_file.split("=")[1] morphbr@718: else: morphbr@718: gmyth_dict["kind"] = "f" morphbr@718: gmyth_dict["cfile"] = _tmp_file morphbr@718: morphbr@718: self.args["input"] = "-" morphbr@718: return gmyth_dict morphbr@718: # _setup_mythfilename morphbr@718: morphbr@718: def _setup_mythfile(err): morphbr@718: size = err.readline().split("Size:")[1] morphbr@718: flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK morphbr@718: fcntl.fcntl(err, fcntl.F_SETFL, flags) morphbr@718: return size morphbr@718: # _setup_mythfile morphbr@718: morphbr@718: def _setup_gmythcat(self): morphbr@718: gmyth_cat = utils.which("gmyth-cat") morphbr@718: if self.args.has_key("gmyth-cat"): morphbr@718: return [ utils.which("gmyth-cat"), morphbr@718: "-h", self.args["gmyth-cat"]["backend"], morphbr@718: "-p", self.args["gmyth-cat"]["port"], morphbr@718: "-" + self.args["gmyth-cat"]["kind"], morphbr@718: self.args["gmyth-cat"]["cfile"] morphbr@718: ] morphbr@718: else: morphbr@723: self.log.error(self.tid, "Error: URI error") morphbr@718: return [] morphbr@718: # _setup_gmythcat morphbr@718: morphbr@718: def start_myth(self, outfd): morphbr@718: opts = _setup_gmythcat(self) morphbr@718: try: morphbr@718: self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, morphbr@718: stderr=subprocess.PIPE, morphbr@718: close_fds=True) morphbr@718: except Exception, e: morphbr@723: self.log.error(self.tid, "Error: gmyth-cat: %s" % e) morphbr@718: return False morphbr@718: morphbr@718: if not self._run_mencoder(input=self.gmyth.stdout, morphbr@718: output=subprocess.PIPE): morphbr@718: return False morphbr@718: morphbr@718: if self.args["gmyth-cat"]["kind"] == "f": morphbr@718: try: morphbr@718: size = _setup_mythfile(self.gmyth.stderr) morphbr@723: self.log.debug(self.tid, "Info: Size of file: %s" % size) morphbr@718: except Exception, e: morphbr@723: self.log.error(self.tid, "Error: Problems getting size of"\ morphbr@723: " file: %s" % e) morphbr@728: outfd.write("Error: Problems getting size of file: %s" % e) morphbr@718: return False morphbr@718: morphbr@728: outfd.write("OK ") morphbr@728: morphbr@718: try: morphbr@718: while self.proc and self.proc.poll() == None: morphbr@718: r, w, x = select([self.gmyth.stderr, self.proc.stdout], morphbr@718: [], [], 0) morphbr@718: if self.proc.stdout in r: morphbr@765: d = self.proc.stdout.read(4096) morphbr@718: outfd.write(d) morphbr@718: morphbr@718: if self.gmyth.stderr in r: morphbr@748: partial = self.gmyth.stderr.readline() morphbr@718: if partial != "": morphbr@723: self.status = utils.progress_bar(int(partial), morphbr@718: int(size), 50) morphbr@718: morphbr@718: except IndexError, e: morphbr@718: pass morphbr@718: except Exception, e: morphbr@723: self.log.error(self.tid, "Error: %s" % e) morphbr@718: return False morphbr@718: morphbr@723: self.log.info(self.tid, "OK: Done") morphbr@718: return True morphbr@718: # _start_myth()