[svn r729] * GMyth-Streamer trunk
authormorphbr
Wed May 30 23:14:38 2007 +0100 (2007-05-30)
branchtrunk
changeset 723f5f7abc760aa
parent 722 c397a899490f
child 724 64b061775d06
[svn r729] * GMyth-Streamer
- More bug fixes ;-)
- Coding style fixes
- Inserted request_handler file
gmyth-stream/server/0.3/gms.py
gmyth-stream/server/0.3/lib/file_handler.py
gmyth-stream/server/0.3/lib/request_handler.py
gmyth-stream/server/0.3/lib/utils.py
gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
gmyth-stream/server/0.3/plugins/transcoders/mencoder.py
gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py
     1.1 --- a/gmyth-stream/server/0.3/gms.py	Wed May 30 19:53:13 2007 +0100
     1.2 +++ b/gmyth-stream/server/0.3/gms.py	Wed May 30 23:14:38 2007 +0100
     1.3 @@ -21,6 +21,9 @@
     1.4                          "\t%(message)s"),
     1.5                  datefmt="%Y-%m-%d %H:%M:%S")
     1.6  
     1.7 +if not os.path.exists(".transcoded"):
     1.8 +    os.mkdir(".transcoded")
     1.9 +
    1.10  pd = os.path.join("plugins", "transcoders")
    1.11  load_plugins_transcoders(pd)
    1.12  serve_forever()
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/gmyth-stream/server/0.3/lib/file_handler.py	Wed May 30 23:14:38 2007 +0100
     2.3 @@ -0,0 +1,71 @@
     2.4 +#!/usr/bin/env python
     2.5 +
     2.6 +__author__ = "Artur Duque de Souza"
     2.7 +__author_email__ = "artur.souza@indt.org.br"
     2.8 +__license__ = "GPL"
     2.9 +__version__ = "0.1"
    2.10 +
    2.11 +import os
    2.12 +import pickle
    2.13 +import lib.utils as utils
    2.14 +
    2.15 +from stat import *
    2.16 +
    2.17 +__all__ = ("FileList", "list_media_files")
    2.18 +
    2.19 +
    2.20 +class TranscodedFile(object):
    2.21 +    """This class creates and reads information about transcoded files."""
    2.22 +    opts = {}
    2.23 +
    2.24 +    def __init__(self, filename, args):
    2.25 +        print filename
    2.26 +        if filename == "" or not os.path.exists(filename):
    2.27 +            self.opts = args.copy()
    2.28 +            self.opts["original"] = self.opts.pop("input", "").split("://")[1]
    2.29 +            self.opts["original_mtime"] = os.stat(
    2.30 +                self.opts["original"])[ST_MTIME]
    2.31 +
    2.32 +            name = os.path.splitext(os.path.basename(self.opts["outfile"]))[0]
    2.33 +            output = open(".transcoded/%s.dat" % name, "wb")
    2.34 +            # dumps data using the highest protocol
    2.35 +            pickle.dump(self.opts, output, -1)
    2.36 +            output.close()
    2.37 +        else:
    2.38 +            name = os.path.splitext(os.path.basename(filename))[0]
    2.39 +            pkl_file = open(".transcoded/%s.dat" % name, "rb")
    2.40 +            self.opts = pickle.load(pkl_file)
    2.41 +    # __init__()
    2.42 +
    2.43 +# TranscodedFile
    2.44 +
    2.45 +
    2.46 +class FileList(list):
    2.47 +    """Class to hold file's list - reimplements str and repr."""
    2.48 +    def __str__(self):
    2.49 +        ret = ""
    2.50 +        if len(self) > 0:
    2.51 +            for item in self:
    2.52 +                ret = ret + " %s" % item
    2.53 +        return ret
    2.54 +    # __str__()
    2.55 +
    2.56 +    def __repr__(self):
    2.57 +        return self.__str__()
    2.58 +    # __repr__()
    2.59 +
    2.60 +# FileList
    2.61 +
    2.62 +def list_media_files(directory, file_list):
    2.63 +    """Show all the media files with extension defined in the var 'ext'
    2.64 +    that are in the directory, appending each one to 'file_list'."""
    2.65 +    ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
    2.66 +    for root, dirs, files in os.walk(directory):
    2.67 +        for name in files:
    2.68 +            if os.path.splitext(name)[1].strip(".") in ext:
    2.69 +                media = os.path.join(root,name)
    2.70 +                if media not in file_list:
    2.71 +                    file_list.append(os.path.join(root,name))
    2.72 +
    2.73 +    return True
    2.74 +# list_media_files()
     3.1 --- a/gmyth-stream/server/0.3/lib/request_handler.py	Wed May 30 19:53:13 2007 +0100
     3.2 +++ b/gmyth-stream/server/0.3/lib/request_handler.py	Wed May 30 23:14:38 2007 +0100
     3.3 @@ -3,20 +3,22 @@
     3.4  __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
     3.5  __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
     3.6  __license__ = "GPL"
     3.7 -__version__ = "0.4"
     3.8 +__version__ = "0.3"
     3.9  
    3.10  import os
    3.11 +import cgi
    3.12 +import socket
    3.13 +import logging
    3.14 +import urlparse
    3.15  import threading
    3.16  import SocketServer
    3.17  import BaseHTTPServer
    3.18 -import socket
    3.19 -import urlparse
    3.20 -import cgi
    3.21 +
    3.22  import lib.utils as utils
    3.23 -import logging
    3.24 +import lib.file_handler as files
    3.25 +import lib.transcoder as transcoder
    3.26  
    3.27  from log import Log
    3.28 -import lib.transcoder as transcoder
    3.29  
    3.30  __all__ = ("RequestHandler")
    3.31  
    3.32 @@ -48,35 +50,32 @@
    3.33  
    3.34      def do_dispatch(self, body):
    3.35          self.url = self.path
    3.36 -
    3.37          pieces = urlparse.urlparse(self.path)
    3.38          self.path = pieces[2]
    3.39          self.query = cgi.parse_qs(pieces[4])
    3.40  
    3.41 -        if self.path == "/":
    3.42 -            self.serve_main(body)
    3.43 -        elif self.path == "/shutdown.do":
    3.44 -            self.serve_shutdown(body)
    3.45 -        elif self.path == "/stop-transcoder.do":
    3.46 -            self.serve_stop_transcoder(body)
    3.47 -        elif self.path == "/status.do":
    3.48 -            self.serve_status(body)
    3.49 -        elif self.path == "/version.do":
    3.50 -            self.serve_version(body)
    3.51 -        elif self.path == "/new_id.do":
    3.52 -            self.serve_new_id(body)
    3.53 -        elif self.path == "/get_log.do":
    3.54 -            self.serve_get_log(body)
    3.55 -        elif self.path == "/get_all_log.do":
    3.56 -            self.serve_get_all_log(body)
    3.57 -        elif self.path == "/stream.do":
    3.58 -            self.serve_stream(body)
    3.59 -        else:
    3.60 +        url = {
    3.61 +            "/": self.serve_main,
    3.62 +            "/shutdown.do": self.serve_shutdown,
    3.63 +            "/stop-transcoder.do": self.serve_stop_transcoder,
    3.64 +            "/status.do": self.serve_status,
    3.65 +            "/version.do": self.serve_version,
    3.66 +            "/new_id.do": self.serve_new_id,
    3.67 +            "/get_log.do": self.serve_get_log,
    3.68 +            "/get_all_log.do": self.serve_get_all_log,
    3.69 +            "/stream.do": self.serve_stream,
    3.70 +            "/list.do": self.serve_list,
    3.71 +            }
    3.72 +
    3.73 +        try:
    3.74 +            url[self.path](body)
    3.75 +        except KeyError, e:
    3.76              action = self.query.get("action", None)
    3.77              if action and "stream.do" in action:
    3.78                  self.serve_stream(body)
    3.79              else:
    3.80                  self.send_error(404, "File not found")
    3.81 +
    3.82      # do_dispatch()
    3.83  
    3.84  
    3.85 @@ -94,10 +93,10 @@
    3.86          ret = ""
    3.87          for name, url in self.menu.items():
    3.88              ret += utils.getHTML("menu", {"name": name, "url": url})
    3.89 -
    3.90          return ret
    3.91      # _nav_items()
    3.92  
    3.93 +
    3.94      def serve_main(self, body):
    3.95          self.send_response(200)
    3.96          self.send_header("Content-Type", "text/html")
    3.97 @@ -107,6 +106,7 @@
    3.98              self.wfile.write(utils.getHTML("index", {"menu": self._nav_items()}))
    3.99      # serve_main()
   3.100  
   3.101 +
   3.102      def serve_version(self, body):
   3.103          self.send_response(200)
   3.104          self.send_header("Content-Type", "text/html")
   3.105 @@ -114,6 +114,7 @@
   3.106          self.end_headers()
   3.107          if body:
   3.108              self.wfile.write("Version: %s" %  __version__)
   3.109 +    # serve_version
   3.110  
   3.111  
   3.112      def serve_shutdown(self, body):
   3.113 @@ -127,6 +128,24 @@
   3.114      # serve_shutdown()
   3.115  
   3.116  
   3.117 +    def serve_list(self, body):
   3.118 +        self.send_response(200)
   3.119 +        self.send_header("Content-Type", "text/html")
   3.120 +        self.send_header('Connection', 'close')
   3.121 +        self.end_headers()
   3.122 +        directories = self.query.get("directory", None)
   3.123 +        if body and directories:
   3.124 +            file_list = []
   3.125 +            for directory in directories[0].split(";"):
   3.126 +                files.list_media_files(directory, file_list)
   3.127 +
   3.128 +            output = files.FileList(map(lambda x, y: x+y, file_list,
   3.129 +                                        ["<br>"]*len(file_list)))
   3.130 +            self.wfile.write(utils.getHTML("file_list", {"file_list": output}))
   3.131 +
   3.132 +    # serve_list()
   3.133 +
   3.134 +
   3.135      def serve_stop_all_transcoders(self, body):
   3.136          self.send_response(200)
   3.137          self.send_header("Content-Type", "text/html")
   3.138 @@ -134,7 +153,8 @@
   3.139          self.end_headers()
   3.140          if body:
   3.141              self.server.stop_transcoders()
   3.142 -            self.wfile.write(utils.getHTML("stop_all", {"menu": self._nav_items()}))
   3.143 +            self.wfile.write(utils.getHTML("stop_all",
   3.144 +                                           {"menu": self._nav_items()}))
   3.145      # serve_stop_all_transcoders()
   3.146  
   3.147  
   3.148 @@ -186,11 +206,16 @@
   3.149  
   3.150          if body:
   3.151              tl = self.server.get_transcoders()
   3.152 -            if not tl:
   3.153 +            if not tl and not self.query.get("tid", None):
   3.154                  running = "<p>No running transcoder.</p>\n"
   3.155                  stopall = ""
   3.156                  stopone = ""
   3.157  
   3.158 +            elif self.query.get("tid", None) and not tl:
   3.159 +                tid = int(self.query.get("tid")[0])
   3.160 +                stat = self.transcoders_log.get_status(tid)
   3.161 +                self.wfile.write("Running: %s" % stat)
   3.162 +
   3.163              elif self.query.get("tid", None):
   3.164                  req_tid = int(self.query.get("tid")[0])
   3.165                  for transcoder, request in tl:
   3.166 @@ -261,7 +286,7 @@
   3.167              if self.query.get("tid", None):
   3.168                  tid = int(self.query.get("tid")[0])
   3.169                  stat = self.transcoders_log.get_status(tid)
   3.170 -                self.wfile.write("%s" % stat)
   3.171 +                self.wfile.write("Status: %s" % stat)
   3.172              else:
   3.173                  stat = self.transcoders_log.get_status()
   3.174                  for rtid, status in stat.iteritems():
     4.1 --- a/gmyth-stream/server/0.3/lib/utils.py	Wed May 30 19:53:13 2007 +0100
     4.2 +++ b/gmyth-stream/server/0.3/lib/utils.py	Wed May 30 23:14:38 2007 +0100
     4.3 @@ -15,7 +15,8 @@
     4.4  
     4.5  log = logging.getLogger("gms.utils")
     4.6  
     4.7 -__all__ = ("which", "load_plugins", "PluginSet", "getHTML")
     4.8 +__all__ = ("which", "load_plugins", "PluginSet", "getHTML",
     4.9 +           "progress_bar", "create_tid", "list_media_files")
    4.10  
    4.11  def which(app):
    4.12      """Function to implement which(1) unix command"""
    4.13 @@ -165,7 +166,7 @@
    4.14      return "<li>%s</li>\n" % opt
    4.15  # _create_html_item
    4.16  
    4.17 -def progress_bar(log, value, max, barsize):
    4.18 +def progress_bar(value, max, barsize):
    4.19      """Creates and displays a progressbar. By OSantana"""
    4.20      chars = int(value * barsize / float(max))
    4.21      percent = int((value / float(max)) * 100)
    4.22 @@ -186,3 +187,5 @@
    4.23      tid = last_tid + 1
    4.24      return tid
    4.25  # create_id()
    4.26 +
    4.27 +
     5.1 --- a/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py	Wed May 30 19:53:13 2007 +0100
     5.2 +++ b/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py	Wed May 30 23:14:38 2007 +0100
     5.3 @@ -71,8 +71,6 @@
     5.4                      self.log.debug(self.tid, "stdout %s" % progress)
     5.5                      if (progress.find ("PROGRESS") >= 0):
     5.6                          self.status = progress.split (":")[1]
     5.7 -                    else:
     5.8 -			self.log.info(self.tid, "Process exit")
     5.9          except Exception, e:
    5.10              self.log.error(self.tid, "Problems handling data: %s" % e)
    5.11              return False
     6.1 --- a/gmyth-stream/server/0.3/plugins/transcoders/mencoder.py	Wed May 30 19:53:13 2007 +0100
     6.2 +++ b/gmyth-stream/server/0.3/plugins/transcoders/mencoder.py	Wed May 30 23:14:38 2007 +0100
     6.3 @@ -14,6 +14,7 @@
     6.4  
     6.5  import lib.utils as utils
     6.6  import lib.server as server
     6.7 +import lib.file_handler as files
     6.8  import plugins.transcoders.mencoder_lib.mythtv as mythtv
     6.9  
    6.10  from select import select
    6.11 @@ -120,6 +121,11 @@
    6.12          self._arg_append(args, self._setup_video())
    6.13  
    6.14          self._arg_append(args, "-really-quiet")
    6.15 +
    6.16 +        if self.args["outfile"] != "-":
    6.17 +            self.args["outfile"] = ".transcoded/%s" % (
    6.18 +                                   os.path.basename(self.args["outfile"]))
    6.19 +
    6.20          self._arg_append(args, "-o %s" % self.args["outfile"])
    6.21          self._arg_append(args, "2>%s" % os.devnull)
    6.22      # _setup_args()
    6.23 @@ -160,7 +166,7 @@
    6.24              self._setup_filename()
    6.25              self._setup_mencoder_opts(self.mencoder_opts)
    6.26          except Exception, e:
    6.27 -            self.log.error(self.tid, e)
    6.28 +            self.log.error(self.tid, "Error: %s" % e)
    6.29      # __init__()
    6.30  
    6.31  
    6.32 @@ -177,15 +183,15 @@
    6.33      def _start_outfile(self, outfd):
    6.34          finished = False
    6.35  
    6.36 -        # fix this (not necessary)
    6.37 -        outfd.write("OK")
    6.38 -
    6.39          # Configuring stdin
    6.40          try:
    6.41 -            _stdin = open(self.args["input"])
    6.42 -            size = int(os.path.getsize(self.args["input"]))
    6.43 +            filename = self.args["input"].split("://")[1]
    6.44 +            _stdin = open(filename)
    6.45 +            size = int(os.path.getsize(filename))
    6.46          except Exception, e:
    6.47 -            self.log.error(self.tid, "Mencoder stdin setup error: %s" % e)
    6.48 +            self.log.error(self.tid, "Error: Mencoder stdin"\
    6.49 +                           " setup error: %s" % e)
    6.50 +            outfd.write("Error: Mencoder stdin setup error: %s" %e)
    6.51              return False
    6.52  
    6.53          self.status = 0
    6.54 @@ -207,8 +213,7 @@
    6.55                          os.write(stdw, data_in)
    6.56                          total_read += 4096
    6.57                          d = stdout.read(4096)
    6.58 -                        self.status = utils.progress_bar(self.log,
    6.59 -                                                         int(total_read),
    6.60 +                        self.status = utils.progress_bar(int(total_read),
    6.61                                                           int(size), 50)
    6.62                      else:
    6.63                          finished = True
    6.64 @@ -218,11 +223,12 @@
    6.65                      d = stdout.read(4096)
    6.66  
    6.67          except Exception, e:
    6.68 -            self.log.error(self.tid, "Problems handling data: %s" % e)
    6.69 +            self.log.error(self.tid, "Error: %s" % e)
    6.70              self.stop()
    6.71              return False
    6.72  
    6.73 -        self.log.info(self.tid, "%s: Finished sending data to client" % repr(self))
    6.74 +        files.TranscodedFile("", self.args)
    6.75 +        self.log.info(self.tid, "OK: Done")
    6.76          return True
    6.77      # _start_outfile()
    6.78  
    6.79 @@ -236,10 +242,10 @@
    6.80                  d = self.proc.stdout.read(1024)
    6.81                  outfd.write(d)
    6.82          except Exception, e:
    6.83 -            self.log.error(self.tid, "Problems handling data: %s" % e)
    6.84 +            self.log.error(self.tid, "Error: %s" % e)
    6.85              return False
    6.86  
    6.87 -        self.log.info(self.tid, "%s: Finished sending data to client" % repr(self))
    6.88 +        self.log.info(self.tid, "OK: Done")
    6.89          return True
    6.90      # _start()
    6.91  
    6.92 @@ -248,7 +254,7 @@
    6.93              self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
    6.94                                           stdout=output, close_fds=True)
    6.95          except Exception, e:
    6.96 -            self.log.error(self.tid, "Error executing mencoder: %s" % e)
    6.97 +            self.log.error(self.tid, "Error: Mencoder: %s" % e)
    6.98              return False
    6.99  
   6.100          return True
   6.101 @@ -275,7 +281,8 @@
   6.102          self.stop()
   6.103  
   6.104          if not ret:
   6.105 -            self.log.error(self.tid, "Problems while starting streaming.")
   6.106 +            self.log.error(self.tid, "Error: Problems while "\
   6.107 +                           "starting streaming.")
   6.108  
   6.109          return ret
   6.110      # start()
     7.1 --- a/gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py	Wed May 30 19:53:13 2007 +0100
     7.2 +++ b/gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py	Wed May 30 23:14:38 2007 +0100
     7.3 @@ -54,7 +54,7 @@
     7.4                   self.args["gmyth-cat"]["cfile"]
     7.5                   ]
     7.6      else:
     7.7 -        self.log.error(self.tid, "URI error")
     7.8 +        self.log.error(self.tid, "Error: URI error")
     7.9          return []
    7.10  # _setup_gmythcat
    7.11  
    7.12 @@ -65,7 +65,7 @@
    7.13                                        stderr=subprocess.PIPE,
    7.14                                        close_fds=True)
    7.15      except Exception, e:
    7.16 -        self.log.error(self.tid, "Error executing gmyth-cat: %s" % e)
    7.17 +        self.log.error(self.tid, "Error: gmyth-cat: %s" % e)
    7.18          return False
    7.19  
    7.20      if not self._run_mencoder(input=self.gmyth.stdout,
    7.21 @@ -75,9 +75,10 @@
    7.22      if self.args["gmyth-cat"]["kind"] == "f":
    7.23          try:
    7.24              size = _setup_mythfile(self.gmyth.stderr)
    7.25 -            self.log.debug(self.tid, "Size of file: %s" % size)
    7.26 +            self.log.debug(self.tid, "Info: Size of file: %s" % size)
    7.27          except Exception, e:
    7.28 -            self.log.error(self.tid, "Problems getting size of file: %s" % e)
    7.29 +            self.log.error(self.tid, "Error: Problems getting size of"\
    7.30 +                           " file: %s" % e)
    7.31              return False
    7.32  
    7.33      try:
    7.34 @@ -91,16 +92,15 @@
    7.35              if self.gmyth.stderr in r:
    7.36                  partial = self.gmyth.stderr.read(50).split("\n")[-2]
    7.37                  if partial != "":
    7.38 -                    self.status = utils.progress_bar(self.log,
    7.39 -                                                     int(partial),
    7.40 +                    self.status = utils.progress_bar(int(partial),
    7.41                                                       int(size), 50)
    7.42  
    7.43      except IndexError, e:
    7.44          pass
    7.45      except Exception, e:
    7.46 -        self.log.error(self.tid, "Problems handling data: %s" % e)
    7.47 +        self.log.error(self.tid, "Error: %s" % e)
    7.48          return False
    7.49  
    7.50 -    self.log.info(self.tid, "Finished sending data")
    7.51 +    self.log.info(self.tid, "OK: Done")
    7.52      return True
    7.53  # _start_myth()