Răsfoiți Sursa

fix(core): Keep full paths in data source syncs (#23550)

DataSource._walk() split each directory path on every occurrence of
the source root, so a directory repeating the root string, such as
/scripts/scripts_old under /scripts, had its relative path truncated.
The sync then failed on its files or skipped them. Only the leading
root is now removed.

Fixes #23542
Martin Hauser 1 zi în urmă
părinte
comite
f515e774ac
2 a modificat fișierele cu 16 adăugiri și 1 ștergeri
  1. 1 1
      netbox/core/models/data.py
  2. 15 0
      netbox/core/tests/test_models.py

+ 1 - 1
netbox/core/models/data.py

@@ -273,7 +273,7 @@ class DataSource(JobsMixin, PrimaryModel):
         paths = set()
 
         for path, dir_names, file_names in os.walk(root):
-            path = path.split(root)[1].lstrip('/')  # Strip root path
+            path = path.removeprefix(root).lstrip('/')  # Strip root path
             if path.startswith('.'):
                 continue
             for file_name in file_names:

+ 15 - 0
netbox/core/tests/test_models.py

@@ -275,6 +275,21 @@ class DataSourceTestCase(TestCase):
 
         self.assertEqual(dict(self.datasource.datafiles.values_list('path', 'data')), {'a.txt': b'new\n'})
 
+    def test_sync_keeps_paths_repeating_source_root(self):
+        """A file below a directory repeating the source root syncs under its full path."""
+        # Repeat the whole root string below it, as /scripts/scripts_old does for /scripts
+        nested_path = f'{self.root}_old/legacy.py'.lstrip('/')
+        (self.root / nested_path).parent.mkdir(parents=True)
+        (self.root / nested_path).write_text('nested\n')
+        (self.root / 'legacy.py').write_text('root\n')
+
+        self.datasource.sync()
+
+        self.assertEqual(
+            dict(self.datasource.datafiles.values_list('path', 'data')),
+            {'a.txt': b'new\n', 'b.txt': b'new\n', 'legacy.py': b'root\n', nested_path: b'nested\n'},
+        )
+
 
 class ObjectTypeTestCase(TestCase):