Skip to content

Commit

Permalink
batou_ext.file.SymlinkAndCleanup: add option "etag_suffix"
Browse files Browse the repository at this point in the history
Given the following deployment:

    self += s3.Download(..., target=f"{version}.tar.gz")
    archive = self._.target
    self += SymlinkAndCleanup(target, pattern="*.tar.gz")

With this, the tarballs that are neither linked by `last` or `current`
are removed. However, the `.etag` files from `s3.Download` are not.

Doing `pattern="*.tar.gz*"` is not an option here since this would
remove ALL etag files. This means that the next deployment would
download the tarball from S3 again since its etag file is missing.

This patch adds an attribute called `etag_suffix` that tries to delete
all files matching the glob pattern `pattern + etag_suffix` unless
`pattern` is equal to the file linked by current or last.

FC-41030
  • Loading branch information
Ma27 committed Jan 22, 2025
1 parent 0fef11f commit 46a5af8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGES.d/20250122_125537_mb_FC_41030_etag_cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- `batou_ext.file.SymlinkAndCleanup`: add option `etag_suffix`.
This contains a suffix that each symlinked file may have.

For instance, when doing `SymlinkAndCleanup` on a file downloaded with
`batou_ext.s3.Download`, the pattern `*.tar.gz` doesn't clean up the
`.etag` files. However, `*.tar.gz*` (or an equivalent) would also remove
the etag files of the files that are symlinked to `current` & `last`.
10 changes: 10 additions & 0 deletions src/batou_ext/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SymlinkAndCleanup(batou.component.Component):

namevar = "current"
pattern = None
etag_suffix = batou.component.Attribute(str, ".etag")
_current_link = None
_last_link = None

Expand Down Expand Up @@ -92,17 +93,26 @@ def remove(links, el):

def _list_removals(self):
candidates = glob.glob(self.pattern)
candidates.extend(glob.glob(self.pattern + self.etag_suffix))

current = self._link(self._current_link)
last = self._link(self._last_link)
if current == self.current:
# keep last+current
self.remove(candidates, current)
self.remove(candidates, last)
if current is not None:
self.remove(candidates, current + self.etag_suffix)
if last is not None:
self.remove(candidates, last + self.etag_suffix)
else:
# keep current + new current"
self.remove(candidates, current)
self.remove(candidates, self.current)
if current is not None:
self.remove(candidates, current + self.etag_suffix)
if self.current is not None:
self.remove(candidates, self.current + self.etag_suffix)

return candidates

Expand Down

0 comments on commit 46a5af8

Please sign in to comment.