66import uuid
77from contextlib import contextmanager
88from pathlib import Path
9+ from typing import TYPE_CHECKING
910from typing import Generator
11+ from typing import Iterator
12+
13+ if TYPE_CHECKING :
14+ from inline_snapshot ._change import ChangeBase
1015
1116from ._external_location import ExternalLocation
1217
@@ -35,6 +40,9 @@ def store(self, location: ExternalLocation, file_path: Path):
3540 """
3641 raise NotImplementedError
3742
43+ def delete (self , location : ExternalLocation ):
44+ raise NotImplementedError
45+
3846 def new_location (
3947 self , location : ExternalLocation , file_path : Path
4048 ) -> ExternalLocation :
@@ -46,7 +54,9 @@ def new_location(
4654 def cleanup (self ):
4755 raise NotImplementedError
4856
49- def sync_used_externals (self , used_externals : list [ExternalLocation ]) -> int :
57+ def sync_used_externals (
58+ self , used_externals : list [ExternalLocation ]
59+ ) -> Iterator [ChangeBase ]:
5060 raise NotImplementedError
5161
5262
@@ -70,6 +80,10 @@ def store(self, location: ExternalLocation, file_path: Path):
7080
7181 shutil .copy (str (file_path ), str (snapshot_path ))
7282
83+ def delete (self , location : ExternalLocation ):
84+ snapshot_path = self ._get_path (location )
85+ snapshot_path .unlink ()
86+
7387 def new_location (
7488 self , location : ExternalLocation , file_path : Path
7589 ) -> ExternalLocation :
@@ -101,8 +115,10 @@ def _get_path(self, location: ExternalLocation) -> Path:
101115 def cleanup (self ):
102116 pass
103117
104- def sync_used_externals (self , used_externals : list [ExternalLocation ]) -> int :
105- return 0
118+ def sync_used_externals (
119+ self , used_externals : list [ExternalLocation ]
120+ ) -> Iterator [ChangeBase ]:
121+ return iter (())
106122
107123
108124class HashStorage (StorageProtocol ):
@@ -129,13 +145,18 @@ def store(self, location: ExternalLocation, file_path: Path):
129145 with file_path .open ("rb" ) as f :
130146 hash_name = file_digest (f , "sha256" ).hexdigest ()
131147
148+ assert location .suffix
149+
132150 if not (self .directory / (hash_name + location .suffix )).exists ():
133- # TODO: remove -new
134151 shutil .copy (
135152 str (file_path ),
136- str (self .directory / (hash_name + "-new" + location .suffix )),
153+ str (self .directory / (hash_name + location .suffix )),
137154 )
138155
156+ def delete (self , location : ExternalLocation ):
157+ path = self ._lookup_path (location .path )
158+ path .unlink ()
159+
139160 def new_location (
140161 self , location : ExternalLocation , file_path : Path
141162 ) -> ExternalLocation :
@@ -154,24 +175,20 @@ def prune_new_files(self):
154175 for file in self .directory .glob ("*-new.*" ):
155176 file .unlink ()
156177
157- def sync_used_externals (self , used_externals : list [ExternalLocation ]) -> int :
178+ def sync_used_externals (
179+ self , used_externals : list [ExternalLocation ]
180+ ) -> Iterator [ChangeBase ]:
158181 unused_externals = self .list ()
159182 for location in used_externals :
160183 used = self .lookup_all (location .path )
161- for u in used :
162- self .persist (u )
163184 unused_externals -= used
164185
165- n = 0
166-
186+ from inline_snapshot ._change import ExternalRemove
167187 from inline_snapshot ._global_state import state
168188
169189 if state ().update_flags .trim :
170190 for name in unused_externals :
171- self .remove (name )
172- n += 1
173-
174- return n
191+ yield ExternalRemove ("trim" , ExternalLocation .from_name (name ))
175192
176193 def cleanup (self ):
177194 self .prune_new_files ()
@@ -183,15 +200,6 @@ def list(self) -> set[str]:
183200 else :
184201 return set ()
185202
186- def persist (self , name ):
187- try :
188- file = self ._lookup_path (name )
189- except StorageLookupError :
190- return
191- if file .stem .endswith ("-new" ):
192- stem = file .stem [:- 4 ]
193- file .rename (file .with_name (stem + file .suffix ))
194-
195203 def _lookup_path (self , name ) -> Path :
196204 if "*" not in name :
197205 p = Path (name )
@@ -211,6 +219,9 @@ def _lookup_path(self, name) -> Path:
211219 def lookup_all (self , name : str ) -> set [str ]:
212220 return {file .name for file in self .directory .glob (name )}
213221
222+ def lookup_all_path (self , name : str ) -> set [path ]:
223+ return {file for file in self .directory .glob (name )}
224+
214225 def remove (self , name ):
215226 self ._lookup_path (name ).unlink ()
216227
0 commit comments