Quellcode durchsuchen

logic to move nam builds

Johan Le Baut vor 1 Woche
Ursprung
Commit
0bc595a318

Datei-Diff unterdrückt, da er zu groß ist
+ 1 - 2
README.md


+ 1 - 0
config/desired-state.yaml

@@ -40,6 +40,7 @@ deployment:
     archive_url: https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/v0.2.3/neural_amp_modeler_lv2_rpi5.tgz
     artifact_directory: artifacts/nam-lv2/arm64/current
     bundle_directory: neural_amp_modeler.lv2
+    backup_directory: .local/state/rigforge/namlv2-backups
   lsp:
     tag: "1.2.37"
     archive_url: https://github.com/lsp-plugins/lsp-plugins/releases/download/1.2.37/lsp-plugins-1.2.37-Linux-aarch64.7z

+ 68 - 5
scripts/deploy.py

@@ -74,6 +74,8 @@ def load_deployment():
         errors.append("NAM LV2 URL must be the approved release asset for the declared tag")
     if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
         errors.append("NAM LV2 bundle directory is invalid")
+    if namlv2.get("backup_directory") != ".local/state/rigforge/namlv2-backups":
+        errors.append("NAM LV2 backup directory is invalid")
     source = pipewire.get("fragment_source")
     if source != "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf":
         errors.append("PipeWire fragment source is invalid")
@@ -170,6 +172,55 @@ def validate_backup_path(backup, destination, is_directory=False):
     return backup_path
 
 
+def namlv2_backup_path(config, destination):
+    relative = PurePosixPath(config.get("backup_directory", ""))
+    if relative.is_absolute() or ".." in relative.parts or not relative.parts:
+        raise RuntimeError("NAM LV2 backup directory must be a safe relative path")
+    home = Path.home().resolve()
+    backup_root = (home / Path(*relative.parts)).resolve()
+    if not backup_root.is_relative_to(home):
+        raise RuntimeError("NAM LV2 backup directory escapes the user home directory")
+    backup = backup_root / f"{destination.name}.rigforge-backup"
+    validate_user_destination(backup)
+    return backup
+
+
+def validate_nam_backup_path(backup, destination, config):
+    if backup is None:
+        return None
+    backup_path = Path(backup)
+    approved = {
+        destination.with_name(destination.name + ".rigforge-backup"),
+        namlv2_backup_path(config, destination),
+    }
+    if backup_path not in approved:
+        raise RuntimeError(f"recorded NAM LV2 backup path is outside approved locations: {backup_path}")
+    if backup_path.is_symlink():
+        raise RuntimeError(f"recorded NAM LV2 backup must not be a symlink: {backup_path}")
+    if backup_path.exists() and not backup_path.is_dir():
+        raise RuntimeError(f"recorded NAM LV2 backup has the wrong type: {backup_path}")
+    return backup_path
+
+
+def migrate_namlv2_backup(config, backup, destination, backup_sha256):
+    """Move legacy in-~/.lv2 backups out of Lilv's plugin search path."""
+    if backup is None or not backup.exists():
+        return backup
+    target = namlv2_backup_path(config, destination)
+    if backup == target:
+        return backup
+    legacy = destination.with_name(destination.name + ".rigforge-backup")
+    if backup != legacy:
+        return backup
+    if tree_sha256(backup) != backup_sha256:
+        raise RuntimeError(f"recorded NAM LV2 backup changed locally: {backup}")
+    if target.exists() or target.is_symlink():
+        raise RuntimeError(f"NAM LV2 backup destination already exists: {target}")
+    target.parent.mkdir(parents=True, exist_ok=True)
+    os.replace(backup, target)
+    return target
+
+
 def atomic_install(source, destination):
     destination.parent.mkdir(parents=True, exist_ok=True)
     fd, temporary_name = tempfile.mkstemp(prefix=f".{destination.name}.", dir=destination.parent)
@@ -738,7 +789,9 @@ def deploy_namlv2(action, config, registry):
             print("No managed NAM LV2 bundle to uninstall.")
             return
         if managed.get("pending_uninstall"):
-            backup_path = validate_backup_path(managed.get("backup"), destination, is_directory=True)
+            backup_path = validate_nam_backup_path(
+                managed.get("backup"), destination, config
+            )
             backup_exists = backup_path is not None and backup_path.is_dir()
             if backup_path is not None and backup_path.exists() and not backup_exists:
                 raise RuntimeError(f"recorded backup has the wrong type: {backup_path}")
@@ -768,7 +821,9 @@ def deploy_namlv2(action, config, registry):
             raise RuntimeError(f"managed NAM LV2 path changed: {destination}")
         if tree_sha256(destination) != managed.get("installed_sha256"):
             raise RuntimeError(f"managed NAM LV2 bundle changed locally; refusing to remove: {destination}")
-        backup_path = validate_backup_path(managed.get("backup"), destination, is_directory=True)
+        backup_path = validate_nam_backup_path(
+            managed.get("backup"), destination, config
+        )
         if backup_path is not None and not backup_path.is_dir():
             raise RuntimeError(f"recorded backup bundle is missing: {backup_path}")
         if backup_path is not None and tree_sha256(backup_path) != managed.get("backup_sha256"):
@@ -791,15 +846,22 @@ def deploy_namlv2(action, config, registry):
             raise RuntimeError(f"managed NAM LV2 bundle is missing: {destination}")
         if tree_sha256(destination) != managed.get("installed_sha256"):
             raise RuntimeError(f"managed NAM LV2 bundle changed locally; refusing overwrite: {destination}")
