def main(gtfs_static_base_path: Path):
    script_path = Path(os.path.realpath(__file__))

    app_config_path = Path(f'{script_path.parent}/inc/config.yml')
    if not os.path.isfile(app_config_path):
        print(f'ERROR: cant load config from {app_config_path}')
        sys.exit(1)
    app_config = load_yaml_config(app_config_path, script_path.parent)

    if not os.path.isdir(gtfs_static_base_path):
        print(f'ERROR, download_base_path is not a folder')
        print(f'{gtfs_static_base_path}')
        sys.exit(1)

    print('Fetching latest CKAN info ...')
    dataset_url, dataset_filename = fetch_latest_archive_url(app_config)

    print(f'Latest filename: {dataset_filename}')

    dataset_extension = dataset_filename[-3:]
    if not dataset_extension == 'zip':
        print(f'ERROR: expected zip, got {dataset_extension} for {dataset_filename}')
        sys.exit(1)

    dataset_folder_name = dataset_filename[0:-4]
    dataset_folder_path = Path(f'{gtfs_static_base_path}/{dataset_folder_name}')

    formatted_date = compute_formatted_date_from_gtfs_folder_path(dataset_folder_path)
    if formatted_date is None:
        print(f"CANT read date from GTFS archive: '{dataset_filename}'")
        sys.exit(1)

    gtfs_agency_path = f'{dataset_folder_path}/agency.txt'
    if os.path.isfile(gtfs_agency_path):
        print(f'The {dataset_filename} is already downloaded / extracted at')
        print(f'{dataset_folder_path}')
        sys.exit(0)
    
    gtfs_static_zip_path = Path(f'{gtfs_static_base_path}/{dataset_filename}')
    if os.path.isfile(gtfs_static_zip_path):
        run_unzip(gtfs_static_zip_path, dataset_folder_path)
        sys.exit(0)

    print(f'Downloading {dataset_url} to {gtfs_static_zip_path} ...')
    curl_sh = f'curl {dataset_url} -o {gtfs_static_zip_path}'
    print(curl_sh, flush=True)
    os.system(curl_sh)
    print('DONE')

    run_unzip(gtfs_static_zip_path, dataset_folder_path)
Exemplo n.º 2
0
def main():
    script_path = Path(os.path.realpath(__file__))
    app_config_path = Path(f'{script_path.parent}/inc/config.yml')
    if not os.path.isfile(app_config_path):
        print(f'ERROR: cant load config from {app_config_path}')
        sys.exit(1)
    app_config = load_yaml_config(app_config_path, script_path.parent)

    usage_help_s = 'Use --gtfs-db-path /path/to/gtfs-db-path --hrdf-db-path /path/to/hrdf-db-path'

    parser = argparse.ArgumentParser()
    parser.add_argument('--gtfs-db-path', '--gtfs-db-path')
    parser.add_argument('--hrdf-db-path', '--hrdf-db-path')
    parser.add_argument('--day', '--day')
    parser.add_argument('--agency_id', '--agency_id')
    args = parser.parse_args()

    if not args.gtfs_db_path:
        print(f'Missing GTFS DB path')
        print(usage_help_s)
        sys.exit(1)
    if not args.hrdf_db_path:
        print(f'Missing HRDF DB path')
        print(usage_help_s)
        sys.exit(1)
    if not os.path.isfile(args.gtfs_db_path):
        print(f'GTFS DB path is invalid')
        print(f'{args.gtfs_db_path}')
        sys.exit(1)
    if not os.path.isfile(args.hrdf_db_path):
        print(f'GTFS B(current) path is invalid')
        print(f'{args.hrdf_db_path}')
        sys.exit(1)

    request_day = datetime.today().date()
    request_day_s = args.day
    if request_day_s:
        request_day = datetime.strptime(request_day_s, "%Y-%m-%d").date()
    else:
        print(f'- using current day {request_day} as --day parameter')

    agency_id = args.agency_id

    gtfs_db_path = Path(args.gtfs_db_path)
    hrdf_db_path = Path(args.hrdf_db_path)

    gtfs_hrdf_controller = GTFS_HRDF_Compare_Controller(app_config, gtfs_db_path, hrdf_db_path)
    gtfs_hrdf_controller.compare(request_day, agency_id)
Exemplo n.º 3
0
def main():
    print('SETUP server')

    script_path = Path(os.path.realpath(__file__))

    app_config_path = Path(f'{script_path.parent}/inc/config.yml')
    if not os.path.isfile(app_config_path):
        print(f'ERROR: cant load config from {app_config_path}')
        sys.exit(1)
    app_config = load_yaml_config(app_config_path, script_path.parent)

    for key in app_config['tmp-data-paths']:
        dir_path = app_config['tmp-data-paths'][key]
        if os.path.isdir(dir_path):
            print(f'... folder {dir_path} exists, skipping ...')
            continue

        print(f'... creating {dir_path}')
        os.makedirs(dir_path, exist_ok=True)

    env_file_version = app_config['env_file_version']

    env_file_lines = [
        '# Generated by setup.py',
        '',
        f'APP_VERSION={env_file_version}',
    ]
    for env_key in app_config['env_data_keys']:
        env_value = app_config['env_data_keys'][env_key]
        env_file_line = f'{env_key}={env_value}'
        env_file_lines.append(env_file_line)
    env_file_lines_s = "\n".join(env_file_lines)

    env_file_path = app_config['env_file_path']
    env_file = open(env_file_path, 'w')
    env_file.write(env_file_lines_s)
    env_file.close()

    print(f'Wrote to {env_file_path}')
Exemplo n.º 4
0
import argparse, os, sys, glob
from pathlib import Path

from inc.gtfs_static_files_generator_controller import GTFS_Static_Files_Generator_Controller
from inc.shared.inc.helpers.config_helpers import load_yaml_config

script_path = Path(os.path.realpath(__file__))

parser = argparse.ArgumentParser(description = '')
parser.add_argument('-db-path', '--db-path', help='Path to GTFS DB')
args = parser.parse_args()

gtfs_db_path = args.db_path
if gtfs_db_path is None:
    print("ERROR, use with --db-path")
    sys.exit(1)
gtfs_db_path = Path(os.path.abspath(gtfs_db_path))

app_config_path = Path(f'{script_path.parent}/inc/config/config.yml')
if not os.path.isfile(app_config_path):
    print(f'ERROR: cant load config from {app_config_path}')
    sys.exit(1)
app_config = load_yaml_config(app_config_path, script_path.parent)

gtfs_static_generator = GTFS_Static_Files_Generator_Controller(gtfs_db_path, app_config)
gtfs_static_generator.generate_weekday_files()
def main():
    script_path = Path(os.path.realpath(__file__))
    app_config_path = Path(f'{script_path.parent}/inc/config.yml')
    if not os.path.isfile(app_config_path):
        print(f'ERROR: cant load config from {app_config_path}')
        sys.exit(1)
    app_config = load_yaml_config(app_config_path, script_path.parent)

    parser = argparse.ArgumentParser()
    parser.add_argument('--gtfs-base-folder-path', '--gtfs-base-folder-path')
    parser.add_argument('--gtfs-db-base-folder-path',
                        '--gtfs-db-base-folder-path')
    parser.add_argument('--force', '--force', action='store_true')
    args = parser.parse_args()

    gtfs_base_folder_path = args.gtfs_base_folder_path
    if not gtfs_base_folder_path:
        print(
            f'ERROR, use it with --gtfs-base-folder-path /path/to/GTFS_base_path'
        )
        sys.exit(1)

    gtfs_base_folder_path = Path(gtfs_base_folder_path)
    if not os.path.isdir(gtfs_base_folder_path):
        print(f'ERROR, --gtfs-base-folder-path doesn\'t exist')
        print(f'{gtfs_base_folder_path}')
        sys.exit(1)

    gtfs_db_base_folder_path = args.gtfs_db_base_folder_path
    if not gtfs_db_base_folder_path:
        print(
            f'ERROR, use it with --gtfs-db-base-folder-path /path/to/GTFS_DB_base_path'
        )
        sys.exit(1)

    gtfs_db_base_folder_path = Path(gtfs_db_base_folder_path)
    if not os.path.isdir(gtfs_db_base_folder_path):
        print(f'ERROR, --gtfs-db-base-folder-path doesn\'t exist')
        print(f'{gtfs_db_base_folder_path}')
        sys.exit(1)

    override_db = args.force == True

    gtfs_dataset_paths = glob.glob(f'{gtfs_base_folder_path}/*')
    gtfs_dataset_paths.sort(reverse=True)

    latest_gtfs_dataset_path = None
    gtfs_dataset_formatted_date = None
    for gtfs_dataset_path in gtfs_dataset_paths:
        if not os.path.isdir(gtfs_dataset_path):
            continue

        gtfs_dataset_path = Path(gtfs_dataset_path)
        gtfs_dataset_formatted_date = compute_formatted_date_from_gtfs_folder_path(
            gtfs_dataset_path)
        if gtfs_dataset_formatted_date is None:
            continue

        latest_gtfs_dataset_path = gtfs_dataset_path
        break

    if not latest_gtfs_dataset_path:
        print(f'ERROR - cant find a GTFS dataset in {gtfs_base_folder_path}')
        sys.exit(1)

    db_filename = f"gtfs_{gtfs_dataset_formatted_date}.sqlite"
    db_path = Path(f'{gtfs_db_base_folder_path}/{db_filename}')

    if os.path.isfile(db_path):
        if override_db:
            print(f'Will override the content in DB at path {db_path} ...')
        else:
            print(
                f'DB for {latest_gtfs_dataset_path} already exists at {db_path}'
            )
            print(f'If you want to overwrite:')
            print(f'- remove the file manually then run this script again')
            print(f'- use --force')
            sys.exit(0)

    print(f'===================================================')
    print(f'GTFS DB IMPORT')
    print(f'  GTFS input path   : {latest_gtfs_dataset_path}')
    print(f'  DB output path    : {db_path}')
    print(f'===================================================', flush=True)

    latest_gtfs_dataset_path = Path(latest_gtfs_dataset_path)

    gtfs_importer = GTFS_DB_Importer(app_config, latest_gtfs_dataset_path,
                                     db_path)

    gtfs_importer.start()