Skip to content

Commit

Permalink
fix: Missing return value when smartremove
Browse files Browse the repository at this point in the history
The method computing a list of snapshots that are ready to get removed now returns an empty list if it can't find a snapshot to remove. Minor PEP8 formatting.

Fix Debian-Bug 973760
Fix #1392
  • Loading branch information
buhtz authored Jun 30, 2023
1 parent 7cf5c81 commit c853223
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version 1.3.4-dev (development of upcoming release)
* Fix bug: Add support for ChainerBackend class as keyring which iterates over all supported keyring backends (#1410)
* Fix bug: Unit test fails on some machines due to warning "Ignoring XDG_SESSION_TYPE=wayland on Gnome..." (#1429)
* Fix bug: Generation of config-manpage caused an error with Debian's Lintian (#1398).
* Fix bug: Return empty list in smartRemove (#1392, Debian Bug Report 973760)
* Breaking change: Minimal Python version 3.8 required (#1358).
* Feature: Exclude /swapfile by default (#1053)
* Documentation: Removed outdated docbook (#1345).
Expand Down
73 changes: 42 additions & 31 deletions common/snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,76 +1382,86 @@ def smartRemoveList(self,
list: snapshots that should be removed
"""
snapshots = listSnapshots(self.config)
logger.debug("Considered: %s" %snapshots, self)
logger.debug(f'Considered: {snapshots}', self)

if len(snapshots) <= 1:
logger.debug("There is only one snapshot, so keep it", self)
return
logger.debug('There is only one snapshot, so keep it', self)
return []

if now_full is None:
now_full = datetime.datetime.today()

now = now_full.date()

#keep the last snapshot
# keep the last snapshot
keep = set([snapshots[0]])

#keep all for the last keep_all days
# keep all for the last keep_all days
if keep_all > 0:
keep |= self.smartRemoveKeepAll(snapshots,
now - datetime.timedelta(days=keep_all-1),
now + datetime.timedelta(days=1))
keep |= self.smartRemoveKeepAll(
snapshots,
now - datetime.timedelta(days=keep_all-1),
now + datetime.timedelta(days=1))

#keep one per day for the last keep_one_per_day days
# keep one per day for the last keep_one_per_day days
if keep_one_per_day > 0:
d = now
for i in range(0, keep_one_per_day):
keep |= self.smartRemoveKeepFirst(snapshots,
d,
d + datetime.timedelta(days=1),
keep_healthy = True)
keep |= self.smartRemoveKeepFirst(
snapshots,
d,
d + datetime.timedelta(days=1),
keep_healthy=True)
d -= datetime.timedelta(days=1)

#keep one per week for the last keep_one_per_week weeks
# keep one per week for the last keep_one_per_week weeks
if keep_one_per_week > 0:
d = now - datetime.timedelta(days = now.weekday() + 1)
d = now - datetime.timedelta(days=now.weekday() + 1)

for i in range(0, keep_one_per_week):
keep |= self.smartRemoveKeepFirst(snapshots,
d,
d + datetime.timedelta(days=8),
keep_healthy = True)
keep |= self.smartRemoveKeepFirst(
snapshots,
d,
d + datetime.timedelta(days=8),
keep_healthy=True)
d -= datetime.timedelta(days=7)

#keep one per month for the last keep_one_per_month months
# keep one per month for the last keep_one_per_month months
if keep_one_per_month > 0:
d1 = datetime.date(now.year, now.month, 1)
d2 = self.incMonth(d1)

for i in range(0, keep_one_per_month):
keep |= self.smartRemoveKeepFirst(snapshots, d1, d2,
keep_healthy = True)
keep |= self.smartRemoveKeepFirst(
snapshots, d1, d2, keep_healthy=True)
d2 = d1
d1 = self.decMonth(d1)

#keep one per year for all years
first_year = int(snapshots[-1].sid[ : 4])
# keep one per year for all years
first_year = int(snapshots[-1].sid[:4])

for i in range(first_year, now.year+1):
keep |= self.smartRemoveKeepFirst(snapshots,
datetime.date(i,1,1),
datetime.date(i+1,1,1),
keep_healthy = True)
datetime.date(i, 1, 1),
datetime.date(i+1, 1, 1),
keep_healthy=True)

logger.debug("Keep snapshots: %s" %keep, self)
logger.debug(f'Keep snapshots: {keep}', self)

del_snapshots = []

for sid in snapshots:
if sid in keep:
continue

if self.config.dontRemoveNamedSnapshots():
if sid.name:
logger.debug("Keep snapshot: %s, it has a name" %sid, self)
logger.debug(
f'Keep snapshot: {sid}, because it has a name', self)
continue

del_snapshots.append(sid)

return del_snapshots

def smartRemove(self, del_snapshots, log = None):
Expand Down Expand Up @@ -1594,8 +1604,9 @@ def freeSpace(self, now):
self.remove(snapshots[0])
del snapshots[0]

#smart remove
# smart remove
enabled, keep_all, keep_one_per_day, keep_one_per_week, keep_one_per_month = self.config.smartRemove()

if enabled:
self.setTakeSnapshotMessage(0, _('Smart remove'))
del_snapshots = self.smartRemoveList(now,
Expand All @@ -1605,7 +1616,7 @@ def freeSpace(self, now):
keep_one_per_month)
self.smartRemove(del_snapshots)

#try to keep min free space
# try to keep min free space
if self.config.minFreeSpaceEnabled():
self.setTakeSnapshotMessage(0, _('Trying to keep min free space'))

Expand Down

0 comments on commit c853223

Please sign in to comment.