-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautop.py
240 lines (172 loc) · 4.13 KB
/
autop.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding:utf-8 -*-
import os
import sys
import string
from collections import namedtuple
try:
os.path.join(__path__, 'libs')
except Exception, e:
__path__=os.getcwd()
libs_path = os.path.join(__path__, 'libs')
if libs_path not in sys.path:
sys.path.insert(0, libs_path)
from dbutil import mssql
class BriefObject(dict):
def __init__(self, *args, **kwargs):
super(Vardata, self).__init__(*args, **kwargs)
self.__dict__.update(kwargs)
def transfer_to_briefobject(datatable):
return [BriefObject(rowset) for rowset in datatable]
class TemplateList:
def __init__(self)
self.tls=[]
def push(self,command):
self.tls.push(command)
def insert(self,locate,command):
self.tls.insert(locate,command)
def __getitem__(self,index):
return self.tls[index]
def __setitem__(self,index,data):
if isinstance(data,Command):
self.tls[index]=data
else:
raise TypeError("*data %s type is error" % (data,))
class App:
def __init__(self,checklist):
self.stack_buttom=None
self.stack_top=self.stack_buttom
self.templateList=TemplateList
def init_content(self):
pass
def destory_content(self):
pass
def run(self):
self.init_content()
for cmd in self.templateList:
frame=CStackFrame(cmd)
frame.back=self.stack_buttom
self.stack_top=frame
if self.stack_buttom:
self.stack_buttom=frame
frame.run()
self.destory_content()
class CStackFrame:
def __init__(self,cmdobj):
self._command=cmdobj
self._global_variables={}
self._local_variables={"servername":"clsfltorder14","dns":"fltorder.db.ctripcorp.com","port":55944,"host":dataset}
self._back=None
def run(self):
#update variable
self.command.run()
def set_local_variable(self,key,value):
self._local_variables[key]value
@property
def local_variables(self):
return self._local_variables
def set_global_variable(self,key,value):
self._global_variables[key]value
@property
def global_variables(self):
return self._global_variables
class VariablesCollections:
def __init__(self,name,value,isreadonly=False):
self.name=name
self._value=value
self._readonly=isreadonly
@property
def readonly(self):
return self._readonly
@property
def value(self):
return self._value
def __getattr__(self,attrname):
pass
class Command(object):
def __init__(self):
pass
class ImportCommand(Command):
def __init__(self,module):
self._module=module
def text(self):
return "import %s" % (module)
class ForeachCommand(Command):
template="""
for d in datalist:
cmd(d)
"""
def __init__(self,datalist,cmd):
self.data=datalist
def run(self,frame):
pass
def __str__(self):
pass
def __repr__(self):
self.__str__()
class MapCommand(Command):
template="""
for d in datalist:
cmd(d)
"""
def __init__(self,datalist,template):
self.data=datalist
self.template=template
def run(self,frame):
r=[]
for d in datalist:
t=string.Template(template)
t.substitute(d.transfer)
t.substutite(frame.lobalVariables)
t.substutite(frame.globalVariables)
def __str__(self):
pass
def __repr__(self):
self.__str__()
class ReduceCommand(Command):
template="""
for d in datalist:
cmd(d)
"""
def __init__(self,datalist,cmd):
self.data=datalist
def run(self,frame):
pass
def __str__(self):
pass
def __repr__(self):
self.__str__()
pass
# $service.hostname,$service.port,$service.machine_name
class ScriptHeader:
def __init__(self):
self._template="""
# -*- coding:utf-8 -*-
import sys
import os
try:
os.path.join(__path__, 'libs')
except Exception, e:
__path__=os.getcwd()
libs_path = os.path.join(__path__, 'libs')
if libs_path not in sys.path:
sys.path.insert(0, libs_path)
from dbutil import mssql
"""
def text(self):
return self._template
class ScriptMain:
def __init__(self):
self._template="""
if __name__ =='__main__':
main()
"""
pass
def text(self):
return self._template
if __name__ == '__main__':
sqlcmd=SqlQueryCommand("sqlperformance.db.sh.ctripcorp.com,55944","TraceDB","select name from sys.tables(nolock) where name=%s","AutoCollectNetworkInterfaceQuery")
header=ScriptHeader()
main=ScriptMain()
print header
print main
print sqlcmd