-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_ingestion.py
More file actions
281 lines (230 loc) · 9.03 KB
/
data_ingestion.py
File metadata and controls
281 lines (230 loc) · 9.03 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""Get data from quincy server using the csv file,
clean the data, merge all logs in one file and
save in corresponding buckets."""
import os
from stat import S_ISDIR
import csv
import shutil
import re
import sys
import paramiko
paramiko.util.log_to_file('/tmp/paramiko.log')
HOST = "ttqc-shell03"
PORT = 22
TRANSPORT = paramiko.Transport((HOST, PORT))
USERNAME = "testtool"
PASSWORD = "B3k1nD+"
TRANSPORT.connect(username=USERNAME, password=PASSWORD)
SFTP = paramiko.SFTPClient.from_transport(TRANSPORT)
# buckets = ['Hardware', 'Other', 'Script', 'Software', 'Tools']
BUCKETS = dict({'Hardware': ['HW'], 'Software': ['SW', 'TOXIC-PR', 'TOXIC-BUILD'],
'Script': ['SCRIPT'], 'Tools': ['TOOLS']})
BUCKETS['Other'] = ['FALSE-FAILURE-ANALYZED', 'FALSE-FAILURE-RESOLVED', 'JBATCH-UPDATE',
'AUTO-UPDATE', 'PARAMS-ISSUE', 'OTHER', 'matchUp']
RE_LIST = [
r'(Jan?|Feb?|Mar?|Apr?|May|Jun?|Jul?|Aug?|Sep?|Oct?|Nov?|Dec?)\s+\d{1,2}\s+',
r'\d{2}:\d{2}:\d{2}\s+',
r'[\[].*?[\]]',
r'<\/?data>',
r'<\/?value>',
r'<\/?valueType>',
r'<\/?name>',
r'<\/?valueUnit>',
r'<\/?object-value>',
r'<\/?object-value-type>',
r'<\/?cli>',
r'<[0-9]{4,5}>',
r'<\/?DATA>'
]
# Remove all extraneous tags from all of the log files and absolute path of script
def trim_logs(filepath):
"""Performs intial cleaning of logs as per RE_LIST.
Parameters: file path
Returns: edited text from log
"""
# Remove absolute path found in beginning of script log.
script_marker = 'script-exec/'
text = open(filepath, errors='replace').read()
text = re.sub(r'[\[]ERROR[\]]\s+', 'ERROR ', text) # Flags any ERROR tag in the log.
for i in RE_LIST:
text = re.sub(i, '', text)
new_line = text.find(script_marker)
text = text[new_line + len(script_marker):]
return text
def parse_xml(*filepath):
"""Remove xml tags and merger output_xml and .log files.
Parameters: file path
Returns: unique text from log
"""
text = re.sub(r'<[^<]+>', "", open(filepath[0], errors='ignore').read())
# Rid of all tags from XML file
text = re.sub(r'[\[].*[\]]', '', text)
lines = text.split("\n")
unique_lines = set(lines)
# Create list of all unique lines found in XML file
unique_lines = list(unique_lines)
for i, u_line in enumerate(unique_lines):
if u_line:
unique_lines[i] = raw_string_xml(unique_lines[i])
unique_lines = set(unique_lines)
if len(filepath) > 1:
with open(filepath[1]) as file:
line = file.readline()
while line:
# Get rid of all extraneous characteristics in string
davo = raw_string_log(line)
# Append line to text to be added to .txt file if not in unique_lines
if davo not in unique_lines:
text += line + '\n'
line = file.readline()
return text
# Get rid of extraneous characters in lines
def raw_string_xml(text):
"""Remove new lines and tabs from text.
Parameters: string
Returns: string
"""
text = text.lower().lstrip()
text = text.rstrip('\n')
text = text.rstrip('\r')
return text
def raw_string_log(line): # Remove all tags from file
"""Remove regex matching lines from text.
Parameters: string
Returns: string
"""
davo = re.sub(r'[\[].*[\]]', '', line)
davo = davo.rstrip('\n')
davo = davo.rstrip('\r')
davo = davo.replace('<', '<')
davo = davo.replace('>', '>')
davo = davo.lower().lstrip()
return davo
def sftp_walk(remote_path):
"""Walks the remote path.
Parameters: dir path on remote
Returns: path, sub-dir, list of files
"""
path = remote_path
files = []
folders = []
for file in SFTP.listdir_attr(remote_path):
if S_ISDIR(file.st_mode):
folders.append(file.filename)
else:
files.append(file.filename)
if files:
yield path, files
for folder in folders:
new_path = os.path.join(remote_path, folder)
for n_file in sftp_walk(new_path):
yield n_file
def download_file(source, destination):
"""download the source file in destination folder.
Parameters: source path, destination path
Returns: string: Found, Not Found
"""
try:
for path, files in sftp_walk(source):
# if TOBY
if any('output.xml' in item for item in files):
for item in files:
if (item.endswith('.log') and item != 'stdout.log')\
or item.endswith('output.xml'):
if not os.path.exists(destination):
os.makedirs(destination)
SFTP.get(os.path.join(os.path.join(path, item)), destination + item)
# print(destination, item)
return 'Found'
# if JT
if any('.pl.log' in item for item in files):
for item in files:
if item.endswith('.pl.log') or item.endswith('.expect'):
if not os.path.exists(destination):
os.makedirs(destination)
SFTP.get(os.path.join(os.path.join(path, item)), destination + item)
# print(destination, item)
return 'Found'
return 'Not Found'
except IOError as err:
print('File Not Found ' + source + ' ' + str(err))
return 'Not Found'
def clean_log(file_path):
"""Remove any duplicacy from logs
Parameters: dir path
"""
lines_seen = set() # holds lines already seen
text = trim_logs(file_path).split('\n')
outfile = open(file_path, "w", errors='ignore')
for line in text:
# Removes duplicate lines
if line not in lines_seen and len(line) < 400:
outfile.write(line + '\n')
lines_seen.add(line)
outfile.close()
def combine_log(logs_path):
"""Combines all logs at path into one text file.
Parameters: dir path
"""
print(logs_path)
for log_path, _, log_names in os.walk(logs_path):
# print(logs_path,_,log_names)
with open(logs_path[:-1] + '.txt', 'wb') as big_file:
for log_name in log_names:
in_file = os.path.join(log_path, log_name)
# Removes some tags here while combining the .log and .xml
if '.xml' in log_name:
# print(log_path, log_name, len(log_names))
expect_log = log_name.replace('_output.xml', '.log')
if expect_log in log_names:
content = parse_xml(in_file, os.path.join(log_path, expect_log))
log_names.remove(log_name)
log_names.remove(expect_log)
else:
content = parse_xml(in_file)
big_file.write(content.encode('utf-8'))
else:
if log_name.replace('.log', '_output.xml') not in log_names:
with open(in_file, 'rb') as log:
shutil.copyfileobj(log, big_file)
clean_log(logs_path[:-1] + '.txt')
shutil.rmtree(logs_path[:-1])
def get_data_from_csv(src_csv='dr_data2.csv', target_dir='clean_data'):
"""Downloads the dr_data using the src_csv on target_dir
Parameters: src_csv, destination dir
"""
with open(src_csv, encoding='utf-8-sig') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
log_path = row['logpath']
exit_code = row['reg_exitcode']
if 'prod' in log_path and exit_code == 'FAIL':
debug_tag = row['debug_tag']
if debug_tag not in ('0', '-'):
for bucket, tags in BUCKETS.items():
for tag in tags:
if tag in debug_tag:
if not (tag == 'OTHER' and debug_tag == 'SCRIPT-OTHER'):
tmp = log_path.split('prod')[1]
tmp = list(filter(lambda x: x != '', tmp.split('/')))
exec_id = tmp[-3]
destination = os.path.join(os.getcwd(), target_dir,
bucket, exec_id) + '/'
status = download_file(log_path, destination)
row['download_status'] = status
if status == 'Found':
combine_log(destination)
def main():
"""Driver code
Parameters :csv_file_path & destination_folder_name.
"""
if len(sys.argv) > 1 and len(sys.argv) == 3:
src_csv = sys.argv[1]
target_dir = sys.argv[2]
get_data_from_csv(src_csv, target_dir)
print('Done')
else:
print('Takes 2 arguments: csv_file_path & destination_folder_name.')
# get_data_from_csv()
if __name__ == '__main__':
main()