"""Utils for bootstraping the flow.""" from feed_sender.flows import helpers from feed_sender.flows.proper_new_releases_tracks.conf import settings from feed_sender.util.aws import s3 as s3LocalUtils def _get_destination_s3_path(is_tracklistings, context_date): """Get full destination S3 path. Args: is_tracklisting (bool): True if S3 object is the track listing file. context_date (str): Context date. Returns: str: Full destination S3 path for releases or tracklistings file. """ destination_s3_path = settings.NEW_RELEASES_TRACKLISTS_S3_PATH.format( date=context_date) new_releases_filename = settings.NEW_RELEASES_FILENAME.format( date=context_date.replace('-', '_')) new_tracklistings_filename = settings.NEW_TRACKLISTINGS_FILENAME.format( date=context_date.replace('-', '_')) if is_tracklistings: return '{}{}'.format(destination_s3_path, new_tracklistings_filename) return '{}{}'.format(destination_s3_path, new_releases_filename) def _get_destination_sftp_path(is_tracklistings, context_date): """Get full destination SFTP remote path. Note that this is a separated method from the one getting S3 path because SFTP path is still unknown. There can be special logic to get the destination SFTP path. If we confirm that later on code is going to be similar, we can combine the two methods. Args: is_tracklisting (bool): True if S3 object is the track listing file. context_date (str): Context date. Returns: str: Full destination SFTP path for releases or tracklistings file. """ destination_sftp_path = settings.SFTP_REMOTE_PATH.format(date=context_date) new_releases_filename = settings.NEW_RELEASES_FILENAME.format( date=context_date.replace('-', '_')) new_tracklistings_filename = settings.NEW_TRACKLISTINGS_FILENAME.format( date=context_date.replace('-', '_')) if is_tracklistings: return '{}{}'.format(destination_sftp_path, new_tracklistings_filename) return '{}{}'.format(destination_sftp_path, new_releases_filename) def feed_already_sent(feed_name, context_date, s3_only=False): """Check if feed has really sent out. Feed has already sent out if both release and track files exist on S3 and status is either UPLOADED_TO_FTP or SENT (uploaded and with the history tables updated). Args: feed_name (str): Name of the feed. context_date (str): Context date. s3_only (bool): True will skip the check for files on SFTP. Return: bool: True if feed has already been sent. """ # Check if both new releases and tracks files exist on S3. track_file_exist = s3LocalUtils.check_s3_key_exist( _get_destination_s3_path(True, context_date)) release_file_exist = s3LocalUtils.check_s3_key_exist( _get_destination_s3_path(False, context_date)) # Check if status is SENT or UPLOADED in DynamoDB table. status_set_to_sent = helpers.get_status( feed_name, context_date) == helpers.STATUS_SENT or helpers.get_status( feed_name, context_date) == helpers.STATUS_UPLOADED_TO_FTP checks = track_file_exist and release_file_exist and status_set_to_sent return checks