This repository has been archived by the owner on May 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CleanupIQMon.py
95 lines (81 loc) · 3.44 KB
/
CleanupIQMon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/env/python
from __future__ import division, print_function
## Import General Tools
import sys
import os
import argparse
import logging
import datetime
import glob
import IQMon
##-------------------------------------------------------------------------
## Main Program
##-------------------------------------------------------------------------
def main():
##-------------------------------------------------------------------------
## Parse Command Line Arguments
##-------------------------------------------------------------------------
## create a parser object for understanding command-line arguments
parser = argparse.ArgumentParser(
description="Program description.")
## add flags
parser.add_argument("-v", "--verbose",
action="store_true", dest="verbose",
default=False, help="Be verbose! (default = False)")
## add arguments
parser.add_argument("--input",
type=str, dest="input",
help="The input.")
args = parser.parse_args()
##-------------------------------------------------------------------------
## Create logger object
##-------------------------------------------------------------------------
logger = logging.getLogger('MyLogger')
logger.setLevel(logging.DEBUG)
## Set up console output
LogConsoleHandler = logging.StreamHandler()
if args.verbose:
LogConsoleHandler.setLevel(logging.DEBUG)
else:
LogConsoleHandler.setLevel(logging.INFO)
LogFormat = logging.Formatter('%(asctime)23s %(levelname)8s: %(message)s')
LogConsoleHandler.setFormatter(LogFormat)
logger.addHandler(LogConsoleHandler)
## Set up file output
LogFileName = os.path.join('/', 'home', 'panoptesmlo', 'IQMon', 'Logs', 'CleanupLog.txt')
LogFileHandler = logging.FileHandler(LogFileName)
LogFileHandler.setLevel(logging.DEBUG)
LogFileHandler.setFormatter(LogFormat)
logger.addHandler(LogFileHandler)
##-------------------------------------------------------------------------
## Remove old files from Plots directory
##-------------------------------------------------------------------------
config = IQMon.Config()
days_to_keep = 60
logger.info('Examining {} for files that are {} days old.'.format(config.pathPlots, days_to_keep))
now = datetime.datetime.today()
files = glob.glob(os.path.join(config.pathPlots, '*'))
n_removed = 0
for file in files:
file_mod = datetime.datetime.fromtimestamp(os.stat(file).st_mtime)
age = now - file_mod
if age.days >= days_to_keep:
n_removed += 1
os.remove(file)
logger.info(' Removed {} files from Plots directory.'.format(n_removed))
##-------------------------------------------------------------------------
## Remove old files from tmp directory
##-------------------------------------------------------------------------
tmp_days_to_keep = 1
logger.info('Examining {} for files that are {} days old.'.format(config.pathTemp, tmp_days_to_keep))
tmp_files = glob.glob(os.path.join(config.pathTemp, '*'))
n_tmp_removed = 0
for tmp_file in tmp_files:
file_mod = datetime.datetime.fromtimestamp(os.stat(tmp_file).st_mtime)
age = now - file_mod
if age.days >= tmp_days_to_keep:
n_tmp_removed += 1
os.remove(tmp_file)
logger.info(' Removed {} files from tmp directory.'.format(n_tmp_removed))
if __name__ == '__main__':
main()