# 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
    os.system('rm {}/*'.format(settings.JUDGE_TESTCASE_PATH))


def makedirs():
    def mkdir(path):
        if not os.path.exists(path):
            os.makedirs(path)
    mkdir(settings.CORE_BUILD_LOG_PATH)
    mkdir(settings.CORE_TESTRUN_STDERR_PATH)
    mkdir(settings.JUDGE_GIT_REPO_PATH)
    mkdir(settings.JUDGE_TESTCASE_PATH)
    print('done!')


def final_rejudge():
    if len(sys.argv) != 4:
        print('usage: ./maintenance.py final_rejudge <input_csv> <output_csv>')
        sys.exit(1)

    compilers = {c.id: c for c in db_session.query(Compiler)}
    submit_versions = set()
    with open(sys.argv[2]) as fin:
        reader = csv.DictReader(fin)
        fieldnames = reader.fieldnames
        for row in reader:
            cid = int(row['cid'])
            assert cid in compilers
            assert compilers[cid].student == row['name'].decode('utf-8')
            for col in fieldnames:
                value = row[col]

# 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

    os.system('rm {}/*'.format(settings.JUDGE_TESTCASE_PATH))


def makedirs():
    def mkdir(path):
        if not os.path.exists(path):
            os.makedirs(path)
    mkdir(settings.CORE_BUILD_LOG_PATH)
    mkdir(settings.CORE_TESTRUN_STDERR_PATH)
    mkdir(settings.JUDGE_GIT_REPO_PATH)
    mkdir(settings.JUDGE_TESTCASE_PATH)
    print('done!')


def final_rejudge():
    if len(sys.argv) != 4:
        print('usage: ./maintenance.py final_rejudge <input_csv> <output_csv>')
        sys.exit(1)

    compilers = {c.id: c for c in db_session.query(Compiler)}
    submit_versions = set()
    with open(sys.argv[2]) as fin:
        reader = csv.DictReader(fin)
        fieldnames = reader.fieldnames
        for row in reader:
            cid = int(row['cid'])
            assert cid in compilers
            assert compilers[cid].student == row['name'].decode('utf-8')
            for col in fieldnames:
                value = row[col]

                if col.decode('utf-8') in ('cid', 'name') or not value: