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