public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/
Date: Tue, 13 Aug 2013 08:56:36 +0000 (UTC)	[thread overview]
Message-ID: <1376383812.a5254ba1cab1dde89f78a9552373dd342de8ae97.dywi@gentoo> (raw)

commit:     a5254ba1cab1dde89f78a9552373dd342de8ae97
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Tue Aug 13 08:50:12 2013 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Tue Aug 13 08:50:12 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=a5254ba1

roverlay/status: status report generation

roverlay/status creates an environment for generating status reports based on
mako templates. CGI mode is supported, too ("--cgi").

---
 roverlay/status.py | 314 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 314 insertions(+)

diff --git a/roverlay/status.py b/roverlay/status.py
new file mode 100644
index 0000000..7872674
--- /dev/null
+++ b/roverlay/status.py
@@ -0,0 +1,314 @@
+# R overlay -- create status reports based on templates
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+from __future__ import print_function
+#from __future__ import division
+
+import os
+import sys
+import cgi
+import cgitb
+import weakref
+
+
+# using mako
+import mako.exceptions
+import mako.lookup
+import mako.template
+
+
+import roverlay.argparser
+import roverlay.runtime
+import roverlay.tools.shenv
+import roverlay.db.rrdtool
+import roverlay.util.common
+
+
+class ReferenceableDict ( dict ):
+   def ref ( self ):
+      return weakref.ref ( self )
+   # --- end of ref (...) ---
+
+   def sorted_items ( self, keysort=None ):
+      if keysort is None:
+         return sorted ( self.items(), key=lambda kv: kv[0] )
+      else:
+         return sorted ( self.items(), key=lambda kv: keysort ( kv[0] ) )
+   # --- end of sorted_items (...) ---
+
+# --- end of ReferenceableDict ---
+
+class SelfReferencingDict ( ReferenceableDict ):
+   SELFREF_KEY = 'dictref'
+
+##   def __setitem__ ( self, key, *args, **kw ):
+##      if key == self.__class__.SELFREF_KEY:
+##         raise AttributeError (
+##            "{!r} is readonly.".format ( self.__class__.SELFREF_KEY )
+##         )
+##      else:
+##         return super ( SelfReferencingDict, self ).__setitem__ (
+##            key, *args, **kw
+##         )
+##   # --- end of __setitem__ (...) ---
+
+   def __init__ ( self, *args, **kwargs ):
+      super ( SelfReferencingDict, self ).__init__ ( *args, **kwargs )
+      self [self.__class__.SELFREF_KEY] = self.ref()
+      # or use __getitem__
+   # --- end of __init__ (...) ---
+
+# --- end of SelfReferencingDict ---
+
+
+class StatusRuntimeEnvironment ( roverlay.runtime.RuntimeEnvironmentBase ):
+   ARG_PARSER_CLS = roverlay.argparser.RoverlayStatusArgumentParser
+
+   TEMPLATE_ENCODING = 'utf-8'
+
+   # variables from /etc/nginx/fastcgi.conf
+   #  (will be kept in template env dict depending on script mode)
+   #
+   #  sed -nr -e 's,^fastcgi_param\s+(\S+)\s+.*$,"\1"\,,p' \
+   #     /etc/nginx/fastcgi.conf | sort
+   #
+   NGINX_CGI_VARS = frozenset ({
+      "CONTENT_LENGTH",
+      "CONTENT_TYPE",
+      "DOCUMENT_ROOT",
+      "DOCUMENT_URI",
+      "GATEWAY_INTERFACE",
+      "HTTPS",
+      "QUERY_STRING",
+      "REDIRECT_STATUS",
+      "REMOTE_ADDR",
+      "REMOTE_PORT",
+      "REQUEST_METHOD",
+      "REQUEST_URI",
+      "SCRIPT_FILENAME",
+      "SCRIPT_NAME",
+      "SERVER_ADDR",
+      "SERVER_NAME",
+      "SERVER_PORT",
+      "SERVER_PROTOCOL",
+      "SERVER_SOFTWARE",
+   })
+
+   # common cgi vars as listed in
+   #  /usr/lib/python3.2/cgi.py
+   #
+   COMMON_CGI_VARS = frozenset ({
+      "AUTH_TYPE",
+      "CONTENT_LENGTH",
+      "CONTENT_TYPE",
+      "DATE_GMT",
+      "DATE_LOCAL",
+      "DOCUMENT_NAME",
+      "DOCUMENT_ROOT",
+      "DOCUMENT_URI",
+      "GATEWAY_INTERFACE",
+      "LAST_MODIFIED",
+      #"PATH",
+      "PATH_INFO",
+      "PATH_TRANSLATED",
+      "QUERY_STRING",
+      "REMOTE_ADDR",
+      "REMOTE_HOST",
+      "REMOTE_IDENT",
+      "REMOTE_USER",
+      "REQUEST_METHOD",
+      "SCRIPT_NAME",
+      "SERVER_NAME",
+      "SERVER_PORT",
+      "SERVER_PROTOCOL",
+      "SERVER_ROOT",
+      "SERVER_SOFTWARE",
+      "HTTP_ACCEPT",
+      "HTTP_CONNECTION",
+      "HTTP_HOST",
+      "HTTP_PRAGMA",
+      "HTTP_REFERER",
+      "HTTP_USER_AGENT",
+   })
+
+
+   def do_setup_mako ( self ):
+      template_dirs = []
+
+      if 'template' in self.options:
+         dirname, basename = os.path.split (
+            self.options ['template'].rstrip ( os.sep )
+         )
+         assert dirname and basename
+         template_dirs.append ( dirname )
+         self.default_template = basename
+      else:
+         self.default_template = self.script_mode
+      # -- end if
+
+      if self.installed:
+         template_dirs.append (
+            self.config.get_or_fail ( 'INSTALLINFO.libexec' )
+         )
+
+      extra_dirs = self.config.get ( 'STATS.TEMPLATE.root' )
+      if extra_dirs:
+         template_dirs.extend ( extra_dirs )
+
+      if not template_dirs:
+         raise Exception ( "no template directories found!" )
+
+
+      module_dir = self.config.get ( 'STATS.TEMPLATE.module_dir', None )
+      if module_dir is None:
+         module_dir = self.config.get ( 'CACHEDIR.root', None )
+         if module_dir:
+            module_dir += os.sep + 'mako_templates'
+
+
+      self._mako_lookup = mako.lookup.TemplateLookup (
+         directories=template_dirs, module_directory=module_dir,
+         output_encoding=self.TEMPLATE_ENCODING,
+      )
+   # --- end of do_setup_mako (...) ---
+
+   def do_setup ( self ):
+      self.do_setup_parser()
+      script_mode = self.options ['script_mode']
+      assert script_mode and script_mode.islower()
+      if script_mode == 'cgi':
+         cgitb.enable()
+
+      self.do_setup_config()
+      self.template_vars = SelfReferencingDict (
+         roverlay.tools.shenv.setup_env()
+      )
+      roverlay.tools.shenv.restore_msg_vars ( self.template_vars )
+
+      try:
+         del self.template_vars ['STATS_DB']
+      except KeyError:
+         pass
+
+      self.script_mode = script_mode
+      self.outfile     = self.options ['outfile']
+      if self.outfile == '-':
+         self.outfile = None
+
+      self.set_template_vars (
+         EXE=sys.argv[0], EXE_NAME=os.path.basename ( sys.argv[0] ),
+         SCRIPT_MODE=script_mode
+      )
+
+      if script_mode == 'cgi':
+         # inherit cgi-related vars from os.environ
+         self.set_template_vars (
+            roverlay.util.common.keepenv_v (
+               self.NGINX_CGI_VARS|self.COMMON_CGI_VARS
+            ),
+            CGI_FORM=cgi.FieldStorage( keep_blank_values=0 ),
+         )
+
+
+      self.stats_db = None
+      stats_db_file = self.config.get ( 'RRD_DB.file', None )
+      if stats_db_file:
+         self.stats_db = roverlay.db.rrdtool.RRD (
+            stats_db_file, readonly=True
+         )
+         self.stats_db.make_cache()
+
+
+      # transfer db cache to template_vars
+      # * copy lastupdate
+      # * import values
+      #
+      self.set_template_vars (
+         self.stats_db.cache ['values'],
+         lastupdate=self.stats_db.cache ['lastupdate'],
+         STATS_DB=self.stats_db, STATS_DB_FILE=stats_db_file,
+      )
+
+      self.do_setup_mako()
+   # --- end of do_setup (...) ---
+
+   def serve_template ( self, template_name=None, catch_exceptions=True ):
+      try:
+         my_template = self._mako_lookup.get_template (
+            template_name if template_name is not None
+            else self.default_template
+         )
+         ret = my_template.render ( **self.template_vars )
+      except:
+         if catch_exceptions:
+            if self.script_mode == 'cgi':
+               ret = mako.exceptions.html_error_template().render()
+            else:
+               ret = mako.exceptions.text_error_template().render()
+         else:
+            raise
+      # -- end if
+      return ret
+   # --- end of serve_template (...) ---
+
+   def set_template_vars ( self, *args, **kwargs ):
+      for kw in args:
+         self.template_vars.update ( kw )
+      self.template_vars.update ( kwargs )
+   # --- end of setup_vars (...) ---
+
+   def write_cgi_header ( self, stream, encode=False, force=False ):
+      if force or self.script_mode == 'cgi':
+         content_type = self.options ['cgi_content_type']
+         if content_type:
+            header = "Content-Type: {}\n\n".format ( content_type )
+            if encode:
+               stream.write ( header.encode ( self.TEMPLATE_ENCODING ) )
+            else:
+               stream.write ( header )
+            return True
+      return False
+   # --- end of write_cgi_header (...) ---
+
+   @classmethod
+   def encode ( cls, text ):
+      return str ( text ).encode ( cls.TEMPLATE_ENCODING )
+   # --- end of encode (...) ---
+
+   @classmethod
+   def decode ( cls, data, force=False ):
+      if force or not isinstance ( data, str ):
+         return data.decode ( cls.TEMPLATE_ENCODING )
+      else:
+         return str ( data )
+   # --- end of decode (...) ---
+
+# --- end of StatusRuntimeEnvironment ---
+
+
+def main_installed ( *args, **kwargs ):
+   return main ( True, *args, **kwargs )
+# --- end of main_installed (...) ---
+
+def main ( installed, *args, **kw ):
+   main_env = StatusRuntimeEnvironment ( installed, *args, **kw )
+   main_env.setup()
+
+   output_encoded = main_env.serve_template()
+
+   if main_env.outfile:
+      with open (
+         main_env.outfile,
+         'w' + ( 'b' if not isinstance ( output, str ) else 't' )
+      ) as FH:
+         # COULDFIX: write cgi header to file?
+         #main_env.write_cgi_header ( FH, encode=True )
+         FH.write ( output_encoded )
+   else:
+      output = main_env.decode ( output_encoded )
+      main_env.write_cgi_header ( sys.stdout, force=True )
+      sys.stdout.write ( output )
+# --- end of main (...) ---


             reply	other threads:[~2013-08-13  8:56 UTC|newest]

