rework etc/generate.py -> bin/rrdweb, just use target names...
authorTero Marttila <terom@fixme.fi>
Sat, 30 Jan 2010 20:13:48 +0200
changeset 12 baf5d3537484
parent 11 f488f8a6e1d8
child 13 30f866582b46
rework etc/generate.py -> bin/rrdweb, just use target names...
bin/rrdweb
etc/generate.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/rrdweb	Sat Jan 30 20:13:48 2010 +0200
@@ -0,0 +1,143 @@
+#!/usr/bin/env python
+
+import os.path, fnmatch
+import optparse, logging
+
+from rrdweb import graph, html
+
+
+def parse_args (args) :
+    global options
+
+    parser = optparse.OptionParser(
+        usage   = "%prog [options] <target> [...]"
+    )
+
+    parser.add_option('-R', "--rrd-dir", default="rrd", help="Path to directory containing .rrd files")
+    parser.add_option('-I', "--img-dir", default="img", help="Path to directory containing .png files")
+    parser.add_option('-W', "--web-dir", default="web", help="Path to directory containing .html files")
+    parser.add_option('-T', "--tpl-dir", default="templates", help="Path to directory containing .html templates files")
+    parser.add_option('-P', "--url-prefix", default="", help="Prefix for URLs")
+    parser.add_option(      "--img-url", default="img", help="URL to directory containing .png files")
+    parser.add_option(      "--web-url", default="web", help="URL to directory containing .html files")
+    
+    options, targets = parser.parse_args(args)
+    
+    return targets
+
+def setup_img_dir (style, interval) :
+    """
+        Generate output dir for given name
+    """
+
+    base_dir = options.img_dir
+    style_dir = os.path.join(base_dir, style)
+    interval_dir = os.path.join(base_dir, style, interval)
+
+    if not os.path.exists(options.img_dir) :
+        raise Exception("img_dir does not exist: " + options.img_dir)
+    
+
+    if not os.path.exists(style_dir) :
+        logging.warn("Create img_dir for style=%s: %s" % (style, style_dir))
+        os.mkdir(style_dir)
+
+    if not os.path.exists(interval_dir) :
+        logging.warn("Create img_dir for style=%s, interval=%s: %s" % (style, interval, interval_dir))
+        os.mkdir(interval_dir)
+
+def setup_dirs () :
+    """
+        Generate output dirs
+    """
+
+    setup_img_dir("overview", "daily")
+
+    for interval in ("daily", "weekly", "yearly") :
+        setup_img_dir("detail", interval)
+
+def target_graph (target, style, interval) : 
+    """
+        Generate .rrd -> .png for given target
+    """
+
+    # XXX: title..
+    title = target
+
+    # compose paths
+    rrd_path = os.path.join(options.rrd_dir, target) + '.rrd'
+    out_path = os.path.join(options.img_dir, style, interval, target) + '.png'
+
+    # graph
+    graph.mrtg(style, interval, title, rrd_path, out_path)
+
+def target_html (target, formatter) :
+    """
+        Generate .html for given target
+    """
+
+    # XXX: title..
+    title = target
+    
+    # build paths
+    html_path = os.path.join(options.web_dir, target + '.html')
+    
+    # render
+    open(html_path, 'w').write(formatter.target(target, title))
+
+def overview_html (targets, formatter) :
+    """
+        Generate .html index
+    """
+    
+    # paths
+    overview_path = os.path.join(options.web_dir, "index.html")
+    
+    # as (target, title) pairs
+    open(overview_path, 'w').write(formatter.overview((target, target) for target in targets))
+
+def generate (targets, filters=None):
+    if filters :
+        # filter targets
+        targets = [target for target in targets if any(fnmatch.fnmatch(target.name, filter) for filter in filters)]
+
+    # dirs
+    setup_dirs()
+    
+    logging.info("Updating %d targets", len(targets))
+
+    # overview
+    formatter = html.Formatter(
+        url_prefix      = options.url_prefix,
+        img_url         = "%(prefix)s/" + options.img_url + "/%(style)s/%(interval)s/%(target)s.png",
+        target_url      = "%(prefix)s/" + options.web_url + "/%(target)s.html",
+    )
+    
+    # html for overview
+    overview_html(targets, formatter)
+
+    for target in targets :
+        logging.info("Target: %s", target)
+        
+        # overview
+        target_graph(target, 'overview', 'daily')
+        
+        # details
+        for interval in ('daily', 'weekly', 'yearly') :
+            target_graph(target, 'detail', interval)
+    
+        # html for target
+        target_html(target, formatter)
+
+def main (args) :
+    # parse args
+    targets = parse_args(args)
+    
+    # run
+    generate(targets)
+
+if __name__ == '__main__' :
+    from sys import argv
+
+    main(argv[1:])
+
--- a/etc/generate.py	Sat Jan 30 20:13:23 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-from rrdweb import graph, html
-
-# list of targets
-from targets import targets
-
-import os.path, fnmatch
-
-rrd_dir = "rrd"
-img_dir = "img"
-web_dir = "web"
-
-def target_graph (target, style, interval) : 
-    # compose paths
-    rrd_path = os.path.join(rrd_dir, target.rrd_name())
-    out_path = os.path.join(img_dir, style, interval, target.img_name())
-
-    # graph
-    graph.mrtg(style, interval, target.title, rrd_path, out_path)
-
-
-def main (targets, filters, style='detail', interval='daily'):
-    if filters :
-        # filter targets
-        targets = [target for target in targets if any(fnmatch.fnmatch(target.name, filter) for filter in filters)]
-
-    # overview
-    html_fmt = html.Formatter(
-        url_prefix      = "/~terom/rrdweb",
-        img_url         = "%(prefix)s/img/%(style)s/%(interval)s/%(target)s.png",
-        target_url      = "%(prefix)s/web/%(target)s.html",
-    )
-
-    # overview page
-    overview_path = os.path.join(web_dir, "index.html")
-    open(overview_path, 'w').write(html_fmt.overview(targets))
-
-    for target in targets :
-        print target.name
-
-        target_graph(target, 'overview', 'daily')
-
-        for interval in ('daily', 'weekly', 'yearly') :
-            target_graph(target, 'detail', interval)
-    
-        # html
-        html_path = os.path.join(web_dir, target.name + '.html')
-        open(html_path, 'w').write(html_fmt.target(target))
-
-if __name__ == '__main__' :
-    import sys
-
-    main(targets, sys.argv[1:])
-