Commit 3f1dbec3 authored by Sriram Karra's avatar Sriram Karra

Clean up old log files based on a parameter in config.json

Read and act on a 'log_hold_period' config variable.
parent db5ab265
##
## Created : Tue Apr 10 15:55:20 IST 2012
## Last Modified : Thu May 31 08:13:52 IST 2012
## Last Modified : Thu Jun 14 19:41:10 IST 2012
##
## Copyright (C) 2012 Sriram Karra <karra.etc@gmail.com>
##
......@@ -43,6 +43,7 @@ from folder import Folder
from pimdb_gc import GCPIMDB
from pimdb_bb import BBPIMDB
from folder_bb import BBContactsFolder
import utils
## Some Global Variables to get started
asynk_ver = 'v0.2.1+'
......@@ -115,7 +116,13 @@ def setup_logging (config):
fileLogger.setLevel(logging.DEBUG)
fileLogger.setFormatter(formatter)
logger.addHandler(fileLogger)
## Delete any old log files as applicable
period = config.get_log_hold_period()
logging.info('Deleting log files older than %d days, if any...', period)
utils.del_files_older_than(logdir, period)
logging.info('Deleting log files older than %d days, if any...done',
period)
def setup_parser ():
p = argparse.ArgumentParser(description='ASynK: PIM Awesome Sync by Karra')
p.add_argument('--dry-run', action='store_true',
......@@ -196,7 +203,7 @@ def setup_parser ():
'a log file for tracking purposes')
p.add_argument('--version', action='version',
version='%(prog)s v' + ('%s' % asynk_ver))
version='%(prog)s ' + ('%s' % asynk_ver))
return p
......
##
## Created : Tue Jul 19 13:54:53 IST 2011
## Last Modified : Thu May 31 22:50:54 IST 2012
## Last Modified : Thu Jun 14 19:26:12 IST 2012
##
## Copyright (C) 2011, 2012 Sriram Karra <karra.etc@gmail.com>
##
......@@ -38,6 +38,8 @@ class AsynkConfigError(Exception):
pass
class Config:
confi_curr_ver = 3
def __init__ (self, confn, staten, sync_through=True):
"""If sync_through is True, any change to the configuration is
immediately written back to the original disk file, otherwise
......@@ -76,6 +78,13 @@ class Config:
confi.close()
statei.close()
if self.get_conf_file_version() < self.confi_curr_ver:
logging.warn('config.json file version is out of date. You may '
'like to upgrade by pulling the latest version of '
'your config.json so you get additional variables '
'to configure. Note that this is optional and your '
'ASynK will continue to work as before.')
self.set_app_root(os.path.abspath(''))
self.sync_through = sync_through
......@@ -180,9 +189,15 @@ class Config:
def get_backup_dir (self):
return self._get_prop('config', 'backup_dir')
def get_backup_hold_period (self):
return self._get_prop('config', 'backup_hold_period')
def get_log_dir (self):
return self._get_prop('config', 'log_dir')
def get_log_hold_period (self):
return self._get_prop('config', 'log_hold_period')
def get_profile_defaults (self):
return self._get_prop('config', 'profile_defaults')
......
##
## Created : Tue Jul 26 06:54:41 IST 2011
## Last Modified : Fri Jun 01 17:40:15 IST 2012
## Last Modified : Thu Jun 14 19:40:06 IST 2012
##
## Copyright (C) 2011, 2012 by Sriram Karra <karra.etc@gmail.com>
##
......@@ -19,7 +19,7 @@
## not, see <http://www.gnu.org/licenses/>.
##
import os, re
import logging, os, re
def abs_pathname (config, fname):
"""If fname is an absolute path then it is returned as is. If it appears
......@@ -80,6 +80,19 @@ def get_event_rel (events, rel):
from datetime import tzinfo, timedelta, datetime
import time as _time
def del_files_older_than (abs_dir, days):
"""Delete all files in abs_dir/ that were modified more than 'days' days
or earlier."""
now = _time.time()
for f in os.listdir(abs_dir):
fi = os.path.join(abs_dir, f)
if os.stat(fi).st_mtime < now - days * 86400:
if os.path.isfile(fi):
logging.debug('Deleting File: %s...', f)
os.remove(fi)
# A class capturing the platform's idea of local time.
ZERO = timedelta(0)
......
// -*- javascript -*-
// Last Modified : Thu May 17 08:44:43 IST 2012
// Last Modified : Thu Jun 14 19:40:31 IST 2012
//
// Copyright (C) 2011, 2012 Sriram Karra <karra.etc@gmail.com>
//
......@@ -30,7 +30,7 @@
// When file version changes there should be an option to
// gracefully sense older config formats and migrate the formats
// to the latest version. Such infra is not yet implemented FIXME.
'file_version' : 2,
'file_version' : 3,
// This prefix is attached to all the Group names, labels
// etc. that are created on Google, field names in BBDB etc.
......@@ -46,10 +46,18 @@
// is used. The value should be a path relative to the root directory
'backup_dir' : './backups',
// Duration in days for holding backup files. Every run will check
// for older files and delete them if any are found
'backup_hold_period' : 7,
// Logs are written to this directory. Value should be a name
// relative to the root diretory of the distribution.
'log_dir' : './logs',
// Duration in days for holding log files. Every run will check
// for older files and delete them if any are found
'log_hold_period' : 7,
// When a profile is created it is populated with the following
// default values.
'profile_defaults' : {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment