"""A Task to make snapshots of all run summaries.""" import os.path from lib import config from lib.utils import aws from lib.utils import ows from tasks.accounting_period_calculate_vat import helpers def snapshot_run_summaries_task(dag_run, **kwargs): """Copy run summaries into vat's sub-folder.""" event = helpers.get_event_from_params(dag_run, **kwargs) accounting_period = ows.get_accounting_period_details(event.target_id) accounting_period_id = accounting_period.get('accounting_period_id') summaries_snapshot_s3_path = helpers.build_snapshot_path_for_module( accounting_period_id, accounting_period.get('accounting_period_name'), module_name='summaries' ) accounting_run_summaries = helpers.get_accounting_run_export_urls_by_period( accounting_period_id ) for url in accounting_run_summaries: key_info = aws.split_path(url) _, file_name = os.path.split(key_info.key) aws.copy_file( source_bucket=config.S3_SALES_BUCKET_NAME, source_key=key_info.key, dest_key=aws.join( summaries_snapshot_s3_path, file_name ) )