diff options
| author | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 22:13:06 +0000 |
|---|---|---|
| committer | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 22:13:06 +0000 |
| commit | 760638c8fdfd8a2415b82982d2d487b5eeaac882 (patch) | |
| tree | 9395f15f9d5ea847a80283d25f61abe4e02f9203 | |
| parent | 870e0553d082abd655423647864c3d2b2fd4adee (diff) | |
ttpf_assets: a replaced DDS that was in use is removed by the next build
Review (@shdann): a file that could not be removed because it was open
stayed for good. The manifest had already switched away from it and the
pass was stamped, so later builds returned before any cleanup.
The manifest that switches the rows now lists the replaced files under
BCPackReplaced. They are removed after it is published, and the list is
written again with only what could not be removed (dropped when empty).
A build that finds the pass stamped still finishes that list. This also
covers a build stopped between the manifest write and the removal.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| -rw-r--r-- | tests/test_ttpf_assets.py | 23 | ||||
| -rw-r--r-- | tools/ttpf_assets.py | 52 |
2 files changed, 63 insertions, 12 deletions
diff --git a/tests/test_ttpf_assets.py b/tests/test_ttpf_assets.py index 83dd175f9..27a099d5a 100644 --- a/tests/test_ttpf_assets.py +++ b/tests/test_ttpf_assets.py | |||
| @@ -118,6 +118,27 @@ class TextureAssets(unittest.TestCase): | |||
| 118 | self.assertTrue(old.exists()) | 118 | self.assertTrue(old.exists()) |
| 119 | # Not hidden: a folder that stays double-sized has its reason on screen. | 119 | # Not hidden: a folder that stays double-sized has its reason on screen. |
| 120 | self.assertIn("pool: 1 replaced DDS file(s) could not be removed", stderr.getvalue()) | 120 | self.assertIn("pool: 1 replaced DDS file(s) could not be removed", stderr.getvalue()) |
| 121 | # And not forever: the manifest lists it, so the next build removes it once nothing holds it, although | ||
| 122 | # that build finds the pass stamped and writes nothing else. | ||
| 123 | self.assertEqual(json.loads(self.catalog.read_bytes())[ttpf_assets.PENDING], ["partial.dds"]) | ||
| 124 | with mock.patch.object(ttpf_assets.ttpf, "Pool", side_effect=AssertionError("unexpected reopen")): | ||
| 125 | self.build() | ||
| 126 | self.assertFalse(old.exists()) | ||
| 127 | self.assertNotIn(ttpf_assets.PENDING, json.loads(self.catalog.read_bytes())) | ||
| 128 | self.build() # nothing left to do: no further write, no error | ||
| 129 | self.assertTrue(self.row_after_export()["File"].startswith("000000_bcpack_")) | ||
| 130 | |||
| 131 | def test_a_run_stopped_after_the_switch_is_finished_by_the_next(self): | ||
| 132 | # The manifest that switches the rows already lists the file to remove, so a build stopped between that | ||
| 133 | # write and the removal leaves nothing behind for good. | ||
| 134 | old = self.upgrade() | ||
| 135 | with mock.patch.object(ttpf_assets, "finish_replaced"): | ||
| 136 | self.build() | ||
| 137 | self.assertTrue(old.exists()) | ||
| 138 | self.assertEqual(json.loads(self.catalog.read_bytes())[ttpf_assets.PENDING], ["partial.dds"]) | ||
| 139 | self.build() | ||
| 140 | self.assertFalse(old.exists()) | ||
| 141 | self.assertNotIn(ttpf_assets.PENDING, json.loads(self.catalog.read_bytes())) | ||
| 121 | 142 | ||
| 122 | def complete_with_row_file(self, file): | 143 | def complete_with_row_file(self, file): |
| 123 | """Run the pass on a manifest whose row names `file`; returns the folder's file names afterwards.""" | 144 | """Run the pass on a manifest whose row names `file`; returns the folder's file names afterwards.""" |
| @@ -160,7 +181,7 @@ class TextureAssets(unittest.TestCase): | |||
| 160 | 181 | ||
| 161 | with mock.patch.object(ttpf_assets.os, "stat", counted), \ | 182 | with mock.patch.object(ttpf_assets.os, "stat", counted), \ |
| 162 | mock.patch.object(ttpf_assets.os.path, "samefile", side_effect=AssertionError("per-pair check")): | 183 | mock.patch.object(ttpf_assets.os.path, "samefile", side_effect=AssertionError("per-pair check")): |
| 163 | self.assertEqual(ttpf_assets.remove_replaced(self.folder, replaced, rows), 0) | 184 | self.assertEqual(ttpf_assets.remove_replaced(self.folder, replaced, rows), []) |
| 164 | self.assertLessEqual(len(calls), 100, len(calls)) | 185 | self.assertLessEqual(len(calls), 100, len(calls)) |
| 165 | # The replaced files are gone and the rows' files stay. | 186 | # The replaced files are gone and the rows' files stay. |
| 166 | self.assertEqual(sorted(p.name for p in self.folder.iterdir()), sorted(row["File"] for row in rows)) | 187 | self.assertEqual(sorted(p.name for p in self.folder.iterdir()), sorted(row["File"] for row in rows)) |
diff --git a/tools/ttpf_assets.py b/tools/ttpf_assets.py index 230473bff..f34c9b62a 100644 --- a/tools/ttpf_assets.py +++ b/tools/ttpf_assets.py | |||
| @@ -58,7 +58,7 @@ def file_identity(path): | |||
| 58 | 58 | ||
| 59 | def remove_replaced(directory, replaced, rows): | 59 | def remove_replaced(directory, replaced, rows): |
| 60 | """Remove the files of `replaced` (names the rows had before they were switched) from `directory`; returns | 60 | """Remove the files of `replaced` (names the rows had before they were switched) from `directory`; returns |
| 61 | how many could not be removed. | 61 | the names that could not be removed (in use), sorted. |
| 62 | 62 | ||
| 63 | Only a .dds file by its bare name in this folder is removed, never manifest.json, and never a file a row still | 63 | Only a .dds file by its bare name in this folder is removed, never manifest.json, and never a file a row still |
| 64 | names: by the same name, by a name that differs in letter case only, or by any other spelling of the same file | 64 | names: by the same name, by a name that differs in letter case only, or by any other spelling of the same file |
| @@ -67,7 +67,7 @@ def remove_replaced(directory, replaced, rows): | |||
| 67 | names = [row["File"] for row in rows if isinstance(row.get("File"), str)] | 67 | names = [row["File"] for row in rows if isinstance(row.get("File"), str)] |
| 68 | named = {os.path.normcase(name) for name in names} | 68 | named = {os.path.normcase(name) for name in names} |
| 69 | on_disk = {file_identity(directory / name) for name in names} - {None} | 69 | on_disk = {file_identity(directory / name) for name in names} - {None} |
| 70 | left = 0 | 70 | left = [] |
| 71 | for name in sorted(replaced): | 71 | for name in sorted(replaced): |
| 72 | if Path(name).name != name or not name.lower().endswith(".dds") or os.path.normcase(name) in named: | 72 | if Path(name).name != name or not name.lower().endswith(".dds") or os.path.normcase(name) in named: |
| 73 | continue | 73 | continue |
| @@ -76,10 +76,38 @@ def remove_replaced(directory, replaced, rows): | |||
| 76 | try: | 76 | try: |
| 77 | (directory / name).unlink(missing_ok=True) | 77 | (directory / name).unlink(missing_ok=True) |
| 78 | except OSError: | 78 | except OSError: |
| 79 | left += 1 | 79 | left.append(name) |
| 80 | return left | 80 | return left |
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | # The manifest key that lists replaced files still to be removed. It is written with the manifest that switches the | ||
| 84 | # rows, so a removal that fails (a file in use) or a run stopped right after the switch is finished by a later run, | ||
| 85 | # even though that run finds the pass stamped and writes nothing else. | ||
| 86 | PENDING = "BCPackReplaced" | ||
| 87 | |||
| 88 | |||
| 89 | def finish_replaced(directory, manifest): | ||
| 90 | """Remove the files the manifest lists under PENDING, and publish it again with what is left.""" | ||
| 91 | pending = manifest.get(PENDING) | ||
| 92 | if not isinstance(pending, list) or not pending: | ||
| 93 | return | ||
| 94 | left = remove_replaced(directory, {name for name in pending if isinstance(name, str)}, manifest["Textures"]) | ||
| 95 | if left: | ||
| 96 | manifest[PENDING] = left | ||
| 97 | else: | ||
| 98 | manifest.pop(PENDING) | ||
| 99 | if left != pending: | ||
| 100 | try: | ||
| 101 | publish(directory / "manifest.json", json.dumps(manifest, indent=2).encode("utf-8")) | ||
| 102 | except OSError as exc: | ||
| 103 | # The files are gone; the list on disk still names them, and a later run drops them (missing is fine). | ||
| 104 | print(f"assets: {directory.name}: could not update manifest.json after removing replaced DDS files: " | ||
| 105 | f"{exc.strerror or exc}", file=sys.stderr) | ||
| 106 | if left: | ||
| 107 | print(f"assets: {directory.name}: {len(left)} replaced DDS file(s) could not be removed (in use?) and stay " | ||
| 108 | "next to the full-chain files; the next build tries again", file=sys.stderr) | ||
| 109 | |||
| 110 | |||
| 83 | def candidate(row, profile): | 111 | def candidate(row, profile): |
| 84 | if row.get("DxgiFormat") not in ttpf.BC_FORMATS: | 112 | if row.get("DxgiFormat") not in ttpf.BC_FORMATS: |
| 85 | return False | 113 | return False |
| @@ -92,12 +120,13 @@ def candidate(row, profile): | |||
| 92 | def complete_pool(source, directory, manifest, settings): | 120 | def complete_pool(source, directory, manifest, settings): |
| 93 | """Publish full chains next to ordinary DDS files, then switch the catalog to them.""" | 121 | """Publish full chains next to ordinary DDS files, then switch the catalog to them.""" |
| 94 | stamp, profile = settings | 122 | stamp, profile = settings |
| 123 | directory = Path(directory) | ||
| 95 | if manifest.get("BCPackPass") == stamp: | 124 | if manifest.get("BCPackPass") == stamp: |
| 125 | finish_replaced(directory, manifest) | ||
| 96 | return 0, 0 | 126 | return 0, 0 |
| 97 | rows = [row for row in manifest["Textures"] if candidate(row, profile)] | 127 | rows = [row for row in manifest["Textures"] if candidate(row, profile)] |
| 98 | if not rows: | 128 | if not rows: |
| 99 | return 0, 0 | 129 | return 0, 0 |
| 100 | directory = Path(directory) | ||
| 101 | written = blocked = 0 | 130 | written = blocked = 0 |
| 102 | superseded = set() # the files the rows named before they were switched to a full chain | 131 | superseded = set() # the files the rows named before they were switched to a full chain |
| 103 | with closing(ttpf.Pool(source)) as pool: | 132 | with closing(ttpf.Pool(source)) as pool: |
| @@ -142,12 +171,13 @@ def complete_pool(source, directory, manifest, settings): | |||
| 142 | if decoder: | 171 | if decoder: |
| 143 | decoder.read_chunk.cache_clear() | 172 | decoder.read_chunk.cache_clear() |
| 144 | manifest["BCPackPass"] = stamp | 173 | manifest["BCPackPass"] = stamp |
| 174 | # The files the switched rows named before would otherwise stay in the folder, named by no row, so every | ||
| 175 | # completed texture would be stored twice. They are listed in the manifest that switches the rows and removed | ||
| 176 | # only after it is published: a file another row still names is kept, and one that can't be removed (in use) | ||
| 177 | # stays listed for the next build. The export is complete either way. | ||
| 178 | pending = {name for name in manifest.get(PENDING) or [] if isinstance(name, str)} | superseded | ||
| 179 | if pending: | ||
| 180 | manifest[PENDING] = sorted(pending) | ||
| 145 | publish(directory / "manifest.json", json.dumps(manifest, indent=2).encode("utf-8")) | 181 | publish(directory / "manifest.json", json.dumps(manifest, indent=2).encode("utf-8")) |
| 146 | # Only now that the manifest names the full chains: the files they replace would otherwise stay in the folder, | 182 | finish_replaced(directory, manifest) |
| 147 | # named by no row, so every completed texture would be stored twice. A file another row still names is kept, | ||
| 148 | # and so is one that can't be removed (in use): the export is complete either way. | ||
| 149 | left = remove_replaced(directory, superseded, manifest["Textures"]) if superseded else 0 | ||
| 150 | if left: | ||
| 151 | print(f"assets: {directory.name}: {left} replaced DDS file(s) could not be removed (in use?) and stay next " | ||
| 152 | "to the full-chain files", file=sys.stderr) | ||
| 153 | return written, blocked | 183 | return written, blocked |