<commit_before>import contextlib
import logging
import os
import pwd
import sys
from typing import Iterable

from sqlalchemy import Table, exists, null, select
from sqlalchemy.engine import Engine
from sqlalchemy.exc import DBAPIError
from sqlalchemy.pool import NullPool

from hades import constants
from hades.common import db
from hades.common.cli import (
    ArgumentParser, parser as common_parser, setup_cli_logging,
)
from hades.common.privileges import dropped_privileges
from hades.config.base import ConfigError
from hades.config.loader import load_config, print_config_error

logger = logging.getLogger('hades.bin.check_database')


def check_database(engine: Engine, user_name: pwd.struct_passwd,
                   tables: Iterable[Table]):
    logger.info("Checking database access as user %s", user_name)
    try:
        conn = engine.connect()
    except DBAPIError as e:
        logger.critical("Could not connect to database as %s: %s",
                        user_name, e)
        raise
    with contextlib.closing(conn):
        for table in tables:
            try:
                check_table(conn, table)
            except DBAPIError as e:
                logger.critical("Query check for table %s as user %s failed: "
                                "%s", table.name, user_name, e)
                raise


def check_table(conn, table):
    conn.execute(select([exists(select([null()]).select_from(table))])).scalar()


def main():
    parser = ArgumentParser(parents=[common_parser])
    args = parser.parse_args()
    setup_cli_logging(parser.prog, args)
    try:
        config = load_config(args.config)
    except ConfigError as e:
        print_config_error(e)
        return os.EX_CONFIG
    try:
        engine = db.create_engine(config, poolclass=NullPool)
        agent_pwd = <PASSWORD>(constants.AGENT_USER)
        with dropped_privileges(agent_pwd):
            check_database(engine, agent_pwd.pw_name,
                           (db.radacct, db.radpostauth))
        portal_pwd = <PASSWORD>.getpwnam(constants.PORTAL_USER)
        with dropped_privileges(portal_pwd):
            check_database(engine, portal_pwd.pw_name,
                           (db.radacct, db.radpostauth, db.radusergroup))<commit_msg>Use correct kwarg exc_info<commit_after>import contextlib
import logging
import os
import pwd
import sys
from typing import Iterable

from sqlalchemy import Table, exists, null, select
from sqlalchemy.engine import Engine
from sqlalchemy.exc import DBAPIError
from sqlalchemy.pool import NullPool

from hades import constants
from hades.common import db
from hades.common.cli import (
    ArgumentParser, parser as common_parser, setup_cli_logging,
)
from hades.common.privileges import dropped_privileges
from hades.config.base import ConfigError
from hades.config.loader import load_config, print_config_error

logger = logging.getLogger('hades.bin.check_database')


def check_database(engine: Engine, user_name: pwd.struct_passwd,
                   tables: Iterable[Table]):
    logger.info("Checking database access as user %s", user_name)
    try:
        conn = engine.connect()
    except DBAPIError as e:
        logger.critical("Could not connect to database as %s: %s",
                        user_name, exc_info=e)
        raise
    with contextlib.closing(conn):
        for table in tables:
            try:
                check_table(conn, table)
            except DBAPIError as e:
                logger.critical("Query check for table %s as user %s failed: "
                                "%s", table.name, user_name, exc_info=e)
                raise


def check_table(conn, table):
    conn.execute(select([exists(select([null()]).select_from(table))])).scalar()


def main():
    parser = ArgumentParser(parents=[common_parser])
    args = parser.parse_args()
    setup_cli_logging(parser.prog, args)
    try:
        config = load_config(args.config)
    except ConfigError as e:
        print_config_error(e)
        return os.EX_CONFIG
    try:
        engine = db.create_engine(config, poolclass=NullPool)
        agent_pwd = <PASSWORD>(constants.AGENT_USER)
        with dropped_privileges(agent_pwd):
            check_database(engine, agent_pwd.pw_name,
                           (db.radacct, db.radpostauth))
        portal_pwd = pwd.get<PASSWORD>(constants.PORTAL_USER)
        with dropped_privileges(portal_pwd):
            check_database(engine, portal_pwd.pw_name,
                           (db.radacct, db.radpostauth, db.radusergroup))
{'fraction_non_alphanumeric': 0.0843453300949437, 'fraction_numerical': 0.0, 'mean_word_length': 2.8003355704697985, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6415571', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1309, 'n_words': 334}