Skip to content

Commit

Permalink
🐛 Do not remove the target root and non-empty directories.
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
rafalkrupinski committed Jun 23, 2024
1 parent 5317863 commit f6266ef
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/rybak/tree_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ def render(
session = Session(
self,
target_root,
set(),
event_sink,
)
ctx = RenderContext(
Expand Down Expand Up @@ -191,26 +190,27 @@ def template_root(self) -> PurePath:
class Session:
template: 'TreeTemplate'
target_root: Path
files_written: MutableSet[Path]
event_sink: EventSink
_files_written: MutableSet[Path] = dataclasses.field(default_factory=set)

def remove_stale(self) -> None:
for path, _, files in os.walk(self.target_root, False):
path_ = Path(path)
existing_dir_path = path_.relative_to(self.target_root)
for path_, dirs, files in os.walk(self.target_root, False):
path = Path(path_)
existing_dir_path = path.relative_to(self.target_root)
removed_files: MutableSet[str] = set()

for file_name in files:
file_path = existing_dir_path / file_name

if file_path not in self.files_written:
if file_path not in self._files_written:
self.event_sink.unlinking_file(file_path)
removed_files.add(file_name)
(self.target_root / file_path).unlink()

if not files or set(files) == removed_files:
path_.rmdir()
if path != self.target_root and not dirs and set(files) == removed_files:
self.event_sink.unlinking_file(path)
path.rmdir()

def writing_file(self, template: PurePath, target: Path) -> None:
self.files_written.add(target)
self._files_written.add(target)
self.event_sink.writing_file(template, target)

0 comments on commit f6266ef

Please sign in to comment.