Skip to content

Instantly share code, notes, and snippets.

@dgk
Created November 14, 2012 08:53
Show Gist options
  • Save dgk/4071059 to your computer and use it in GitHub Desktop.
Save dgk/4071059 to your computer and use it in GitHub Desktop.
Backups weeding script
#!/usr/bin/python
# -*- coding: utf8 -*-
import os
import re
import datetime
from calendar import Calendar
from itertools import chain
KEEP_DAY = 0
def make_test_files(num=200):
now = datetime.datetime.now()
for i in range(num):
date = datetime.datetime.now() - datetime.timedelta(days=i)
date = date.strftime("%Y-%m-%d")
open('file.%s.ext' % date, 'w').close()
def cleanup(directory='.'):
today = datetime.date.today()
week_ago = today - datetime.timedelta(days=7)
month_ago = today - datetime.timedelta(days=31)
calendar = Calendar()
re_file = r'^(.+)[-.]([0-9]{4})-([0-9]{2})-([0-9]{2})\.(.+)$'
_files = os.listdir(directory)
file_dates = {}
oldest_files = {}
files = []
for file_name in _files:
match = re.search(re_file, file_name)
if not match:
continue
year, month, day = ( int(x) for x
in ( match.group(x) for x in (2, 3, 4)))
date = datetime.date(year, month, day)
group = tuple(match.group(x) for x in (1, 5))
file_dates.setdefault(group, []).append(date)
files.append((file_name, date, group))
for group, dates in file_dates.items():
oldest_files[group] = min(dates)
for file_name, date, group in files:
if date == oldest_files[group]:
# skip oldest file
continue
if date > week_ago:
# skip this week files
continue
if date > month_ago:
# process this month files
if date.weekday() != KEEP_DAY :
os.unlink(file_name)
continue
keep_day = min([ x for x
in list(chain(*calendar.monthdatescalendar(date.year, date.month)))
if x.month==date.month and x.weekday() == KEEP_DAY])
if date != keep_day:
os.unlink(file_name)
#make_test_files()
cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment