Menu

[r1639]: / afanasy / python / check_parser.py  Maximize  Restore  History

Download this file

163 lines (122 with data), 3.9 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os
import subprocess
import sys
import cgruutils
def UsageExit(message=''):
"""Missing DocString
:param message:
:return:
"""
if message != '':
print('Error: %s' % message)
print('Usage: %s [parser type] [frames number] [command line]' %
os.path.basename(sys.argv[0]))
exit(1)
if len(sys.argv) < 4:
UsageExit()
try:
framesNum = int(sys.argv[2])
except: # TODO: Too broad exception clause
UsageExit('%s\nInvalid frames number.' % str(sys.exc_info()[1]))
parserType = sys.argv[1]
parserModule = __import__('parsers', globals(), locals(), [parserType])
cmd = 'parserModule.%s.%s()' % (parserType, parserType)
print(cmd)
parser = eval(cmd)
taskInfo = dict()
taskInfo['frames_num'] = framesNum
taskInfo['wdir'] = os.getcwd()
taskInfo['environment'] = dict()
taskInfo['environment']['PDG_DIR'] = os.getcwd()
parser.setTaskInfo(taskInfo)
arguments = []
for i in range(3, len(sys.argv)):
arguments.append(sys.argv[i])
process = subprocess.Popen(arguments, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.close()
stdout = process.stdout
stderr = process.stderr
def printMuted(i_str):
sys.stdout.write('\033[1;30m')
sys.stdout.write(i_str)
sys.stdout.write('\033[0m')
sys.stdout.flush()
Parser_Error = False
Parser_BadResult = False
Parser_Warning = False
Parser_Activity = None
Parser_Resources = None
Parser_Report = None
resources = ''' {"host_resources":{
"cpu_num":4, "cpu_mhz":2790, "cpu_loadavg":[4,6,5],
"cpu_user":9, "cpu_nice":0, "cpu_system":4, "cpu_idle":84, "cpu_iowait":0, "cpu_irq":0, "cpu_softirq":0,
"mem_total_mb":7853, "mem_free_mb":4885, "mem_cached_mb":2724, "mem_buffers_mb":475,
"swap_total_mb":7812, "swap_used_mb":103,
"hdd_total_gb":34, "hdd_free_gb":11, "hdd_rd_kbsec":0, "hdd_wr_kbsec":47, "hdd_busy":0,
"net_recv_kbsec":0, "net_send_kbsec":0,
"logged_in_users":["foo","bar"]
}}'''
resources = json.loads(resources)
while True:
stdout.flush()
data = stdout.readline()
if data is None:
break
if len(data) < 1:
break
printMuted(cgruutils.toStr(data))
args = dict()
args['mode'] = 'RUN'
args['pid'] = process.pid
args['data'] = data
args['resources'] = json.dumps(resources)
parser.parse(args)
info = 'Parse:'
info += ' %d%%: %d frame %d%%;' % (parser.percent, parser.frame, parser.percentframe)
if len(parser.activity):
info += ' Activity: %s;' % parser.activity
Parser_Activity = parser.activity
if len(parser.resources):
info += ' Resources: %s;' % parser.resources
Parser_Resources = parser.resources
if len(parser.report):
info += ' Report: %s;' % parser.report
Parser_Report = parser.report
if parser.warning:
info += '\nPARSER WARNING'
Parser_Warning = True
if parser.error:
info += '\nPARSER ERROR'
Parser_Error = True
if parser.badresult:
info += '\nPARSER BAD RESULT'
Parser_BadResult = True
sys.stdout.write('%s\n' % info)
sys.stdout.flush()
sys.stdout.flush()
print('\nResult:')
if len(parser.files_all):
print('Files:')
for afile in parser.files_all:
print(afile)
StrErr = cgruutils.toStr(stderr.read()).replace('\r', '')
if len(StrErr):
print('\nSTDERR:')
print(StrErr)
if Parser_Activity:
print('Activity: ' + Parser_Activity)
if Parser_Resources:
print('Resources: ' + Parser_Resources)
if Parser_Report:
print('Report: ' + Parser_Report)
if Parser_Warning:
print('PARSER WARNING')
if Parser_Error:
print('PARSER ERROR')
if Parser_BadResult:
print('PARSER BAD RESULT')
if not Parser_Warning and not Parser_Error and not Parser_BadResult:
print('\nSUCCESS')
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.