Skip to content
Snippets Groups Projects

remove system exits from datafetcher tests

Merged Janet Goldstein requested to merge WS-179-4-remove-sys-exits into main
All threads resolved!
10 files
+ 153
382
Compare changes
  • Side-by-side
  • Inline
Files
10
@@ -8,9 +8,8 @@ import sys
from argparse import Namespace
from pathlib import Path
from datafetcher.errors import MissingSettingsException, NoProfileException
from datafetcher.errors import MissingSettingsException, NoProfileException, DataFetcherException
from datafetcher.project_fetcher import ParallelFetcher
from datafetcher.return_codes import ReturnCode
from .locations_report import LocationsReport
from .utilities import get_arg_parser, get_capo_settings, FlexLogger, path_is_accessible
@@ -48,7 +47,7 @@ class DataFetcher:
def __init__(self, args: Namespace, df_capo_settings: dict):
self.usage = self._build_usage_message()
if args is None or df_capo_settings is None:
self._exit_with_error(ReturnCode.MISSING_SETTING)
raise MissingSettingsException()
self.args = args
self.settings = df_capo_settings
self.verbose = self.args.verbose
@@ -56,52 +55,39 @@ class DataFetcher:
# required arguments
self.output_dir = args.output_dir
if self.output_dir is None:
self._exit_with_error(ReturnCode.MISSING_SETTING)
raise MissingSettingsException("output directory option is missing")
output_dir = Path(self.output_dir)
if not output_dir.is_dir() or not path_is_accessible(output_dir):
logging.error(f"output location {self.output_dir} inaccessible " f"or not found")
self._exit_with_error(ReturnCode.MISSING_SETTING)
raise MissingSettingsException(
f"output location {self.output_dir} inaccessible or not found"
)
self._LOG = FlexLogger(self.__class__.__name__, self.output_dir, self.verbose)
if args.location_file is not None:
if args.product_locator is not None:
self._LOG.error("required: location file OR product locator " "-- not both")
self._exit_with_error(ReturnCode.MISSING_SETTING)
raise MissingSettingsException(
"required: location file OR product locator -- not both"
)
self.location_file = args.location_file
elif args.product_locator is not None:
self.product_locator = args.product_locator
else:
self._LOG.error("you must specify either a location file or a " "product locator")
self._exit_with_error(ReturnCode.MISSING_SETTING)
raise MissingSettingsException(
"you must specify either a location file or a product locator"
)
self.profile = args.profile
if self.profile is None:
self._exit_with_error(ReturnCode.MISSING_PROFILE)
raise NoProfileException()
# optional arguments
self.is_dry = args.dry_run
self.force = args.force
self.verbose = args.verbose or False
try:
self.locations_report = self._get_locations()
self.servers_report = self.locations_report.servers_report
except SystemExit as exc:
self._LOG.error(f"{exc}")
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 _exit_with_error(self, return_code: ReturnCode):
print(self.usage)
sys.exit(return_code.value["code"])
self.locations_report = self._get_locations()
self.servers_report = self.locations_report.servers_report
@staticmethod
def _build_usage_message() -> str:
@@ -110,9 +96,6 @@ class DataFetcher:
(--product-locator PRODUCT_LOCATOR | --location-file LOCATION_FILE)
[--dry-run] [--output-dir OUTPUT_DIR] [-v, --verbose]
[--profile PROFILE]\n"""
usage_str += "\n\t\tReturn codes:\n"
for return_code in ReturnCode:
usage_str += f"\n\t\t\t{return_code.value['code']}: " f"{return_code.value['text']}"
return usage_str
def run(self):
@@ -121,36 +104,12 @@ class DataFetcher:
:return:
"""
try:
fetcher = ParallelFetcher(self.args, self.settings, self._LOG, self.servers_report)
return fetcher.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
fetcher = ParallelFetcher(self.args, self.settings, self._LOG, self.servers_report)
return fetcher.run()
def _get_locations(self):
try:
capo_settings = get_capo_settings(self.profile)
except MissingSettingsException as exc:
# if a setting couldn't be found, we can be pretty sure that
# the profile's no good
raise NoProfileException from exc
try:
return LocationsReport(self._LOG, self.args, capo_settings)
except Exception as exc:
self._LOG.error(f"{exc}")
if hasattr(self, "location_file"):
self._exit_with_error(ReturnCode.CANNOT_OPEN_LOCATION_FILE)
elif hasattr(self, "product_locator"):
self._exit_with_error(ReturnCode.PRODUCT_LOCATOR_NOT_FOUND)
else:
# should never happen; we've already checked
self._exit_with_error(ReturnCode.MISSING_SETTING)
capo_settings = get_capo_settings(self.profile)
return LocationsReport(self._LOG, self.args, capo_settings)
def main():
@@ -161,15 +120,12 @@ def main():
parser = get_arg_parser()
try:
args = parser.parse_args()
except Exception as exc:
logging.error(f"{exc}")
return exc.value
except SystemExit as exc:
logging.error(f"{exc}")
return exc.value.code
settings = get_capo_settings(args.profile)
datafetcher = DataFetcher(args, settings)
return datafetcher.run()
settings = get_capo_settings(args.profile)
datafetcher = DataFetcher(args, settings)
return datafetcher.run()
except DataFetcherException as exc:
logging.exception(exc.message)
sys.exit(exc.code)
if __name__ == "__main__":
Loading