diff --git a/netbox/extras/migrations/0091_create_managedfiles.py b/netbox/extras/migrations/0091_create_managedfiles.py index 6921247a4..79a80821f 100644 --- a/netbox/extras/migrations/0091_create_managedfiles.py +++ b/netbox/extras/migrations/0091_create_managedfiles.py @@ -8,16 +8,9 @@ import extras.models.mixins def create_files(cls, root_name, root_path): - path_tree = [ - path for path, _, _ in os.walk(root_path) - if os.path.basename(path)[0] not in ('_', '.') - ] - - modules = list(pkgutil.iter_modules(path_tree)) + modules = list(pkgutil.iter_modules([root_path])) filenames = [] - for importer, module_name, is_pkg in modules: - if is_pkg: - continue + for importer, module_name, ispkg in modules: try: module = importer.find_module(module_name).load_module(module_name) rel_path = os.path.relpath(module.__file__, root_path) diff --git a/netbox/extras/models/mixins.py b/netbox/extras/models/mixins.py index 82b0b9983..ab04245ad 100644 --- a/netbox/extras/models/mixins.py +++ b/netbox/extras/models/mixins.py @@ -1,5 +1,5 @@ import os -from pkgutil import ModuleInfo, get_importer +from importlib.machinery import SourceFileLoader __all__ = ( 'PythonModuleMixin', @@ -12,16 +12,17 @@ class PythonModuleMixin: def path(self): return os.path.splitext(self.file_path)[0] - def get_module_info(self): - path = os.path.dirname(self.full_path) - module_name = os.path.basename(self.path) - return ModuleInfo( - module_finder=get_importer(path), - name=module_name, - ispkg=False - ) + @property + def pymodule_name(self): + path, filename = os.path.split(self.full_path) + name = os.path.splitext(filename)[0] + if name == '__init__': + # File is a package + return os.path.basename(path) + else: + return name def get_module(self): - importer, module_name, _ = self.get_module_info() - module = importer.find_module(module_name).load_module(module_name) + loader = SourceFileLoader(self.pymodule_name, self.full_path) + module = loader.load_module() return module diff --git a/netbox/extras/models/scripts.py b/netbox/extras/models/scripts.py index 29c8085db..257a7b701 100644 --- a/netbox/extras/models/scripts.py +++ b/netbox/extras/models/scripts.py @@ -1,7 +1,6 @@ import inspect from functools import cached_property -from django.contrib.contenttypes.fields import GenericRelation from django.db import models from django.urls import reverse @@ -51,10 +50,7 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile): # For child objects in submodules use the full import path w/o the root module as the name return cls.full_name.split(".", maxsplit=1)[1] - try: - module = self.get_module() - except ImportError: - return {} + module = self.get_module() scripts = {} ordered = getattr(module, 'script_order', []) diff --git a/netbox/templates/extras/report_list.html b/netbox/templates/extras/report_list.html index 58eefa4f7..ea774ce1d 100644 --- a/netbox/templates/extras/report_list.html +++ b/netbox/templates/extras/report_list.html @@ -34,7 +34,7 @@ {% endif %} - {{ module.name|bettertitle }} + {{ module.name }}