forked from oZONo32/EHCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classapp.py
executable file
·190 lines (158 loc) · 4.75 KB
/
classapp.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Base Application class for database and similar functions.
"""
import os,sys,time,commands,socket,traceback
import shutil
import MySQLdb,datetime
import MySQLdb.cursors
import ConfigParser, os
class Application:
input=''
output=''
filefinished=False
dbhost=''
dbname=''
dbusername=''
dbuserpassword=''
activeuser=''
logedin_username=''
def initialize(self):
self.connecttodb()
def __init__(self):
self.initialize()
self.hataayikla=False
def findValueInStr(self,str,find):
if str.count(find)<0:
return ""
ret=str[len(find)+1:]
return ret
def readDbConfig(self):
print 'reading config...'
ehcpdir=''
dosya='/etc/ehcp/ehcp.conf'
try:
config = ConfigParser.ConfigParser()
config.read(dosya)
ehcpdir=config.get('ehcp','ehcpdir')
except Exception, e:
try:
f=open(dosya)
except:
print "Error:dosya acarken hata olustu.",dosya
return
for i in f:
i=i.strip()
if i.count('ehcpdir=')>0:
ehcpdir=self.findValueInStr(i,'ehcpdir')
if ehcpdir!='':
print "found ehcpdir setting:",ehcpdir
else:
print "ehcpdir setting not found in (/etc/ehcp/ehcp.conf) !"
return
try:
dosya=ehcpdir+'/config.php'
f=open(dosya)
except:
print "Error:dosya acarken hata olustu.",dosya
return
for i in f:
i=i.strip()
if i.count('$dbhost')>0:
self.dbhost=self.findValueInStr(i,'$dbhost')[1:-2]
if i.count('$dbname')>0:
self.dbname=self.findValueInStr(i,'$dbname')[1:-2]
if i.count('$dbusername')>0:
self.dbusername=self.findValueInStr(i,'$dbusername')[1:-2]
if i.count('$dbpass')>0:
self.dbuserpassword=self.findValueInStr(i,'$dbpass')[1:-2]
print "db settings:",self.dbhost,self.dbname,self.dbusername
def connecttodb(self,db='ehcp'):
print "connecting to db.. ",db
self.readDbConfig()
try:
hostn=socket.gethostname()
passw=''
self.connection = MySQLdb.connect(self.dbhost, self.dbusername, self.dbuserpassword, self.dbname,cursorclass=MySQLdb.cursors.DictCursor)
#self.connection = MySQLdb.connect("localhost", "root", passw,db,cursorclass=MySQLdb.cursors.DictCursor)
self.conn=self.connection.cursor()
self.connection.set_character_set('utf8')
self.conn.execute("set names utf8")
self.conn.execute("SET CHARACTER SET utf8")
self.conn.execute("SET COLLATION_CONNECTION = 'utf8_turkish_ci'")
self.conn.execute("SET autocommit= on")
self.connected=True
#print "Baglanti tamam."
except Exception, ex:
print "Veritabanina baglanirken hata olustu.",db,str(ex)
def checkDuplicateProgram(self):
pid=os.getpid()
# bu programin ismini de al.
progname=commands.getoutput("ps ax | grep -v grep | grep "+pid.__str__()+" | awk '{print $6}'")
progname=progname.strip()
progname=progname.split('/').pop() # path ile beraber yazinca boylece, sondaki programi aliyor.
print "Progname:",progname
# ismi ayni olan, ancak pidi farkli baska program var mi ?
otherprog=commands.getoutput("ps ax | grep -v grep | grep '"+progname+"' | grep -v "+pid.__str__()+" | awk '{print $5 \" \" $6}'")
otherprog=otherprog.strip()
if otherprog !='':
progs=commands.getoutput("ps ax | grep -v grep | grep '"+progname+"'")
print progs,"\n"
print "program zaten calisiyor. ",pid," cikiyor..."
sys.exit()
def query(self,q,debugprint=False):
try:
if debugprint: print q
self.conn.execute(q)
return self.conn.fetchall()
except Exception,e:
print "** query islenirken hata olustu:",q,repr(e)
self.traceback()
#traceback.print_exc(file=sys.stdout)
#print '-'*60
return False
def kayitsayisi(self,tablo,where=''):
q="select count(*) as sayi from "+tablo
if where!='':
q=q+" where "+where
res=self.query(q)
if type(res)==type(None):
print "kayitsayisi: kayit alinamadı:",q,res
return -1
elif len(res)==0:
print "kayitsayisi: kayit alinamadı:",q,res
return -1
else:
return res[0]['sayi']
def executequery(self,q,debugprint=False):
try:
if debugprint: print q
return self.conn.execute(q)
except Exception, ex:
print "++query islerken hata olustu:",q,str(ex)
#self.traceback()
if str(ex).count('gone away'):
self.connecttodb()
try:
return self.conn.execute(q)
except:
pass
if self.hataayikla:
raise
return False
def traceback(self):
if os.path.exists("./traceit"):
print '-'*60
traceback.print_stack()
print '-'*60
if os.path.exists("./raiseit"):
raise
def escape(self,s):
# http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20
if type(s)==unicode:
return self.connection.escape_string(s.encode('utf-8'))
else:# type(s)==str:
return self.connection.escape_string(s)
def esc(self,s):
return self.escape(s)