Skip to content
Snippets Groups Projects
mock_data_fetcher.py 2.52 KiB
""" for testing the attempt to copy rather than stream files """

import sys
from argparse import Namespace

from datafetcher.locations_report import LocationsReport
from datafetcher.project_fetcher import ParallelFetcher
from datafetcher.return_codes import ReturnCode
from datafetcher.utilities import get_capo_settings, ExecutionSite, FlexLogger

from .df_pytest_utils import TEST_PROFILE

# pylint: disable=C0103, R0201, R0902, R0903, W0621


class MockProdDataFetcher:
    """ Creates and launches a datafetcher using the dsoc-prod profile """

    def __init__(self, args: Namespace, settings: dict):
        if args is None or settings is None:
            self._exit_with_error(ReturnCode.MISSING_SETTING)
        self.args = args
        self.settings = settings
        self.verbose = self.args.verbose

        self.output_dir = args.output_dir
        self.profile = args.profile

        self._LOG = FlexLogger(self.__class__.__name__,
                               self.output_dir, self.verbose)

        try:
            self.locations_report = self._get_locations()
            self.servers_report = self.locations_report.servers_report
        except SystemExit:
            if args.location_file:
                self._exit_with_error(ReturnCode.CANNOT_OPEN_LOCATION_FILE)
            else:
                self._exit_with_error(ReturnCode.PRODUCT_LOCATOR_NOT_FOUND)
            raise

        except Exception as exc:
            self._LOG.error(
                f'>>> throwing unexpected {type(exc)} during init: {exc}')
            raise

    def _get_locations(self):
        """
        Create a locations report with DSOC as exec site
            to force copy rather than stream
        :return:
        """

        capo_settings = get_capo_settings(TEST_PROFILE)
        capo_settings['execution_site'] = ExecutionSite.DSOC.value

        return LocationsReport(self._LOG,
                               self.args,
                               capo_settings)

    def run(self):
        """
        identical to DataFetcher.run()
        :return:
        """

        try:
            return ParallelFetcher(
                self.args, self.settings, self._LOG,
                self.servers_report).run()
        except SystemExit as exc:
            self._LOG.error(f'{exc}')
            raise
        except Exception as exc:
            self._LOG.error(
                f'>>> throwing unexpected exception during run: {exc}')
            raise

    def _exit_with_error(self, return_code: ReturnCode):
        sys.exit(return_code.value['code'])