-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_tips.py
executable file
·195 lines (154 loc) · 4.65 KB
/
load_tips.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh
# -*- python -*-
# This file is python bilingual: it starts a comment in Python, and is a no-op in shell
""":"
# Find a suitable python interpreter (adapt for your specific needs)
for cmd in python3 python python2; do
command -v > /dev/null $cmd && exec $cmd $0 "$@"
done
echo "Error: Could not find a valid python interpreter --> exiting!"
exit 2
":"""
from __future__ import print_function
from fnmatch import fnmatch
import os, sys, re, getpass, base64, argparse
import MySQLdb as mdb
import warnings
import configparser
try:
input = raw_input
except:
pass
warnings.filterwarnings("ignore", "Unknown table.*")
dividerPat = re.compile(r'^##\-\-*$')
class LD_TIPS(object):
def __init__(self, confFn):
self.__host = None
self.__user = None
self.__passwd = None
self.__db = None
self.__conn = None
self.__confFn = confFn
def __readFromUser(self):
""" Ask user for database access info. (private) """
self.__host = input("Database host:")
self.__user = input("Database user:")
self.__passwd = getpass.getpass("Database pass:")
self.__db = input("Database name:")
self.__writeConfig()
def __writeConfig(self):
config=configparser.ConfigParser()
config.add_section("MYSQL")
config.set("MYSQL","HOST",self.__host)
config.set("MYSQL","USER",self.__user)
config.set("MYSQL","PASSWD",base64.b64encode(self.__passwd))
config.set("MYSQL","DB",self.__db)
fn = self.__db + "_db.conf"
f = open(fn,"w")
config.write(f)
f.close()
def __readConfig(self):
""" Read database access info from config file. (private)"""
confFn = self.__confFn
try:
config=configparser.ConfigParser()
config.read(confFn)
self.__host = config.get("MYSQL","HOST")
self.__user = config.get("MYSQL","USER")
self.__passwd = base64.b64decode(config.get("MYSQL","PASSWD")).decode()
self.__db = config.get("MYSQL","DB")
except configparser.NoOptionError as err:
sys.stderr.write("\nCannot parse the config file\n")
sys.stderr.write("Switch to user input mode...\n\n")
self.__readFromUser()
def db_connect(self):
if(os.path.exists(self.__confFn)):
self.__readConfig()
else:
self.__readFromUser()
try:
self.__conn = mdb.connect(self.__host, self.__user, self.__passwd, self.__db)
cursor = self.__conn.cursor()
cursor.execute("DROP TABLE IF EXISTS tips")
cursor.execute("""
CREATE TABLE IF NOT EXISTS `tips` (
`tips_id` INT unsigned NOT NULL auto_increment,
`msg` varchar(2048) NOT NULL,
PRIMARY KEY (`tips_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1
""")
except:
print("Failed to connect");
raise
def insertTip(self, txt):
cursor = self.__conn.cursor()
txt = txt.replace("'",r"\'")
cursor.execute("START TRANSACTION")
cursor.execute("INSERT INTO tips(msg) VALUES('%s')" % txt)
cursor.execute("COMMIT")
def nrows(self):
cursor = self.__conn.cursor()
query = "SELECT count(*) from tips"
cursor.execute(query)
resultA = cursor.fetchall()
Nrows = resultA[0][0]
print("number of rows: ",Nrows)
def db_disconnect(self):
self.__conn.close()
def files_in_tree(path, pattern):
fileA = []
wd = os.getcwd()
if (not os.path.isdir(path)):
return fileA
os.chdir(path)
path = os.getcwd()
os.chdir(wd)
for root, dirs, files in os.walk(path):
for name in files:
fn = os.path.join(root, name)
if (fnmatch(fn,pattern)):
fileA.append(fn)
fileA.sort()
return fileA
def main():
confFn = None
if (len(sys.argv) > 1):
confFn = sys.argv[1]
else:
A = [ "HPCTips_db.conf", "HPC_Tips_db.conf" ]
for i in A:
if (os.path.exists(i)):
confFn = i
break
if (not confFn):
print("No database config file found -> exiting!");
sys.exit(1)
tips = LD_TIPS(confFn)
tips.db_connect()
fileA = files_in_tree("./tips", "*.tips")
rowT = {}
for fn in fileA:
f = open (fn,"r")
lines = f.readlines()
f.close()
idx = 0
txt = ""
sA = []
rowT[fn] = 0
for s in lines:
s = s.rstrip()
m = dividerPat.search(s)
if (m and sA):
txt = "\n".join(sA)
idx = idx + 1
rowT[fn] = rowT[fn] + 1
tips.insertTip(txt)
sA = []
elif (not m):
sA.append(s)
for fn in rowT:
print(fn+":", rowT[fn])
tips.nrows()
#print("number of rows: ",
tips.db_disconnect()
if ( __name__ == '__main__'): main()