forked from ztwo/Auto_Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.py
More file actions
211 lines (167 loc) · 5.39 KB
/
Utils.py
File metadata and controls
211 lines (167 loc) · 5.39 KB
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
# -*- coding: utf-8 -*-
__author__ = 'joko'
"""
@author:joko
@time: 16/11/8 下午2:52
"""
import time
import subprocess
import os
import sys
import ConfigParser
import sqlite3
import re
def get_now_time():
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
def sleep(s):
return time.sleep(s)
def cmd(cmd):
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,bufsize=1,close_fds=True)
class ConfigIni():
def __init__(self):
self.current_directory = os.path.split(
os.path.realpath(sys.argv[0]))[0]
self.path = os.path.split(__file__)[0].replace('lib','data/test_info.ini')
self.cf = ConfigParser.ConfigParser()
self.cf.read(self.path)
def get_ini(self, title, value):
return self.cf.get(title, value)
def set_ini(self, title, value, text):
self.cf.set(title, value, text)
return self.cf.write(open(self.path, "wb"))
def add_ini(self, title):
self.cf.add_section(title)
return self.cf.write(open(self.path))
def get_options(self, data):
# 获取所有的section
options = self.cf.options(data)
return options
class colour:
@staticmethod
def c(msg, colour):
try:
from termcolor import colored, cprint
p = lambda x: cprint(x, '%s' % colour)
return p(msg)
except:
print (msg)
@staticmethod
def show_verbose(msg):
colour.c(msg, 'white')
@staticmethod
def show_debug(msg):
colour.c(msg, 'blue')
@staticmethod
def show_info(msg):
colour.c(msg, 'green')
@staticmethod
def show_warn(msg):
colour.c(msg, 'yellow')
@staticmethod
def show_error(msg):
colour.c(msg, 'red')
class Logging:
flag = True
@staticmethod
def error(msg):
if Logging.flag == True:
# print get_now_time() + " [Error]:" + "".join(msg)
colour.show_error(get_now_time() + " [Error]:" + "".join(msg))
@staticmethod
def warn(msg):
if Logging.flag == True:
colour.show_warn(get_now_time() + " [Warn]:" + "".join(msg))
@staticmethod
def info(msg):
if Logging.flag == True:
colour.show_info(get_now_time() + " [Info]:" + "".join(msg))
@staticmethod
def debug(msg):
if Logging.flag == True:
colour.show_debug(get_now_time() + " [Debug]:" + "".join(msg))
@staticmethod
def success(msg):
if Logging.flag == True:
colour.show_verbose(get_now_time() + " [Success]:" + "".join(msg))
def l():
"""
打印log
文件名+函数名,return
:return:
"""
def log(func):
def wrapper(*args, **kwargs):
t = func(*args, **kwargs)
filename = str(sys.argv[0]).split('/')[-1].split('.')[0]
Logging.success('{}:{}, return:{}'.format(filename, func.__name__, t))
return t
return wrapper
return log
class Asql:
def __init__(self, ):
ini = ConfigIni()
test_db_path = ini.get_ini('test_db', 'test_result')
self.conn = sqlite3.connect(test_db_path)
self.cursor = self.conn.cursor()
self.__is_table()
def execute(self, *args, **kwargs):
"""
:param args:
:param kwargs:
:return: 提交数据
"""
self.cursor.execute(*args, **kwargs)
def close(self):
self.cursor.close()
self.conn.commit()
self.conn.close()
def __is_table(self):
"""
判断表是否存在
:return:
"""
self.cursor.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='test_results'")
row = self.cursor.fetchone()
if row[0] != 1:
self.__built_table()
def __built_table(self):
"""
建表
:return:
"""
self.execute("""
CREATE TABLE test_results
(
case_id INTEGER PRIMARY KEY,
case_name TEXT,
device_name TEXT,
cpu_list TEXT,
mem_list TEXT,
execution_status TEXT,
created_time DATETIME DEFAULT (datetime('now', 'localtime'))
);""")
def insert_per(self, case_name, device_name, cpu_list, mem_list, execution_status, ):
key = "(case_name,device_name,cpu_list,mem_list,execution_status,created_time)"
values = "('{}','{}','{}','{}','{}','{}')" \
.format(case_name, device_name, cpu_list, mem_list, execution_status, get_now_time())
self.execute("INSERT INTO test_results {} VALUES {}".format(key, values))
def select_per(self, case_name, device_name):
statement = "select * from test_results where " \
"case_name = '{}' " \
"and " \
"device_name = '{}' " \
"and " \
"execution_status = 1 " \
"order by created_time desc".format(case_name, device_name)
self.cursor.execute(statement)
row = self.cursor.fetchone()
if row is not None:
cpu = re.findall(r"\d+\.?\d*", row[3])
mem = re.findall(r"\d+\.?\d*", row[4])
return [int(i) for i in cpu], [int(i) for i in mem]
else:
return None
if __name__ == '__main__':
a = Asql()
print (a.select_per('login1', 'sanxing'))
a.close()