Skip to content
Snippets Groups Projects
Commit 9281da9a authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Add -d/--dry-run options

parent e6eb87f5
Branches
No related tags found
No related merge requests found
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import re, os
import re, os, sys
import datetime as dt
import sqlite3
import logging
......@@ -61,11 +61,36 @@ def delete_older(conn, config, logger, dry_run=True):
def data_retention():
# Start logging
logger = logging.getLogger(__name__)
logger.info('Starting data_retention() in {}'.format(APPLICATION_NAME))
# Add dry run options
dry = ["-d", "--dry-run"]
# Initially false (no dry-run)
dry_run = False
# At the time only one argument is supported, display error otherwise
if len(sys.argv) == 2:
# If -d or --dry-run is present, activate the flag, display in log
if sys.argv[1] in dry:
dry_run = True
logger.warning("Running in Dry-Run-Mode (listing to-be-deleted entries, but not actually deleting anything)")
else:
if not sys.argv[1] in ["-h", "--help"]:
logger.error("Argument \"{}\" is not supported".format(sys.argv[1]))
else:
logger.info("stechuhr-server data retention script.")
logger.info("Possible options are: {}".format(dry))
exit()
elif len(sys.argv) > 2:
logger.error("Arguments {} ar not supported".format(", ".join(sys.argv[1:])))
logger.info("Possible options are: {}".format(dry))
exit()
# Initialize the configuration (create a default one if needed)
config = initialize_config(logger)
logger.info('Will delete entries older than {} days (as set in config)'.format(config["retention"]["duration"]))
......@@ -74,7 +99,7 @@ def data_retention():
conn = initialize_db(config, logger)
# Delete data older than the time specified in the config
delete_older(conn, config, logger, dry_run=False)
delete_older(conn, config, logger, dry_run=dry_run)
logger.info("Exiting.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment