This commit is contained in:
diocloid 2021-08-18 20:28:12 +02:00
parent 2443340eb8
commit 6192f24cef

28
main.py
View File

@ -1,5 +1,5 @@
import argparse
from datetime import date, datetime, timedelta
from datetime import date, datetime
from dateutil.relativedelta import relativedelta
import os
from pathlib import Path
@ -24,8 +24,7 @@ def get_month_minus_x(p_date: date, limitmonths: int):
list = []
limitmonths = limitmonths - 1
while (limitmonths >= 0):
list.append((datetime(p_date.year, p_date.month, 1) +
relativedelta(months=-limitmonths) + timedelta(days=-1)).date())
list.append((datetime(p_date.year, p_date.month, 1) + relativedelta(months=-limitmonths) + relativedelta(days=-1)).date())
limitmonths = limitmonths - 1
return list
@ -35,8 +34,7 @@ def get_quarter_minus_x(p_date: date, limitquarter: int):
list = []
while (limitquarter > 0):
last_quarter = get_quarter(p_date) - limitquarter
list.append((datetime(p_date.year + 3 * last_quarter // 12,
3 * last_quarter % 12 + 1, 1) + timedelta(days=-1)).date())
list.append((datetime(p_date.year + 3 * last_quarter // 12, 3 * last_quarter % 12 + 1, 1) + relativedelta(days=-1)).date())
limitquarter = limitquarter - 1
return list
@ -46,8 +44,7 @@ def get_year_minus_x(p_date: date, limityear: int):
list = []
limityear = limityear - 1
while (limityear >= 0):
list.append((datetime(p_date.year, 1, 1) + relativedelta(years=-limityear) +
timedelta(days=-1)).date())
list.append((datetime(p_date.year, 1, 1) + relativedelta(years=-limityear) + relativedelta(days=-1)).date())
limityear = limityear - 1
return list
@ -66,18 +63,14 @@ def process_file(child, daydates, monthdates, quarterdates, yeardates):
print(f'{{"name": "{child.name}", "filedate": "", "keep": "True - no YYYYMMDD to parse", "action": "none"}}')
else:
filedate = datetime.strptime(matches_list[0], '%Y%m%d')
keep = test_for_keep(filedate, daydates, monthdates,
quarterdates, yeardates)
keep = test_for_keep(filedate, daydates, monthdates, quarterdates, yeardates)
if (keep):
print(
f'{{"name": "{child.name}", "filedate": "{filedate}", "keep": "{keep}", "action": "none"}}')
print(f'{{"name": "{child.name}", "filedate": "{filedate}", "keep": "{keep}", "action": "none"}}')
else:
print(
f'{{"name": "{child.name}", "filedate": "{filedate}", "keep": "{keep}" , "action": "to remove"}}')
os.remove(child)
print(
f'{{"name": "{child.name}", "filedate": "{filedate}", "keep": "{keep}" , "action": "removed"}}')
print(f'{{"name": "{child.name}", "filedate": "{filedate}", "keep": "{keep}" , "action": "to remove"}}')
#os.remove(child)
#print(f'{{"name": "{child.name}", "filedate": "{filedate}", "keep": "{keep}" , "action": "removed"}}')
def test_for_keep(filedate, daydates, monthdates, quarterdates, yeardates):
@ -108,8 +101,7 @@ def test_for_keep(filedate, daydates, monthdates, quarterdates, yeardates):
def main(path, days, months, quarters, years):
print(f'Path is {path}')
print(
f'Keeping {years} end of year, {quarters} end of quarter, {months} end of month, {days} daily backups')
print(f'Keeping {years} end of year, {quarters} end of quarter, {months} end of month, {days} daily backups')
daydates = get_day_minus_x(date.today(), days)
monthdates = get_month_minus_x(date.today(), months)