Thread overview: 159+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-13  8:56 André Erdmann [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-01-26 17:41 [gentoo-commits] proj/R_overlay:master commit in: roverlay/ André Erdmann
2015-01-26 17:41 André Erdmann
2014-07-18 16:20 André Erdmann
2014-07-18  2:50 [gentoo-commits] proj/R_overlay:wip/addition_control " André Erdmann
2014-07-18 16:20 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2014-07-18  2:28 [gentoo-commits] proj/R_overlay:wip/addition_control " André Erdmann
2014-07-18 16:20 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2014-07-16 15:14 André Erdmann
2014-06-05 22:09 André Erdmann
2014-04-01 16:38 André Erdmann
2014-02-16 16:30 André Erdmann
2014-02-15 19:49 André Erdmann
2014-02-15 19:49 André Erdmann
2014-01-25 18:14 André Erdmann
2013-12-11 18:40 André Erdmann
2013-12-11 18:40 André Erdmann
2013-09-23 15:30 André Erdmann
2013-09-20 15:57 André Erdmann
2013-09-19 15:00 André Erdmann
2013-09-17 16:40 André Erdmann
2013-09-17 16:40 André Erdmann
2013-09-17 16:40 André Erdmann
2013-09-17 16:40 André Erdmann
2013-09-16 13:43 André Erdmann
2013-09-13 15:10 André Erdmann
2013-09-12 16:36 André Erdmann
2013-09-12 16:36 André Erdmann
2013-09-12 16:36 André Erdmann
2013-09-11 11:14 André Erdmann
2013-09-11 10:19 André Erdmann
2013-09-10 14:40 André Erdmann
2013-09-10 14:40 André Erdmann
2013-09-10 14:40 André Erdmann
2013-09-10 14:40 André Erdmann
2013-09-06 17:27 André Erdmann
2013-09-06 17:27 André Erdmann
2013-09-03 15:50 André Erdmann
2013-09-02 12:27 André Erdmann
2013-09-02  8:44 André Erdmann
2013-08-30 14:49 André Erdmann
2013-08-30 14:49 André Erdmann
2013-08-29 12:36 André Erdmann
2013-08-29 12:36 André Erdmann
2013-08-28 15:54 André Erdmann
2013-08-27 15:39 André Erdmann
2013-08-23 13:52 André Erdmann
2013-08-23 13:52 André Erdmann
2013-08-23 13:52 André Erdmann
2013-08-19 15:42 André Erdmann
2013-08-16 14:05 André Erdmann
2013-08-16 11:02 André Erdmann
2013-08-16 10:43 André Erdmann
2013-08-16 10:43 André Erdmann
2013-08-14 14:56 André Erdmann
2013-08-14 14:56 André Erdmann
2013-08-13  8:56 André Erdmann
2013-08-13  8:56 André Erdmann
2013-08-12  8:28 André Erdmann
2013-08-12  8:18 André Erdmann
2013-08-07 16:10 André Erdmann
2013-08-02 14:30 André Erdmann
2013-08-02 10:34 André Erdmann
2013-08-02 10:34 André Erdmann
2013-08-01 12:44 André Erdmann
2013-08-01 12:44 André Erdmann
2013-07-29 14:56 André Erdmann
2013-07-29  8:55 André Erdmann
2013-07-26 13:02 André Erdmann
2013-07-23  7:51 André Erdmann
2013-07-23  7:51 André Erdmann
2013-07-19 18:00 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-23  7:51 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-07-17 18:05 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-17 18:05 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-07-15 22:31 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-16 16:36 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-07-12 13:57 André Erdmann
2013-06-22 15:24 André Erdmann
2013-06-22 15:24 André Erdmann
2013-06-22 15:24 André Erdmann
2013-06-22 15:24 André Erdmann
2013-06-19 18:58 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-22 15:24 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-19 18:58 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-19 18:59 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-13 16:34 André Erdmann
2013-06-05 18:08 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-13 16:34 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-05 18:08 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-13 16:34 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-04 21:06 André Erdmann
2013-04-25 16:44 André Erdmann
2013-04-25 16:44 André Erdmann
2013-03-05 11:27 André Erdmann
2013-02-09 20:45 André Erdmann
2013-02-05 17:48 André Erdmann
2013-02-05 17:48 André Erdmann
2013-01-30 20:16 André Erdmann
2013-01-30 20:16 André Erdmann
2013-01-28 23:54 André Erdmann
2013-01-28 23:54 André Erdmann
2013-01-28 23:54 André Erdmann
2012-10-02 10:04 André Erdmann
2012-08-20 11:16 André Erdmann
2012-08-13 18:07 André Erdmann
2012-08-09  9:26 André Erdmann
2012-08-08 23:46 André Erdmann
2012-08-08 23:46 André Erdmann
2012-08-07  8:50 André Erdmann
2012-08-02 15:14 André Erdmann
2012-08-01  7:25 André Erdmann
2012-07-31 17:51 André Erdmann
2012-07-30  8:52 André Erdmann
2012-07-30  8:52 André Erdmann
2012-07-24 16:59 [gentoo-commits] proj/R_overlay:overlay_wip " André Erdmann
2012-07-30  8:52 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2012-07-18 16:49 [gentoo-commits] proj/R_overlay:overlay_wip " André Erdmann
2012-07-30  8:52 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2012-07-16 16:15 André Erdmann
2012-07-16 16:15 André Erdmann
2012-07-16 16:15 [gentoo-commits] proj/R_overlay:depres_wip " André Erdmann
2012-07-16 16:15 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2012-07-10 17:43 André Erdmann
2012-07-09 17:19 André Erdmann
2012-07-04 18:21 André Erdmann
2012-07-04 18:21 André Erdmann
2012-07-03 17:48 André Erdmann
2012-06-28 13:29 André Erdmann
2012-06-26 15:42 André Erdmann
2012-06-25 18:19 André Erdmann
2012-06-21 16:55 André Erdmann
2012-06-20 19:03 André Erdmann
2012-06-20 19:03 André Erdmann
2012-06-18 16:27 André Erdmann
2012-06-15 20:34 André Erdmann
2012-06-15 20:34 André Erdmann
2012-06-15 20:34 André Erdmann
2012-06-15 20:34 André Erdmann
2012-06-15 20:34 André Erdmann
2012-06-15 20:34 André Erdmann
2012-06-12 17:17 André Erdmann
2012-06-06 19:52 André Erdmann
2012-06-06 19:52 André Erdmann
2012-06-06 19:52 André Erdmann
2012-06-05 17:30 André Erdmann
2012-06-04 19:07 André Erdmann
2012-06-04 19:07 André Erdmann
2012-06-04 15:43 André Erdmann
2012-06-01 16:19 André Erdmann
2012-06-01 16:19 André Erdmann
2012-06-01 15:46 André Erdmann
2012-05-31 18:24 André Erdmann
2012-05-30 20:15 André Erdmann
2012-05-30 19:36 André Erdmann
2012-05-30 19:36 André Erdmann
2012-05-30 16:09 André Erdmann
2012-05-30 16:09 André Erdmann
2012-05-30 16:09 André Erdmann
2012-05-30 16:09 André Erdmann
2012-05-30 10:58 André Erdmann
2012-05-30 10:58 André Erdmann
2012-05-30 10:58 André Erdmann
2012-05-30 10:58 André Erdmann
2012-05-29 17:09 André Erdmann
2012-05-29 17:09 André Erdmann
2012-05-29 17:09 André Erdmann
2012-05-29 17:09 André Erdmann
2012-05-29 17:09 André Erdmann
2012-05-26 13:14 André Erdmann
2012-05-26 13:14 André Erdmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1376383812.a5254ba1cab1dde89f78a9552373dd342de8ae97.dywi@gentoo \
    --to=dywi@mailerd.de \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox