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