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