python script reading config file and then zipping files after X days from directories. dir contains dynamic files. i have specified the extension and the "startswith" in the config. problem i have is it zips up everything. (if i can get this work will add a delete too). i also want it to perform these actions only when it is in the specified server environment. eg if it is in vod just do vod stuff and skip vod2 as defined in the config file.
## config file
[log1]
env = vod2
path = /projects/log/files
zip_time = 7
del_time = 14
ftype = dmplog
[log2]
env = vod
path = /projects/log/files
zip_time = 7
del_time = 14
ftype = dmptrc
# main section
def main():
global zip_cfg
# current time
now = time.time()
true_env = '%s' % (os.environ['LOGNAME'])
env = true_env
zip_cfg = '/home/etc/file_cleanup.cfg'
conf_config = ConfigParser.ConfigParser()
conf_config.read(zip_cfg)
# check environment and then loop thru config file
if env == true_env:
print env
for section in conf_config.sections():
env = conf_config.get(section, 'env')
path = conf_config.get(section, 'path')
del_time = conf_config.get(section, 'del_time')
zip_time = conf_config.get(section, 'zip_time')
ftype = conf_config.get(section, 'ftype')
zip_time2 = int(zip_time)
#print zip_time2
print ""
# check if file older than X days then zip
dirList = os.listdir(path)
for fnames in dirList:
if fnames.endswith(ftype) or fnames.startswith(ftype):
true_file = os.path.join(path, fnames)
print true_file
# diff b/w current time and last time file was modified
ftime = now - zip_time2 * 86400
print ftime
if os.stat(true_file).st_mtime < ftime:
if os.path.isfile(true_file):
#print os.stat(true_file).st_mtime
zippy(path)
#os.remove(path)