from itertools import (
    chain,
)
import logging

from azul import (
    config,
    require,
)
from azul.logging import (
    configure_script_logging,
)
from azul.terra import (
    TDRClient,
    TDRSourceName,
)

log = logging.getLogger(__name__)


def main():
    configure_script_logging(log)
    tdr = TDRClient()
    tdr.register_with_sam()

    tdr_catalogs = (
        catalog.name
        for catalog in config.catalogs.values()
        if catalog.plugins['repository'] == 'tdr'
    )
    for source in set(chain(*map(config.tdr_sources, tdr_catalogs))):
        source = TDRSourceName.parse(source)
        api_project = tdr.lookup_source_project(source)
        require(api_project == source.project,
                'Actual Google project of TDR source differs from configured '
                'one',
                api_project, source)
        tdr.check_api_access(source)
        tdr.check_bigquery_access(source)


if __name__ == '__main__':
    main()

[(18, 'logging.getLogger', 'logging.getLogger', False, 'import logging\n'), (22, 'azul.logging.configure_script_logging', 'configure_script_logging', False, 'from azul.logging import configure_script_logging\n'), (23, 'azul.terra.TDRClient', 'TDRClient', False, 'from azul.terra import TDRClient, TDRSourceName\n'), (32, 'azul.terra.TDRSourceName.parse', 'TDRSourceName.parse', False, 'from azul.terra import TDRClient, TDRSourceName\n'), (34, 'azul.require', 'require', False, 'from azul import config, require\n'), (28, 'azul.config.catalogs.values', 'config.catalogs.values', False, 'from azul import config, require\n')]