[PR #11763] [MERGED] Fixes #11226 - Change the way we invalidate the module cache to support reloading code from subpackages #13838

Closed
opened 2025-12-29 23:21:05 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/11763
Author: @kkthxbye-code
Created: 2/16/2023
Status: Merged
Merged: 2/16/2023
Merged by: @jeremystretch

Base: developHead: 11226-fix-script-reloading


📝 Commits (1)

  • a778ef1 Change the way we invalidate the module cache to support reloading code from subpackages

📊 Changes

1 file changed (+19 additions, -7 deletions)

View changed files

📝 netbox/extras/scripts.py (+19 -7)

📄 Description

Fixes: #11226

This PR intends to fix cases where script and utility code loaded from subpackages wasn't getting reloaded after changes were made.

The way this is done, is by taking the base module path (the string before the first dot), so if a script module import path is my_submodule.my_script_file.MyScriptClass we would delete any module from sys.modules where the base my_submodule matches.

Furthermore we remove any module from sys.modules where the base path equals scripts. This was added, because utility functions imported via. the normal import statement, would be imported like:

from scripts.util.util_file import util_function

Which would be present in sys.modules as:

scripts.util.util_file

Removing all modules from the cache which starts with scripts solves this issue, and from my testing causes no other issues. I tested this as well as I could, with the main focus being the default value for SCRIPT_ROOT. Subpackage importing when using a non-default SCRIPT_ROOT is still not great, but outside the scope of this PR.

In short you can now modify the following without reloading netbox or the rqworker:

  • The name of the script class in subpackages
  • Script variables in subpackages
  • Imported utility functions

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/11763 **Author:** [@kkthxbye-code](https://github.com/kkthxbye-code) **Created:** 2/16/2023 **Status:** ✅ Merged **Merged:** 2/16/2023 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `11226-fix-script-reloading` --- ### 📝 Commits (1) - [`a778ef1`](https://github.com/netbox-community/netbox/commit/a778ef1f622c9057021ca3171392133f27c96953) Change the way we invalidate the module cache to support reloading code from subpackages ### 📊 Changes **1 file changed** (+19 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `netbox/extras/scripts.py` (+19 -7) </details> ### 📄 Description ### Fixes: #11226 This PR intends to fix cases where script and utility code loaded from subpackages wasn't getting reloaded after changes were made. The way this is done, is by taking the base module path (the string before the first dot), so if a script module import path is `my_submodule.my_script_file.MyScriptClass` we would delete any module from sys.modules where the base `my_submodule` matches. Furthermore we remove any module from sys.modules where the base path equals `scripts`. This was added, because utility functions imported via. the normal import statement, would be imported like: `from scripts.util.util_file import util_function` Which would be present in sys.modules as: `scripts.util.util_file` Removing all modules from the cache which starts with `scripts` solves this issue, and from my testing causes no other issues. I tested this as well as I could, with the main focus being the default value for `SCRIPT_ROOT`. Subpackage importing when using a non-default `SCRIPT_ROOT` is still not great, but outside the scope of this PR. In short you can now modify the following without reloading netbox or the rqworker: * The name of the script class in subpackages * Script variables in subpackages * Imported utility functions --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 23:21:05 +01:00
adam closed this issue 2025-12-29 23:21:05 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#13838