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@653: def _setup_mythfile(err): morphbr@653: size = err.read(20).split("\n")[0].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@653: return [ utils.which("gmyth-cat"), morphbr@653: "-h", self.args["gmyth-cat"][0], morphbr@653: "-p", self.args["gmyth-cat"][1], morphbr@653: "-" + self.args["gmyth-cat"][2], morphbr@653: self.args["gmyth-cat"][3] morphbr@653: ] 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@653: if self.args["gmyth-cat"][2] == "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()