-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdelete_old_infs.py
64 lines (53 loc) · 1.94 KB
/
delete_old_infs.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
# IM - Infrastructure Manager
# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import datetime
sys.path.append("..")
sys.path.append(".")
from IM.config import Config
from IM.db import DataBase
class DeleteInfs():
@staticmethod
def delete_data_from_db(db_url, date):
db = DataBase(db_url)
if db.connect():
db.execute("DELETE FROM inf_list WHERE deleted = 1 and date < '%s';" % date)
db.close()
else:
sys.stderr.write("ERROR connecting with the database!.")
sys.exit(-1)
if __name__ == "__main__":
if not Config.DATA_DB:
sys.stderr.write("No DATA_DB defined in the im.cfg file!!\n")
sys.exit(-1)
sys.stdout.write("Deleting old data from DB: %s.\n" % Config.DATA_DB)
date = None
if len(sys.argv) > 1:
date = sys.argv[1]
date = date.replace("/", "-")
parts = date.split("-")
try:
year = int(parts[0])
month = int(parts[1])
day = int(parts[2])
datetime.date(year, month, day)
except:
sys.stdout.write("Incorrect date format (YYYY-MM-DD).\n")
sys.exit(1)
else:
sys.stdout.write("No Date specified.\n")
sys.exit(1)
DeleteInfs.delete_data_from_db(Config.DATA_DB, date)