Skip to content

Instantly share code, notes, and snippets.

@lestercheung
Last active August 29, 2015 13:56
Show Gist options
  • Save lestercheung/9345692 to your computer and use it in GitHub Desktop.
Save lestercheung/9345692 to your computer and use it in GitHub Desktop.
What's wrong with this? (See comment for trackback)
# -*- coding: utf-8 -*-
from parsley import makeGrammar
from pprint import pprint
import datetime
cmdstart = """Perforce server info:
2014/01/02 02:32:33 pid 19834 p4user@p4client 10.20.0.238 [p4/2013.3/DARWIN90X86_64/740675] 'user-submit -d Lr5 catalog dump 2014/01/02 lr5.lrcat.sql.xz'"""
cmdcomplete = """Perforce server info:
2014/01/02 02:32:33 pid 19834 completed .216s 4+8us 1251+0io 0+0net 2472k 0pf """
cmdtrack = """Perforce server info:
2014/01/02 02:32:33 pid 19834 p4user@p4client 10.20.0.238 [p4/2013.3/DARWIN90X86_64/740675] 'user-submit -d Lr5 catalog dump @2014/01/02 lr5.lrcat.sql.xz'
--- lapse .218s
--- usage 4+12us 1251+0io 0+0net 2472k 0pf
--- rpc msgs/size in+out 3+4/0mb+0mb himarks 64835/131068 snd/rcv .000s/.001s
--- db.user
--- pages in+out+cached 3+0+2
--- locks read/write 1/0 rows get+pos+scan put+del 1+0+0 0+0
--- db.group
--- pages in+out+cached 3+0+2
--- locks read/write 1/0 rows get+pos+scan put+del 0+7+14 0+0
--- db.domain
--- pages in+out+cached 4+0+2
--- locks read/write 2/0 rows get+pos+scan put+del 1+0+0 0+0
--- db.view
--- pages in+out+cached 4+0+4
--- locks read/write 3/0 rows get+pos+scan put+del 0+3+6 0+0
--- db.revcx
--- pages in+out+cached 2+0+1
--- locks read/write 1/0 rows get+pos+scan put+del 0+0+0 0+0
--- total lock wait+held read/write 0ms+92ms/0ms+0ms
---
--- pages in+out+cached 7+0+5
--- locks read/write 2/0 rows get+pos+scan put+del 0+1+1 0+0
--- total lock wait+held read/write 0ms+93ms/0ms+0ms
--- max lock wait+held read/write 0ms+92ms/0ms+0ms
--- db.workingx
--- pages in+out+cached 2+0+1
--- locks read/write 1/0 rows get+pos+scan put+del 0+0+0 0+0
--- total lock wait+held read/write 0ms+79ms/0ms+0ms
--- db.trigger
--- pages in+out+cached 3+0+2
--- locks read/write 1/0 rows get+pos+scan put+del 0+1+5 0+0
--- total lock wait+held read/write 0ms+1ms/0ms+0ms
--- db.change
--- pages in+out+cached 3+0+1
--- locks read/write 2/0 rows get+pos+scan put+del 0+0+0 0+0
--- total lock wait+held read/write 1ms+54ms/0ms+0ms
--- max lock wait+held read/write 1ms+54ms/0ms+0ms
--- db.changex
--- pages in+out+cached 2+0+1
--- locks read/write 1/0 rows get+pos+scan put+del 0+0+0 0+0
--- total lock wait+held read/write 0ms+48ms/0ms+0ms
--- db.desc
--- pages in+out+cached 2+0+1
--- locks read/write 1/0 rows get+pos+scan put+del 0+0+0 0+0
--- total lock wait+held read/write 0ms+20ms/0ms+0ms
--- db.job
--- pages in+out+cached 3+0+1
--- locks read/write 2/0 rows get+pos+scan put+del 0+0+0 0+0
--- total lock wait+held read/write 0ms+13ms/0ms+0ms
--- max lock wait+held read/write 0ms+13ms/0ms+0ms
--- db.fixrev
--- pages in+out+cached 3+0+1
--- locks read/write 2/0 rows get+pos+scan put+del 0+0+0 0+0
--- db.bodtext
--- pages in+out+cached 4+0+2
--- locks read/write 2/0 rows get+pos+scan put+del 4+0+0 0+0
--- total lock wait+held read/write 0ms+1ms/0ms+0ms
--- max lock wait+held read/write 0ms+1ms/0ms+0ms
--- db.protect
--- pages in+out+cached 3+0+2
--- locks read/write 1/0 rows get+pos+scan put+del 0+1+15 0+0
--- db.monitor
--- pages in+out+cached 3+2+2
--- locks read/write 0/1 rows get+pos+scan put+del 0+0+0 1+0
"""
p4logGrammar = r"""
sp = ' '
# override builtin rule
# ws = ( ' ' | '\t' | '\n' )
thing = cmdstart | cmdcomplete | cmdtrack
cmdstart = header:err p4date:d p4time:t pid:pid p4user:u '@' p4client:c ip:ip app:app cmd:cmd
-> dict(type='cmd/start', err=err, date=d, time=t, pid=pid,
user=u, client=c, ip=ip, app=app, cmd=cmd)
header = 'Perforce server ' ('info' | 'error'):r ':' ws -> r=='error'
p4date = num:y '/' num:m '/' num:d sp -> datetime.date(year=y, month=m, day=d)
p4time = num:h ':' num:m ':' num:s sp -> datetime.time(hour=h, minute=m, second=s)
pid = 'pid ' num:p sp -> p
p4user = <( letterOrDigit | ~( '@' | '#' | sp )anything )*>
p4client = p4user:c sp -> c
ipv4 = <digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3}>
ip = ipv4:i sp -> i
# FIXME: app name can in fact contains "]"!!
app = '[' (~']' anything)*:c '] ' -> ''.join(c)
# FIXME: cmd can contains comma!!
cmd = "'" ( ~("'" | '\n' | '--- ') anything)*:c "'" -> ''.join(c)
cmdcomplete = header:err p4date:d p4time:t pid:pid 'completed ' exectime:et rusage:ru io:io net:net mem:mem pf:pf sp
-> dict(type='cmd/complete', err=err, date=d, time=t, pid=pid,
exectime=et, rusage=ru, io=io, net=net, mem=mem, pf=pf)
exectime = < digit* '.' digit+ 's'>:t sp -> t
pf = <digit*>:p 'pf' -> int(p)
cmdtrack = trackheaders:h ws lapse:l ws -> l
# ((lapse|usage|rpc|trackdbs) ws)*:stats
# -> str(stats)
# dict(h.items()+stats.items())
trackheaders = header:h p4date:d p4time:t pid:pid p4user:u '@' p4client:c ip:ip app:app cmd:cmd
-> dict(type='cmd/track', err=h, p4date=d, p4time=t, pid=pid, user=u, client=c, ip=ip, app=app, cmd=cmd)
lapse = '--- lapse ' num:l 's'-> l
usage = '--- usage ' <rusage:r io:io net:n mem:m pf:p4>:u -> u
rusage = <num '+' num 'us'>:r sp -> r
io = <num '+' num 'io'>:i sp -> i
net = <num '+' num 'net'>:n sp -> n
mem = <num 'k'|'m'>:m sp -> m
# --- rpc msgs/size in+out 3+4/0mb+0mb himarks 64835/131068 snd/rcv .000s/.001s
rpc = '--- rpc msgs/size in+out ' num:msg_in '+' num:msg_out '/' num:mb_in '+' num:mb_out ' himarks ' num:recv_mark '/' num:send_mark ' snd/rcv ' num:stime 's/' num:rtime 's'
-> dict(
msg_in=msg_in,
msg_out=msg_out,
mb_in=mb_in,
mb_out=mb_out,
recv_mark=recv_mark,
send_mark=send_mark,
stime=stime,
rtime=rtime)
# trackdbs = ((trackdb|'---'|page_rpt|lock_rpt|tot_rpt|max_rpt) ws)*:all -> all
trackdbs = (('---'|page_rpt|lock_rpt|tot_rpt|max_rpt) ws)*:all -> all
#--- db.user
#--- pages in+out+cached 3+0+2
#--- locks read/write 1/0 rows get+pos+scan put+del 1+0+0 0+0
trackdb = '--- db.' <letter*>:tbl ws trackpages:tp ws tracklocks:tl ws
-> dict(tbl=tbl, page=tp, lock=tl)
trackpages = '--- pages in+out+cached' <num'+'num'+'num>:r ws -> r
tracklocks = '--- locks read/write ' num:r '/' num:w ' get+post+scan put+del'
num:g '+' num:p '+' num:s sp num:put '+' num:d ws
-> dict(read=r, write=w, get=g, post=p, scan=s, put=put, delete=d)
page_rpt = '--- pages in+out+cached ' num:i '+' num:o '+' num:c ws
-> dict(page_in=i, page_out=o, page_cached=c)
# --- locks read/write 2/0 rows get+pos+scan put+del 0+1+1 0+0
lock_rpt = '--- locks read/write ' num:r '/' num:w ' rows get+pos+scan put+del '
num:g '+' num:p '+' num:s sp num:put '+' num:delete
-> dict(read=r, write=w, get=g, pos=p, scan=s, put=put, delete=delete)
# --- total lock wait+held read/write 0ms+93ms/0ms+0ms
tot_rpt = '--- total lock wait+held read/write ' num:rw 'ms+' num:rh 'ms/' num:ww 'ms+' num:wh 'ms' ws
-> dict(read_wait=rw, read_held=rh, write_wait=ww, write_held=wh)
# --- max lock wait+held read/write 0ms+92ms/0ms+0ms
max_rpt = '--- max lock wait+held read/write ' num:rw 'ms+' num:rh 'ms/' num:ww 'ms+' num:wh 'ms' ws
-> dict(tot_read_wait=rw, tot_read_held=rh, tot_write_wait=ww, tot_write_held=wh)
num = ('-' | -> ''):sign (intPart:ds (floatPart(sign ds)
| -> int(sign + ds)))
digits = <digit*>
digit1_9 = :x ?(x in '123456789') -> x
intPart = ((digit1_9 | -> ''):first digits:rest -> first + rest) | digit
floatPart :sign :ds = <('.' digits exponent?) | exponent>:tail
-> float(sign + ds + tail)
exponent = ('e' | 'E') ('+' | '-')? digits
"""
# exectime = timmings (utime+systime)
# io = input+output (blocks)
# net = in+out socket messages
# pf = major page faults
parser = makeGrammar(p4logGrammar, dict(datetime=datetime))
s0 = """Perforce server info:
2014/01/02 02:32:33 pid 19834 p4user@p4client 10.20.0.238 [p4/2013.3/DARWIN90X86_64/740675] 'user-submit -d Lr5 catalog dump @2014/01/02 lr5.lrcat.sql.xz'"""
s1 = """Perforce server info:
2014/01/02 02:32:33 pid 19834 p4user@p4client 10.20.0.238 [p4/2013.3/DARWIN90X86_64/740675] 'user-submit -d Lr5 catalog dump @2014/01/02 lr5.lrcat.sql.xz'
--- lapse .218s"""
pprint(parser(cmdstart).thing())
pprint(parser(cmdcomplete).thing())
pprint(parser(s1).thing())
# plp(cmdend).thing()
# plp(cmdtrack).thing()
# {'app': 'p4/2013.3/DARWIN90X86_64/740675',
# 'client': 'p4client',
# 'cmd': 'user-submit -d Lr5 catalog dump 2014/01/02 lr5.lrcat.sql.xz',
# 'date': datetime.date(2014, 1, 2),
# 'err': False,
# 'ip': '10.20.0.238',
# 'pid': 19834,
# 'time': datetime.time(2, 32, 33),
# 'type': 'cmd/start',
# 'user': 'p4user'}
# {'date': datetime.date(2014, 1, 2),
# 'err': False,
# 'exectime': '.216s',
# 'io': '1251+0io',
# 'mem': '2472k',
# 'net': '0+0net',
# 'pf': 0,
# 'pid': 19834,
# 'rusage': '4+8us',
# 'time': datetime.time(2, 32, 33),
# 'type': 'cmd/complete'}
# Traceback (most recent call last):
# File "p4log.py", line 181, in <module>
# pprint(parser(s1).thing())
# File "/Library/Python/2.7/site-packages/parsley.py", line 98, in invokeRule
# raise err
# ometa.runtime.ParseError:
# --- lapse .218s
# ^
# Parse error at line 3, column 0: expected EOF. trail: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment