# HG changeset patch # User morphbr # Date 1180563278 -3600 # Node ID f5f7abc760aad1262ef699031fbaa710335f0696 # Parent c397a899490f0e04c313e4c60c23c395e0e0014f [svn r729] * GMyth-Streamer - More bug fixes ;-) - Coding style fixes - Inserted request_handler file diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/gms.py --- a/gmyth-stream/server/0.3/gms.py Wed May 30 19:53:13 2007 +0100 +++ b/gmyth-stream/server/0.3/gms.py Wed May 30 23:14:38 2007 +0100 @@ -21,6 +21,9 @@ "\t%(message)s"), datefmt="%Y-%m-%d %H:%M:%S") +if not os.path.exists(".transcoded"): + os.mkdir(".transcoded") + pd = os.path.join("plugins", "transcoders") load_plugins_transcoders(pd) serve_forever() diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/lib/file_handler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmyth-stream/server/0.3/lib/file_handler.py Wed May 30 23:14:38 2007 +0100 @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +__author__ = "Artur Duque de Souza" +__author_email__ = "artur.souza@indt.org.br" +__license__ = "GPL" +__version__ = "0.1" + +import os +import pickle +import lib.utils as utils + +from stat import * + +__all__ = ("FileList", "list_media_files") + + +class TranscodedFile(object): + """This class creates and reads information about transcoded files.""" + opts = {} + + def __init__(self, filename, args): + print filename + if filename == "" or not os.path.exists(filename): + self.opts = args.copy() + self.opts["original"] = self.opts.pop("input", "").split("://")[1] + self.opts["original_mtime"] = os.stat( + self.opts["original"])[ST_MTIME] + + name = os.path.splitext(os.path.basename(self.opts["outfile"]))[0] + output = open(".transcoded/%s.dat" % name, "wb") + # dumps data using the highest protocol + pickle.dump(self.opts, output, -1) + output.close() + else: + name = os.path.splitext(os.path.basename(filename))[0] + pkl_file = open(".transcoded/%s.dat" % name, "rb") + self.opts = pickle.load(pkl_file) + # __init__() + +# TranscodedFile + + +class FileList(list): + """Class to hold file's list - reimplements str and repr.""" + def __str__(self): + ret = "" + if len(self) > 0: + for item in self: + ret = ret + " %s" % item + return ret + # __str__() + + def __repr__(self): + return self.__str__() + # __repr__() + +# FileList + +def list_media_files(directory, file_list): + """Show all the media files with extension defined in the var 'ext' + that are in the directory, appending each one to 'file_list'.""" + ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov'] + for root, dirs, files in os.walk(directory): + for name in files: + if os.path.splitext(name)[1].strip(".") in ext: + media = os.path.join(root,name) + if media not in file_list: + file_list.append(os.path.join(root,name)) + + return True +# list_media_files() diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/lib/request_handler.py --- a/gmyth-stream/server/0.3/lib/request_handler.py Wed May 30 19:53:13 2007 +0100 +++ b/gmyth-stream/server/0.3/lib/request_handler.py Wed May 30 23:14:38 2007 +0100 @@ -3,20 +3,22 @@ __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza" __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br" __license__ = "GPL" -__version__ = "0.4" +__version__ = "0.3" import os +import cgi +import socket +import logging +import urlparse import threading import SocketServer import BaseHTTPServer -import socket -import urlparse -import cgi + import lib.utils as utils -import logging +import lib.file_handler as files +import lib.transcoder as transcoder from log import Log -import lib.transcoder as transcoder __all__ = ("RequestHandler") @@ -48,35 +50,32 @@ def do_dispatch(self, body): self.url = self.path - pieces = urlparse.urlparse(self.path) self.path = pieces[2] self.query = cgi.parse_qs(pieces[4]) - if self.path == "/": - self.serve_main(body) - elif self.path == "/shutdown.do": - self.serve_shutdown(body) - elif self.path == "/stop-transcoder.do": - self.serve_stop_transcoder(body) - elif self.path == "/status.do": - self.serve_status(body) - elif self.path == "/version.do": - self.serve_version(body) - elif self.path == "/new_id.do": - self.serve_new_id(body) - elif self.path == "/get_log.do": - self.serve_get_log(body) - elif self.path == "/get_all_log.do": - self.serve_get_all_log(body) - elif self.path == "/stream.do": - self.serve_stream(body) - else: + url = { + "/": self.serve_main, + "/shutdown.do": self.serve_shutdown, + "/stop-transcoder.do": self.serve_stop_transcoder, + "/status.do": self.serve_status, + "/version.do": self.serve_version, + "/new_id.do": self.serve_new_id, + "/get_log.do": self.serve_get_log, + "/get_all_log.do": self.serve_get_all_log, + "/stream.do": self.serve_stream, + "/list.do": self.serve_list, + } + + try: + url[self.path](body) + except KeyError, e: action = self.query.get("action", None) if action and "stream.do" in action: self.serve_stream(body) else: self.send_error(404, "File not found") + # do_dispatch() @@ -94,10 +93,10 @@ ret = "" for name, url in self.menu.items(): ret += utils.getHTML("menu", {"name": name, "url": url}) - return ret # _nav_items() + def serve_main(self, body): self.send_response(200) self.send_header("Content-Type", "text/html") @@ -107,6 +106,7 @@ self.wfile.write(utils.getHTML("index", {"menu": self._nav_items()})) # serve_main() + def serve_version(self, body): self.send_response(200) self.send_header("Content-Type", "text/html") @@ -114,6 +114,7 @@ self.end_headers() if body: self.wfile.write("Version: %s" % __version__) + # serve_version def serve_shutdown(self, body): @@ -127,6 +128,24 @@ # serve_shutdown() + def serve_list(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + directories = self.query.get("directory", None) + if body and directories: + file_list = [] + for directory in directories[0].split(";"): + files.list_media_files(directory, file_list) + + output = files.FileList(map(lambda x, y: x+y, file_list, + ["
"]*len(file_list))) + self.wfile.write(utils.getHTML("file_list", {"file_list": output})) + + # serve_list() + + def serve_stop_all_transcoders(self, body): self.send_response(200) self.send_header("Content-Type", "text/html") @@ -134,7 +153,8 @@ self.end_headers() if body: self.server.stop_transcoders() - self.wfile.write(utils.getHTML("stop_all", {"menu": self._nav_items()})) + self.wfile.write(utils.getHTML("stop_all", + {"menu": self._nav_items()})) # serve_stop_all_transcoders() @@ -186,11 +206,16 @@ if body: tl = self.server.get_transcoders() - if not tl: + if not tl and not self.query.get("tid", None): running = "

No running transcoder.

\n" stopall = "" stopone = "" + elif self.query.get("tid", None) and not tl: + tid = int(self.query.get("tid")[0]) + stat = self.transcoders_log.get_status(tid) + self.wfile.write("Running: %s" % stat) + elif self.query.get("tid", None): req_tid = int(self.query.get("tid")[0]) for transcoder, request in tl: @@ -261,7 +286,7 @@ if self.query.get("tid", None): tid = int(self.query.get("tid")[0]) stat = self.transcoders_log.get_status(tid) - self.wfile.write("%s" % stat) + self.wfile.write("Status: %s" % stat) else: stat = self.transcoders_log.get_status() for rtid, status in stat.iteritems(): diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/lib/utils.py --- a/gmyth-stream/server/0.3/lib/utils.py Wed May 30 19:53:13 2007 +0100 +++ b/gmyth-stream/server/0.3/lib/utils.py Wed May 30 23:14:38 2007 +0100 @@ -15,7 +15,8 @@ log = logging.getLogger("gms.utils") -__all__ = ("which", "load_plugins", "PluginSet", "getHTML") +__all__ = ("which", "load_plugins", "PluginSet", "getHTML", + "progress_bar", "create_tid", "list_media_files") def which(app): """Function to implement which(1) unix command""" @@ -165,7 +166,7 @@ return "
  • %s
  • \n" % opt # _create_html_item -def progress_bar(log, value, max, barsize): +def progress_bar(value, max, barsize): """Creates and displays a progressbar. By OSantana""" chars = int(value * barsize / float(max)) percent = int((value / float(max)) * 100) @@ -186,3 +187,5 @@ tid = last_tid + 1 return tid # create_id() + + diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py --- a/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py Wed May 30 19:53:13 2007 +0100 +++ b/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py Wed May 30 23:14:38 2007 +0100 @@ -71,8 +71,6 @@ self.log.debug(self.tid, "stdout %s" % progress) if (progress.find ("PROGRESS") >= 0): self.status = progress.split (":")[1] - else: - self.log.info(self.tid, "Process exit") except Exception, e: self.log.error(self.tid, "Problems handling data: %s" % e) return False diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/plugins/transcoders/mencoder.py --- a/gmyth-stream/server/0.3/plugins/transcoders/mencoder.py Wed May 30 19:53:13 2007 +0100 +++ b/gmyth-stream/server/0.3/plugins/transcoders/mencoder.py Wed May 30 23:14:38 2007 +0100 @@ -14,6 +14,7 @@ import lib.utils as utils import lib.server as server +import lib.file_handler as files import plugins.transcoders.mencoder_lib.mythtv as mythtv from select import select @@ -120,6 +121,11 @@ self._arg_append(args, self._setup_video()) self._arg_append(args, "-really-quiet") + + if self.args["outfile"] != "-": + self.args["outfile"] = ".transcoded/%s" % ( + os.path.basename(self.args["outfile"])) + self._arg_append(args, "-o %s" % self.args["outfile"]) self._arg_append(args, "2>%s" % os.devnull) # _setup_args() @@ -160,7 +166,7 @@ self._setup_filename() self._setup_mencoder_opts(self.mencoder_opts) except Exception, e: - self.log.error(self.tid, e) + self.log.error(self.tid, "Error: %s" % e) # __init__() @@ -177,15 +183,15 @@ def _start_outfile(self, outfd): finished = False - # fix this (not necessary) - outfd.write("OK") - # Configuring stdin try: - _stdin = open(self.args["input"]) - size = int(os.path.getsize(self.args["input"])) + filename = self.args["input"].split("://")[1] + _stdin = open(filename) + size = int(os.path.getsize(filename)) except Exception, e: - self.log.error(self.tid, "Mencoder stdin setup error: %s" % e) + self.log.error(self.tid, "Error: Mencoder stdin"\ + " setup error: %s" % e) + outfd.write("Error: Mencoder stdin setup error: %s" %e) return False self.status = 0 @@ -207,8 +213,7 @@ os.write(stdw, data_in) total_read += 4096 d = stdout.read(4096) - self.status = utils.progress_bar(self.log, - int(total_read), + self.status = utils.progress_bar(int(total_read), int(size), 50) else: finished = True @@ -218,11 +223,12 @@ d = stdout.read(4096) except Exception, e: - self.log.error(self.tid, "Problems handling data: %s" % e) + self.log.error(self.tid, "Error: %s" % e) self.stop() return False - self.log.info(self.tid, "%s: Finished sending data to client" % repr(self)) + files.TranscodedFile("", self.args) + self.log.info(self.tid, "OK: Done") return True # _start_outfile() @@ -236,10 +242,10 @@ d = self.proc.stdout.read(1024) outfd.write(d) except Exception, e: - self.log.error(self.tid, "Problems handling data: %s" % e) + self.log.error(self.tid, "Error: %s" % e) return False - self.log.info(self.tid, "%s: Finished sending data to client" % repr(self)) + self.log.info(self.tid, "OK: Done") return True # _start() @@ -248,7 +254,7 @@ self.proc = subprocess.Popen(self.mencoder_opts, stdin=input, stdout=output, close_fds=True) except Exception, e: - self.log.error(self.tid, "Error executing mencoder: %s" % e) + self.log.error(self.tid, "Error: Mencoder: %s" % e) return False return True @@ -275,7 +281,8 @@ self.stop() if not ret: - self.log.error(self.tid, "Problems while starting streaming.") + self.log.error(self.tid, "Error: Problems while "\ + "starting streaming.") return ret # start() diff -r c397a899490f -r f5f7abc760aa gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py --- a/gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py Wed May 30 19:53:13 2007 +0100 +++ b/gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py Wed May 30 23:14:38 2007 +0100 @@ -54,7 +54,7 @@ self.args["gmyth-cat"]["cfile"] ] else: - self.log.error(self.tid, "URI error") + self.log.error(self.tid, "Error: URI error") return [] # _setup_gmythcat @@ -65,7 +65,7 @@ stderr=subprocess.PIPE, close_fds=True) except Exception, e: - self.log.error(self.tid, "Error executing gmyth-cat: %s" % e) + self.log.error(self.tid, "Error: gmyth-cat: %s" % e) return False if not self._run_mencoder(input=self.gmyth.stdout, @@ -75,9 +75,10 @@ if self.args["gmyth-cat"]["kind"] == "f": try: size = _setup_mythfile(self.gmyth.stderr) - self.log.debug(self.tid, "Size of file: %s" % size) + self.log.debug(self.tid, "Info: Size of file: %s" % size) except Exception, e: - self.log.error(self.tid, "Problems getting size of file: %s" % e) + self.log.error(self.tid, "Error: Problems getting size of"\ + " file: %s" % e) return False try: @@ -91,16 +92,15 @@ if self.gmyth.stderr in r: partial = self.gmyth.stderr.read(50).split("\n")[-2] if partial != "": - self.status = utils.progress_bar(self.log, - int(partial), + self.status = utils.progress_bar(int(partial), int(size), 50) except IndexError, e: pass except Exception, e: - self.log.error(self.tid, "Problems handling data: %s" % e) + self.log.error(self.tid, "Error: %s" % e) return False - self.log.info(self.tid, "Finished sending data") + self.log.info(self.tid, "OK: Done") return True # _start_myth()