# Path: database.py
# def init_db():


import os
import csv
import sys
import time
import json
import shutil
import codecs
import hashlib
import StringIO
import utils
import settings
from datetime import datetime
from collections import namedtuple
from jinja2 import Environment, PackageLoader, select_autoescape, Template
from models import *
from database import db_session, init_db
    
    # generate daily rank
    testcase_rank = {}
    person_rank = {}
    for day in days:
        # collect version_ids
        version_ids = set()
        for cid in rejudge_table:
            version_id = rejudge_table[cid].get(day, None)
            if version_id and versions[version_id]['version'].phase == 'end':
                version_ids.add(version_id)
        
        # generate daily testcase rank
        testcase_rank[day] = gen_testcase_rank(versions, version_ids)
        template = env.get_template('final_testcase.html')
        os.makedirs(os.path.join(output_dir, day))
        for t, d in testcase_rank[day].iteritems():
            filename = os.path.join(output_dir, day, 'testcase-{}.html'.format(t))
            with codecs.open(filename, 'w', 'utf-8') as f:
                f.write(template.render(compilers=compilers, 
                                        versions=versions,
                                        day=day,
                                        show_score=True,
                                        **d))
        
        # generate daily person rank
        person_rank[day] = gen_person_rank(rejudge_table, versions, testcase_rank[day], DISCOUNT[day])
        testcase_list = sorted(testcase_rank[day].iterkeys())
        template = env.get_template('final_person.html')
        filename = os.path.join(output_dir, day, 'result.html')

# Path: database.py
# def init_db():


# Path: maintenance.py
import os
import csv
import sys
import time
import json
import shutil
import codecs
import hashlib
import StringIO
import utils
import settings
from datetime import datetime
from collections import namedtuple
from jinja2 import Environment, PackageLoader, select_autoescape, Template
from models import *
from database import db_session, init_db

    
    # generate daily rank
    testcase_rank = {}
    person_rank = {}
    for day in days:
        # collect version_ids
        version_ids = set()
        for cid in rejudge_table:
            version_id = rejudge_table[cid].get(day, None)
            if version_id and versions[version_id]['version'].phase == 'end':
                version_ids.add(version_id)
        
        # generate daily testcase rank
        testcase_rank[day] = gen_testcase_rank(versions, version_ids)
        template = env.get_template('final_testcase.html')
        os.makedirs(os.path.join(output_dir, day))
        for t, d in testcase_rank[day].iteritems():
            filename = os.path.join(output_dir, day, 'testcase-{}.html'.format(t))
            with codecs.open(filename, 'w', 'utf-8') as f:
                f.write(template.render(compilers=compilers, 
                                        versions=versions,
                                        day=day,
                                        show_score=True,
                                        **d))
        
        # generate daily person rank
        person_rank[day] = gen_person_rank(rejudge_table, versions, testcase_rank[day], DISCOUNT[day])
        testcase_list = sorted(testcase_rank[day].iterkeys())
        template = env.get_template('final_person.html')
        filename = os.path.join(output_dir, day, 'result.html')

        with codecs.open(filename, 'w', 'utf-8') as f: