File indexing completed on 2024-11-26 02:34:35
0001
0002
0003 '''
0004 Generates static HTML for the given database file.
0005 Warrning!: did not finish the implementation, see TODO comment.
0006
0007 Author: Albertas Gimbutas, Vilnius University (LT)
0008 e-mail: albertasgim@gmail.com
0009 '''
0010 import sqlite3
0011 import re
0012 from os import listdir, makedirs, getcwd
0013 from os.path import isfile, join, exists, dirname, basename
0014 from .app_utils import *
0015 from optparse import OptionParser
0016
0017 from jinja2 import Environment, FileSystemLoader, escape
0018 env = Environment(loader=FileSystemLoader('templates'))
0019
0020 parser = OptionParser(usage='Usage: %prog --db PATH_TO_DB [options]')
0021 parser.add_option('--db', action='store', dest='db_name',
0022 help='Absolute path to SQLite3 database file.')
0023 parser.add_option('--th', action='store', dest='threshold', default=1e-5,
0024 help='Threshold to use for static HTML statistics. Default: %default.')
0025
0026 def create_page(path, content):
0027 path = join(*path)
0028 if not exists(dirname(path)):
0029 makedirs(dirname(path))
0030 f = open(path + '.html', 'w')
0031 f.write(content)
0032 f.close()
0033
0034 def dbfile2html(db_name, work_path, threshold=1e-5):
0035 """
0036 Generates static HTML from given release comparison database file.
0037 Algorithm: iterates through database, renders Jinja2 templates and saves
0038 them to static HTML files.
0039 """
0040 if not exists(db_name):
0041 print("\nError: SQLite3 database file does not exsits. Exitting...\n")
0042 exit()
0043
0044 conn = sqlite3.connect(db_name)
0045 c = conn.cursor()
0046
0047
0048 path = join(work_path, 'static_html')
0049 if not exists(path):
0050 makedirs(path)
0051
0052 global_context = {'db_name': None , 'threshold': threshold,
0053 'file_id': None, 'args': [], 'kwargs': None}
0054 global_context['static_html'] = True
0055 global_context['base_path'] = work_path.strip('/')
0056
0057
0058 context = global_context.copy()
0059 db_list_temp = env.get_template('db_list.html')
0060 context['db_list'] = db_list_with_releases(work_path)
0061 f = open(join(path, 'index.html'), 'w')
0062 f.write(db_list_temp.render(context))
0063
0064
0065 c.execute('''SELECT id, title, statistical_test FROM ReleaseComparison;''')
0066 releases = c.fetchall()
0067 rel_summary_temp = env.get_template('release_summary.html')
0068 dir_summary_temp = env.get_template('directory_summary.html')
0069
0070
0071 for rel_id, release_title, st_test in releases:
0072 context = global_context.copy()
0073 context.update(get_release_summary_stats(c, release_title, st_test, threshold))
0074 context['release_title'] = release_title
0075 context['st_test'] = st_test
0076 create_page([path, release_title, st_test], rel_summary_temp.render(context))
0077
0078
0079 print('Generating %s (%s) comparison pages...' % (release_title, st_test))
0080 c.execute('''SELECT id, directory_id FROM RootFileComparison WHERE release_comparison_id = ?;''', (rel_id,))
0081 for file_id, file_top_dir_id in c.fetchall():
0082 context['file_id'] = file_id
0083 context.update(get_directory_summary_stats(c, [], file_id, threshold))
0084 create_page([path, release_title, st_test, str(file_id)], dir_summary_temp.render(context))
0085
0086 c.execute('''SELECT id FROM Directory WHERE parent_id=?''', (file_top_dir_id,))
0087 children_dirs = c.fetchall()
0088
0089
0090 def create_dir_pages(c, dir_id, dir_path):
0091
0092 c.execute('''SELECT name FROM Directory WHERE id=?''', (dir_id,))
0093 dir_path.append(c.fetchone()[0])
0094 context.update(get_directory_summary_stats(c, dir_path, file_id, threshold))
0095 create_page([path, release_title, st_test, str(file_id)] + dir_path, dir_summary_temp.render(context))
0096
0097
0098 for children_dir in children_dirs:
0099 create_dir_pages(c, children_dir[0], [])
0100 print('Done.')
0101
0102
0103 if __name__ == '__main__':
0104 opts, args = parser.parse_args()
0105 dbfile2html(opts.db_name, dirname(opts.db_name), opts.threshold)