Exemplo n.º 1
0
def delete_expired_reports(config_file, google_secrets_file, age_in_days):
    """
    Performs the partner report deletion as needed.
    """
    LOG('Starting partner report deletion using config file "{}", Google config "{}", and {} days back'
        .format(config_file, google_secrets_file, age_in_days))

    if not config_file:
        FAIL(ERR_NO_CONFIG, 'No config file passed in.')

    if not google_secrets_file:
        FAIL(ERR_NO_SECRETS, 'No secrets file passed in.')

    if age_in_days <= 0:
        FAIL(ERR_BAD_AGE, 'age_in_days must be a positive integer.')

    config = _config_or_exit(config_file, google_secrets_file)

    try:
        delete_before_dt = datetime.now(UTC) - timedelta(days=age_in_days)
        drive = DriveApi(config['google_secrets_file'])
        LOG('DriveApi configured')
        drive.delete_files_older_than(
            config['drive_partners_folder'],
            delete_before_dt,
            mimetype='text/csv',
            prefix="{}_{}".format(REPORTING_FILENAME_PREFIX,
                                  config['partner_report_platform_name']))
        LOG('Partner report deletion complete')
    except Exception as exc:  # pylint: disable=broad-except
        FAIL_EXCEPTION(ERR_DELETING_REPORTS, 'Unexpected error occurred!', exc)
Exemplo n.º 2
0
 def test_delete_files_older_than(self, mock_from_service_account_file):  # pylint: disable=unused-argument
     """
     Tests the logic to delete files older than a certain age.
     """
     five_days_ago = datetime.now(UTC) - timedelta(days=5)
     fake_newish_files = [
         {
             'id': 'fake-text-file-id-{}'.format(idx),
             'createdTime': five_days_ago + timedelta(days=1),
             'mimeType': 'text/plain'
         }
         for idx in range(1, 10, 2)
     ]
     fake_old_files = [
         {
             'id': 'fake-text-file-id-{}'.format(idx),
             'createdTime': five_days_ago - timedelta(days=14),
             'mimeType': 'text/plain'
         }
         for idx in range(2, 10, 2)
     ]
     fake_files = fake_newish_files + fake_old_files
     http_mock_sequence = HttpMockSequence([
         # First, a request is made to the discovery API to construct a client object for Drive.
         (
             {'status': '200'},
             self.mock_discovery_response_content,
         ),
         # Then, a request is made to list files.
         (
             {'status': '200', 'content-type': 'application/json'},
             json.dumps({'files': fake_files}, default=lambda x: x.isoformat()).encode('utf-8'),
         ),
     ])
     with patch.object(DriveApi, 'delete_files', return_value=None) as mock_delete_files:
         test_client = DriveApi('non-existent-secrets.json', http=http_mock_sequence)
         test_client.delete_files_older_than('fake-folder-id', five_days_ago)
     # Verify that the correct files were requested to be deleted.
     mock_delete_files.assert_called_once_with(['fake-text-file-id-{}'.format(idx) for idx in range(2, 10, 2)])