Catch import errors during backend init

This commit is contained in:
Jeremy Stretch 2023-08-01 09:41:29 -04:00
parent cb7cf1f66a
commit a9450a1929
3 changed files with 9 additions and 38 deletions

View File

@ -60,7 +60,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install boto3 dulwich
pip install pycodestyle coverage tblib
- name: Build documentation

View File

@ -54,13 +54,6 @@ class DataBackend:
"""
return
def handle_missing_dependency(self, e):
"""
Hook for handling exceptions related to the attempted import of missing modules. Returns an exception
to be raised.
"""
return e
@property
def url_scheme(self):
return urlparse(self.url).scheme.lower()
@ -105,10 +98,7 @@ class GitBackend(DataBackend):
sensitive_parameters = ['password']
def init_config(self):
try:
from dulwich.config import ConfigDict
except ModuleNotFoundError as e:
raise self.handle_missing_dependency(e)
# Initialize backend config
config = ConfigDict()
@ -120,18 +110,9 @@ class GitBackend(DataBackend):
return config
def handle_missing_dependency(self, e):
return ImportError(_(
"Unable to initialize the git data backend: dulwich library is not installed. Run 'pip install dulwich' "
"within the NetBox Python environment to install it."
))
@contextmanager
def fetch(self):
try:
from dulwich import porcelain
except ModuleNotFoundError as e:
raise self.handle_missing_dependency(e)
local_path = tempfile.TemporaryDirectory()
@ -179,28 +160,16 @@ class S3Backend(DataBackend):
REGION_REGEX = r's3\.([a-z0-9-]+)\.amazonaws\.com'
def init_config(self):
try:
from botocore.config import Config as Boto3Config
except ModuleNotFoundError as e:
raise self.handle_missing_dependency(e)
# Initialize backend config
return Boto3Config(
proxies=settings.HTTP_PROXIES,
)
def handle_missing_dependency(self, e):
return ImportError(_(
"Unable to initialize the Amazon S3 backend: boto3 library is not installed. Run 'pip install boto3' "
"within the NetBox Python environment to install it."
))
@contextmanager
def fetch(self):
try:
import boto3
except ModuleNotFoundError as e:
raise self.handle_missing_dependency(e)
local_path = tempfile.TemporaryDirectory()

View File

@ -151,7 +151,7 @@ class DataSource(JobsMixin, PrimaryModel):
Create/update/delete child DataFiles as necessary to synchronize with the remote source.
"""
if self.status == DataSourceStatusChoices.SYNCING:
raise SyncError(f"Cannot initiate sync; syncing already in progress.")
raise SyncError("Cannot initiate sync; syncing already in progress.")
# Emit the pre_sync signal
pre_sync.send(sender=self.__class__, instance=self)
@ -160,7 +160,10 @@ class DataSource(JobsMixin, PrimaryModel):
DataSource.objects.filter(pk=self.pk).update(status=self.status)
# Replicate source data locally
try:
backend = self.get_backend()
except ModuleNotFoundError as e:
raise SyncError(f"There was an error initializing the backend: {e}")
with backend.fetch() as local_path:
logger.debug(f'Syncing files from source root {local_path}')