-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.py
146 lines (129 loc) · 5.37 KB
/
util.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
"""A utility for maintaining the GFYP database."""
import sys
import csv
import logging
import shutil
from datetime import datetime
import gfyp_db #gfyp_db.py
from common import pretty_print, log #common.py
def usage():
"""Print usage info."""
usage_str = (
"GFYP Utilities\n"
"usage: python util.py <$BOLD$command$END$> [command parameters space "
"separated]\n"
"Commands:\n"
" $BOLD$usage$END$ - prints this message\n"
" $BOLD$build$END$ - creates a blank database named db.db\n"
" $BOLD$add$END$ (domain name) (email address) [optional: filename of csv file containing additional tlds] - inserts a new "
"domain(s) to monitor into db.db\n"
" $BOLD$removemonitor$END$ (domain name) - removes a domain from "
"being monitored\n"
" $BOLD$removeentry$END$ (domain name) - removes an identified "
"domain from the found entries\n"
" $BOLD$dump$END$ (file name) - Writes the contents of the found "
"domain name table into the file in CSV format\n"
" $BOLD$migrate$END$ - Upgrades the GFYP database to the most "
"recent schema format\n"
" $BOLD$addnote$END$ (domain name) (note in quotes)- Add a note "
"to a discovered domain entry")
pretty_print(usage_str)
sys.exit()
def dump():
"""Write database to CSV file."""
filename = sys.argv[2]
with gfyp_db.DatabaseConnection() as db_con:
with open(filename, 'wb') as csvfile:
csvoutput = csv.writer(csvfile)
found_entries = db_con.get_all_found_domains()
entries_iter = found_entries.fetchall()
for entry in entries_iter:
csvoutput.writerow(entry)
print(("Wrote %d entries to '%s'." % (len(entries_iter), filename)))
def build():
"""Create tables."""
with gfyp_db.DatabaseConnection() as db_con:
is_err = db_con.table_init()
err_msg = ", but with errors"
msg = "Database is initalized%s." % (err_msg if is_err else '')
print(msg)
log_level = logging.ERROR if is_err else logging.INFO
log(msg, log_level)
def add_domain():
"""Inserts a new domain to monitor"""
if len(sys.argv) != 4 and len(sys.argv) != 5:
log("Incorrect number of arguments for adding domain: %s" % sys.argv,
logging.ERROR)
usage()
email_notif_addr = sys.argv[3]
domain_list = []
domain_list.append(sys.argv[2])
if len(sys.argv) == 5:
#Looks like a TLD file is present, add them as well
baseName = ((sys.argv[2]).rsplit('.'))[0]
with open(sys.argv[4],'rb') as csvfile:
csvreader = csv.reader(csvfile)
for tld in csvreader:
domain_list.append(baseName+"."+tld[0])
with gfyp_db.DatabaseConnection() as db_con:
for domain in domain_list:
db_con.add_watch_entry(domain, email_notif_addr)
def remove_domain():
"""Removes a domain from being monitored"""
domain_name = sys.argv[2]
with gfyp_db.DatabaseConnection() as db_con:
db_con.delete_watch_entry(domain_name)
def remove_entry():
"""Removes an identified domain from the list of found entries"""
domain_name = sys.argv[2]
with gfyp_db.DatabaseConnection() as db_con:
db_con.delete_found_domain(domain_name)
def migrate():
"""Update the database to the current schema version"""
is_curr = False
db_ver = 0
with gfyp_db.DatabaseConnection() as db_con:
is_curr = db_con.is_db_current()
db_ver = db_con.get_version()
if not is_curr:
dst = "db.bak.%s" % str(datetime.now())
msg = "Updated database to most recent version - Existing database stored as: %s" % dst
shutil.move("db.db",dst)
build()
if db_ver == 0:
# Case db_ver == 0: Needs to be modified to account for UNIQUE monitor domains
with gfyp_db.DatabaseConnection() as db_con:
with gfyp_db.DatabaseConnection(filename=dst) as old_db_con:
existing_watch_entries = old_db_con.get_watch_entries()
for entry in existing_watch_entries:
db_con.add_watch_entry(entry[1],entry[0])
existing_found_entries = old_db_con.get_all_found_domains()
entries_iter = existing_found_entries.fetchall()
for entry in entries_iter:
db_con.add_discovered_domain(entry[0],entry[1])
else:
msg = "Database is currently at the most recent schema version. No changes necessary."
print(msg)
log(msg,logging.INFO)
def addnote():
"""Add a note for a found domain"""
domain_name = sys.argv[2]
note = sys.argv[3]
with gfyp_db.DatabaseConnection() as db_con:
db_con.add_note(domain_name,note)
FUNCTIONS = {'build': build,
'usage': usage,
'add': add_domain,
'removeentry' : remove_entry,
'removemonitor' : remove_domain,
'dump' : dump,
'migrate' : migrate,
'addnote' : addnote}
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] not in FUNCTIONS:
log("Invalid arguments: %s" % sys.argv, logging.ERROR)
usage()
else:
# python util.py (utility function argument) (function parameters space separated)
FUNCTIONS[sys.argv[1]]()