-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataWatcher.py
220 lines (173 loc) · 7.43 KB
/
DataWatcher.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
## Copyright (c) 2009 Astun Technology
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
## THE SOFTWARE.
### DataWatcher.py - Main Process for the DataWatcher process
# Import Astun Stuff
#from SendMail import SendMailMessageSimple1
from SendMail import send_mail
import Output
import Config
import Data
# Import Shared
import sys
import datetime
import lxml.etree as ET
class DataWatcher:
#####
# To Instantiate this class
###
def __init__( self, params ):
# Init
self._bError = False
self._dtStart = datetime.datetime.now( )
self._dtEnd = datetime.datetime.now( )
self._bContinue = False
# Get the Output
self._Out = Output.ClassOutput('DataWatcher')
# Config Filename
if ( len( params ) < 3 ):
self._bError = True
else:
self._sConfigFile = params[ 1 ]
# Does the config file exist
if ( Config.FileExists( self._sConfigFile ) == False ):
self._bConfigError = True
else:
self._bConfigError = False
return
#####
#####
# XSL Transform
###
def XSLTransform(self,xmlFile,xslFile):
self._Out.OutputInfo('Transforming xsl')
try:
dom = ET.parse(xmlFile)
xslt = ET.parse(xslFile)
transform = ET.XSLT(xslt)
newdom = transform(dom)
result = ET.tostring(newdom, pretty_print=True)
except:
self._Out.OutputError( 'On performing the xsl transform', True )
self._Out.OutputException( sys.exc_info( ), True )
raise
return result
#####
# Send an email to the person specified in SMTPToFail
###
def SendErrorEmail(self):
SendMailMessageSimple1(self._sConfigFile,'An error ocurred in the Data comparison process.',self._Out)
return
#####
# Send an email to the person specified in SMTPTo with the results attached
###
def SendResultsEmail(self,query):
sAttachment = self.XSLTransform('email.xml','email.xsl')
file = open('results.html','w')
file.write(sAttachment)
file.close()
sFilename = self._sConfigFile
# Get the various values that we need
sServer = Config.GetValue( sFilename, 'SMTPServer', self._Out )
if ( sServer <> '' ):
sMyName = Config.GetValue( sFilename, 'MyName', self._Out )
sTo = Config.GetValue( sFilename, 'SMTPTo', self._Out )
sFrom = Config.GetValue( sFilename, 'SMTPFrom', self._Out )
sCC = Config.GetValue( sFilename, 'SMTPCC', self._Out )
sBody = Config.GetValue( sFilename, 'SMTPBody', self._Out )
sSubject= Config.GetValue( sFilename, 'SMTPSubject', self._Out )
sSMTPUser = Config.GetValue( sFilename, 'SMTPUser', self._Out )
sSMTPPassword = Config.GetValue ( sFilename, 'SMTPPass', self._Out)
# Info messages
self._Out.OutputInfo( 'Sending e-mail to: ' + sTo + '...' )
if (sCC <> ''):
self._Out.OutputInfo( '... cc: ' + sCC + '...' )
self._Out.OutputInfo( '... regarding ' + sSubject + '...' )
sErrorMsg = self._Out.GetErrorMessage( )
# Build the body of the message
dtNow = datetime.datetime.now( )
sMessageBody = dtNow.strftime( "%a, %d %b %Y %H:%M" ) + '\n\n'
if ( sMyName <> '' ):
sMessageBody += 'Name: ' + sMyName + '\n\n'
sMessageBody += 'Query Name: ' + query + '\n\n'
sMessageBody += 'SQL: ' + Config.GetValue( sFilename, str(query + 'SQL'), self._Out ) + '\n\n'
sMessageBody += 'Unique Key: ' + Config.GetValue( sFilename, str(query + 'UniqueKey'), self._Out ) + '\n\n'
sMessageBody += 'Destination Table: ' + Config.GetValue( sFilename, str(query + 'DestTable'), self._Out ) + '\n\n'
sMessageBody += 'DataWatcher comparison results are attached'
if ( sBody <> '' and sBody <> ' ' ):
sMessageBody += sBody + '\n\n'
if ( sErrorMsg <> '' ):
sMessageBody += sErrorMsg + '\n\nPlease see the log for more details.'
# Make the lists
if ( len( sTo ) > 0 and sTo <> '' ):
lTo = sTo.split( ',' )
else:
lTo = []
if ( len( sCC ) > 0 and sCC <> '' ):
lCC = sCC.split( ',' )
else:
lCC = []
# Do the mail thang
send_mail( send_to = lTo, send_cc = lCC, message_subject = sSubject, message_text = sMessageBody, send_from = sFrom, mail_server = sServer, Out = self._Out, files = ["results.html"], smtp_user = sSMTPUser, smtp_password = sSMTPPassword)
else:
# Indicate we couldn't send
self._Out.OutputInfo( 'No configuration for SendMail' )
return
#####
# Start the Watcher
#####
def DataWatcherStart(self,params):
if (self._bConfigError == True):
print 'Cannot find configuration file. Aborting..'
else:
# My Name is?
self._sName = Config.GetValue( self._sConfigFile, 'MyName', self._Out )
# Flesh the Output out
self._Out.SetFilename( self._sName + '.log' )
self._Out.SetOutputLevel( Output.OUTPUTLEVEL_INFO )
self._Out.SetFileLogging( True )
# Log me
self._Out.OutputInfo('___________________________')
self._Out.OutputInfo('DataWatcher starting')
# Check for Query name, then get SQL,SourceDB and Destination table
tQueries = Config.GetValues(self._sConfigFile,'Query', self._Out)
for sQuery in tQueries:
if (sQuery == params[2]):
self._bContinue = True
sQuery = params[2]
if (self._bContinue == False):
self._Out.OutputError('Configuration information does not exist for the query ' + params[2])
else:
compare = Data.DataCompare(self._sConfigFile, self._Out)
try:
bTableExists = compare.ImportData(sQuery)
# If the table already exists when the import was done, we can do the
# comparison as we have x and x_LASTUPDATE
if (bTableExists == True):
compare.FindDifferences(sQuery)
self.SendResultsEmail(sQuery)
except:
self.SendErrorEmail()
return
#####
# Main
###
if ( len( sys.argv ) < 3 ):
print 'Required parameters: <Configuration Filename> <QueryName>'
else:
control = DataWatcher( sys.argv )
control.DataWatcherStart( sys.argv )
#####