CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dbfile2html.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 # coding: utf-8
3 '''
4 Generates static HTML for the given database file.
5 Warrning!: did not finish the implementation, see TODO comment.
6 
7 Author: Albertas Gimbutas, Vilnius University (LT)
8 e-mail: albertasgim@gmail.com
9 '''
10 import sqlite3
11 import re
12 from os import listdir, makedirs, getcwd
13 from os.path import isfile, join, exists, dirname, basename
14 from app_utils import *
15 from optparse import OptionParser
16 
17 from jinja2 import Environment, FileSystemLoader, escape
18 env = Environment(loader=FileSystemLoader('templates')) # Template directory has to exist
19 
20 parser = OptionParser(usage='Usage: %prog --db PATH_TO_DB [options]')
21 parser.add_option('--db', action='store', dest='db_name',
22  help='Absolute path to SQLite3 database file.')
23 parser.add_option('--th', action='store', dest='threshold', default=1e-5,
24  help='Threshold to use for static HTML statistics. Default: %default.')
25 
26 def create_page(path, content):
27  path = join(*path)
28  if not exists(dirname(path)):
29  makedirs(dirname(path))
30  f = open(path + '.html', 'w')
31  f.write(content)
32  f.close()
33 
34 def dbfile2html(db_name, work_path, threshold=1e-5):
35  """
36  Generates static HTML from given release comparison database file.
37  Algorithm: iterates through database, renders Jinja2 templates and saves
38  them to static HTML files.
39  """
40  if not exists(db_name):
41  print "\nError: SQLite3 database file does not exsits. Exitting...\n"
42  exit()
43 
44  conn = sqlite3.connect(db_name)
45  c = conn.cursor()
46 
47  ## Initialise working directory.
48  path = join(work_path, 'static_html')
49  if not exists(path):
50  makedirs(path)
51 
52  global_context = {'db_name': None , 'threshold': threshold,
53  'file_id': None, 'args': [], 'kwargs': None}
54  global_context['static_html'] = True
55  global_context['base_path'] = work_path.strip('/')
56 
57  ## Generate DB list page
58  context = global_context.copy()
59  db_list_temp = env.get_template('db_list.html')
60  context['db_list'] = db_list_with_releases(work_path)
61  f = open(join(path, 'index.html'), 'w')
62  f.write(db_list_temp.render(context))
63 
64  ## Generate ReleaseComparison html pages
65  c.execute('''SELECT id, title, statistical_test FROM ReleaseComparison;''')
66  releases = c.fetchall()
67  rel_summary_temp = env.get_template('release_summary.html')
68  dir_summary_temp = env.get_template('directory_summary.html')
69 
70 
71  for rel_id, release_title, st_test in releases:
72  context = global_context.copy()
73  context.update(get_release_summary_stats(c, release_title, st_test, threshold))
74  context['release_title'] = release_title
75  context['st_test'] = st_test
76  create_page([path, release_title, st_test], rel_summary_temp.render(context))
77 
78  ## Generate RootFileComparison html pages
79  print 'Generating %s (%s) comparison pages...' % (release_title, st_test)
80  c.execute('''SELECT id, directory_id FROM RootFileComparison WHERE release_comparison_id = ?;''', (rel_id,))
81  for file_id, file_top_dir_id in c.fetchall():
82  context['file_id'] = file_id
83  context.update(get_directory_summary_stats(c, [], file_id, threshold))
84  create_page([path, release_title, st_test, str(file_id)], dir_summary_temp.render(context))
85 
86  c.execute('''SELECT id FROM Directory WHERE parent_id=?''', (file_top_dir_id,))
87  children_dirs = c.fetchall()
88 
89  ## Generate Directory html pages
90  def create_dir_pages(c, dir_id, dir_path):
91  # Generate Directory page
92  c.execute('''SELECT name FROM Directory WHERE id=?''', (dir_id,))
93  dir_path.append(c.fetchone()[0])
94  context.update(get_directory_summary_stats(c, dir_path, file_id, threshold))
95  create_page([path, release_title, st_test, str(file_id)] + dir_path, dir_summary_temp.render(context))
96  # TODO: Call for subdirectories
97 
98  for children_dir in children_dirs:
99  create_dir_pages(c, children_dir[0], [])
100  print 'Done.'
101 
102 
103 if __name__ == '__main__':
104  opts, args = parser.parse_args()
105  dbfile2html(opts.db_name, dirname(opts.db_name), opts.threshold)
def get_release_summary_stats
Definition: app_utils.py:162
def db_list_with_releases
Definition: app_utils.py:149
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def get_directory_summary_stats
Definition: app_utils.py:282