CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Utilities/RelMon/python/web/dbfile2html.py

Go to the documentation of this file.
00001 #! /usr/bin/python
00002 # coding: utf-8
00003 '''
00004 Generates static HTML for the given database file.
00005 Warrning!: did not finish the implementation, see TODO comment.
00006 
00007 Author:  Albertas Gimbutas,  Vilnius University (LT)
00008 e-mail:  albertasgim@gmail.com
00009 '''
00010 import sqlite3
00011 import re
00012 from os import listdir, makedirs, getcwd
00013 from os.path import isfile, join, exists, dirname, basename
00014 from app_utils import *
00015 from optparse import OptionParser
00016 
00017 from jinja2 import Environment, FileSystemLoader, escape
00018 env = Environment(loader=FileSystemLoader('templates'))  # Template directory has to exist
00019 
00020 parser = OptionParser(usage='Usage: %prog --db PATH_TO_DB [options]')
00021 parser.add_option('--db', action='store', dest='db_name',
00022         help='Absolute path to SQLite3 database file.')
00023 parser.add_option('--th', action='store', dest='threshold', default=1e-5,
00024         help='Threshold to use for static HTML statistics. Default: %default.')
00025 
00026 def create_page(path, content):
00027     path = join(*path)
00028     if not exists(dirname(path)):
00029         makedirs(dirname(path))
00030     f = open(path + '.html', 'w')
00031     f.write(content)
00032     f.close()
00033 
00034 def dbfile2html(db_name, work_path, threshold=1e-5):
00035     """
00036     Generates static HTML from given release comparison database file.
00037     Algorithm: iterates through database, renders Jinja2 templates and saves
00038     them to static HTML files.
00039     """
00040     if not exists(db_name):
00041         print "\nError: SQLite3 database file does not exsits. Exitting...\n"
00042         exit()
00043 
00044     conn = sqlite3.connect(db_name)
00045     c = conn.cursor()
00046 
00047     ## Initialise working directory.
00048     path = join(work_path, 'static_html')
00049     if not exists(path):
00050         makedirs(path)
00051 
00052     global_context = {'db_name': None , 'threshold': threshold,
00053                                 'file_id': None, 'args': [], 'kwargs': None}
00054     global_context['static_html'] = True
00055     global_context['base_path'] = work_path.strip('/')
00056 
00057     ## Generate DB list page
00058     context = global_context.copy()
00059     db_list_temp = env.get_template('db_list.html')
00060     context['db_list'] = db_list_with_releases(work_path)
00061     f = open(join(path, 'index.html'), 'w')
00062     f.write(db_list_temp.render(context))
00063 
00064     ## Generate ReleaseComparison html pages
00065     c.execute('''SELECT id, title, statistical_test FROM ReleaseComparison;''')
00066     releases = c.fetchall()
00067     rel_summary_temp = env.get_template('release_summary.html')
00068     dir_summary_temp = env.get_template('directory_summary.html')
00069 
00070 
00071     for rel_id, release_title, st_test in releases:
00072         context = global_context.copy()
00073         context.update(get_release_summary_stats(c, release_title, st_test, threshold))
00074         context['release_title'] = release_title
00075         context['st_test'] = st_test
00076         create_page([path, release_title, st_test], rel_summary_temp.render(context))
00077 
00078         ## Generate RootFileComparison html pages
00079         print 'Generating %s (%s) comparison pages...' % (release_title, st_test)
00080         c.execute('''SELECT id, directory_id FROM RootFileComparison WHERE release_comparison_id = ?;''', (rel_id,))
00081         for file_id, file_top_dir_id in c.fetchall():
00082             context['file_id'] = file_id
00083             context.update(get_directory_summary_stats(c, [], file_id, threshold))
00084             create_page([path, release_title, st_test, str(file_id)], dir_summary_temp.render(context))
00085 
00086             c.execute('''SELECT id FROM Directory WHERE parent_id=?''', (file_top_dir_id,))
00087             children_dirs = c.fetchall()
00088 
00089             ## Generate Directory html pages
00090             def create_dir_pages(c, dir_id, dir_path):
00091                 # Generate Directory page
00092                 c.execute('''SELECT name FROM Directory WHERE id=?''', (dir_id,))
00093                 dir_path.append(c.fetchone()[0])
00094                 context.update(get_directory_summary_stats(c, dir_path, file_id, threshold))
00095                 create_page([path, release_title, st_test, str(file_id)] + dir_path, dir_summary_temp.render(context))
00096                 # TODO: Call for subdirectories
00097 
00098             for children_dir in children_dirs:
00099                 create_dir_pages(c, children_dir[0], [])
00100         print 'Done.'
00101 
00102 
00103 if __name__ == '__main__':
00104     opts, args = parser.parse_args()
00105     dbfile2html(opts.db_name, dirname(opts.db_name), opts.threshold)