+        backup = validate_nam_backup_path(managed.get("backup"), destination, config)
+        backup_sha256 = managed.get("backup_sha256")
+        if backup is not None:
+            backup = migrate_namlv2_backup(
+                config, backup, destination, backup_sha256
+            )
+            managed["backup"] = str(backup)
+            registry["namlv2"] = managed
+            write_registry(registry)
         if managed.get("tag") == config["tag"] and (
             artifact_digest is None or managed.get("artifact_sha256") == artifact_digest
         ):
             print(f"NAM LV2 tag {config['tag']} is already installed and unchanged.")
             return
-        backup = validate_backup_path(managed.get("backup"), destination, is_directory=True)
-        backup_sha256 = managed.get("backup_sha256")
     elif destination.exists():
-        backup = destination.with_name(destination.name + ".rigforge-backup")
+        backup = namlv2_backup_path(config, destination)
         if backup.exists():
             raise RuntimeError(f"backup path already exists: {backup}")
         backup_sha256 = None
@@ -838,6 +900,7 @@ def deploy_namlv2(action, config, registry):
         try:
             shutil.copytree(extracted_bundle, staged_bundle)
             if managed is None and backup is not None:
+                backup.parent.mkdir(parents=True, exist_ok=True)
                 shutil.copytree(destination, backup)
                 backup_created = True
                 backup_sha256 = tree_sha256(backup)

+ 3 - 2
scripts/validate_desired_state.py

@@ -445,9 +445,10 @@ def validate_state(state):
                 errors.append(
                     "deployment.namlv2.artifact_directory must be artifacts/nam-lv2/arm64/current"
                 )
-            if namlv2.get("artifact_directory") != "artifacts/nam-lv2/arm64/current":
+            if namlv2.get("backup_directory") != ".local/state/rigforge/namlv2-backups":
                 errors.append(
-                    "deployment.namlv2.artifact_directory must be artifacts/nam-lv2/arm64/current"
+                    "deployment.namlv2.backup_directory must be "
+                    ".local/state/rigforge/namlv2-backups"
                 )
 
         sooperlooper = deployment.get("sooperlooper")

+ 37 - 0
tests/test_deploy.py

@@ -49,6 +49,7 @@ class DeploymentTests(unittest.TestCase):
                 f"{tag}/neural_amp_modeler_lv2_rpi5.tgz"
             ),
             "bundle_directory": "neural_amp_modeler.lv2",
+            "backup_directory": ".local/state/rigforge/namlv2-backups",
         }
 
     def plugin_config(self, component):
@@ -919,11 +920,47 @@ Attributes = A_ -rw-r--r--
         self.assertEqual(registry["namlv2"]["tag"], "v0.2.3")
         self.assertTrue(registry["namlv2"]["archive_sha256"])
         self.assertTrue(registry["namlv2"]["installed_sha256"])
+        backup = Path(registry["namlv2"]["backup"])
+        self.assertEqual(
+            backup,
+            self.home / ".local/state/rigforge/namlv2-backups/neural_amp_modeler.lv2.rigforge-backup",
+        )
+        self.assertTrue(backup.is_dir())
         deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
         self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
         self.assertNotIn("manifest.ttl", {path.name for path in destination.iterdir()})
+        self.assertFalse(backup.exists())
         self.assertNotIn("namlv2", deploy.read_registry())
 
+    def test_nam_install_migrates_legacy_adjacent_backup_out_of_lv2_search_path(self):
+        destination = self.home / ".lv2/neural_amp_modeler.lv2"
+        destination.mkdir(parents=True)
+        (destination / "old.txt").write_text("original plugin", encoding="utf-8")
+        self.install_nam()
+
+        config = self.nam_config()
+        registry = deploy.read_registry()
+        record = registry["namlv2"]
+        old_backup = destination.with_name(destination.name + ".rigforge-backup")
+        new_backup = (
+            self.home
+            / config["backup_directory"]
+            / (destination.name + ".rigforge-backup")
+        )
+        os.replace(new_backup, old_backup)
+        record["backup"] = str(old_backup)
+        registry["namlv2"] = record
+        deploy.write_registry(registry)
+
+        deploy.deploy_namlv2("install", config, deploy.read_registry())
+
+        self.assertFalse(old_backup.exists())
+        self.assertTrue(new_backup.is_dir())
+        self.assertEqual(
+            deploy.read_registry()["namlv2"]["backup"],
+            str(new_backup),
+        )
+
     def test_nam_install_uses_the_dynamic_runtime_build_artifact(self):
         config = deploy.load_deployment()["namlv2"]
         destination = self.home / ".lv2/neural_amp_modeler.lv2"

+ 6 - 0
tests/test_desired_state.py

@@ -151,6 +151,12 @@ class DesiredStateValidationTests(unittest.TestCase):
         state["deployment"]["namlv2"]["tag"] = "../latest"
         self.assertTrue(any("tag must be a simple version tag" in error for error in validate_state(state)))
 
+        state = copy.deepcopy(self.state)
+        state["deployment"]["namlv2"]["backup_directory"] = "../../outside"
+        self.assertTrue(
+            any("backup_directory must be" in error for error in validate_state(state))
+        )
+
         state = copy.deepcopy(self.state)
         state["deployment"]["lsp"]["tag"] = "../latest"
         self.assertTrue(any("deployment.lsp.tag must be a simple version tag" in error for error in validate_state(state)))

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.