Skip to content

Commit

Permalink
Prune old backups
Browse files Browse the repository at this point in the history
Fixes #145

This commit adds a function to prune (14 days) old backups.
  • Loading branch information
deathaxe committed Mar 19, 2023
1 parent c822773 commit 442ca19
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Package Control.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
// The URL to post install, upgrade and removal notices to
"submit_url": "https://packagecontrol.io/submit",

// Maximum days to keep backups. All older ones are removed when ST starts
"max_backup_age": 14,

// If missing packages should be automatically installed when ST starts
"install_missing": true,

Expand Down
3 changes: 3 additions & 0 deletions package_control/package_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def run(self):
'version': __version__
})

# To limit diskspace occupied remove all old enough backups
self.manager.prune_backup_dir()

# Scan through packages and complete pending operations
found_packages = self.cleanup_pending_packages()

Expand Down
25 changes: 25 additions & 0 deletions package_control/package_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(self):
'ignore_vcs_packages',
'install_missing',
'install_prereleases',
'max_backup_age',
'package_destination',
'package_name_map',
'package_profiles',
Expand Down Expand Up @@ -2052,6 +2053,30 @@ def backup_package_dir(self, package_name):
)
return False

def prune_backup_dir(self):
"""
Remove all backups older than ``max_backup_age`` days.
"""

age = max(0, self.settings.get('max_backup_age', 14))
today = datetime.date.today()
backup_dir = os.path.join(sys_path.data_path(), 'Backup')

if not os.path.isdir(backup_dir):
return

for fname in os.listdir(backup_dir):
package_backup_dir = os.path.join(backup_dir, fname)
if not os.path.isdir(package_backup_dir):
continue

try:
date = datetime.date(int(fname[:4]), int(fname[4:6]), int(fname[6:8]))
if (today - date).days > age:
delete_directory(package_backup_dir)
except ValueError:
continue

def print_messages(self, package_name, package_dir, is_upgrade, old_version, new_version):
"""
Prints out package install and upgrade messages
Expand Down

0 comments on commit 442ca19

Please sign in to comment.