Difference between revisions of "Python REGEX Examples"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: =Reformat a LoadRunner Script= <pre> #!/usr/bin/env python # # $Id:$ # #-------------------------------------------------------------------------- import re import os import sys import ...) |
PeterHarding (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 71: | Line 71: | ||
line = line.replace('\t', ' ') | line = line.replace('\t', ' ') | ||
if re.search('test001. | if re.search('test001.performiq.com.au', line): | ||
line = line.replace('test001. | line = line.replace('test001.performiq.com.au', '{System}') | ||
if re.search('web_reg_find\("Text= | if re.search('web_reg_find\("Text=app', line): | ||
ofd.write(separator) | ofd.write(separator) | ||
Line 92: | Line 92: | ||
continue | continue | ||
#----- Cull | #----- Cull logo request call ---------------------------------------------- | ||
if strip_logo: | if strip_logo: |
Latest revision as of 09:09, 3 April 2009
Reformat a LoadRunner Script
#!/usr/bin/env python # # $Id:$ # #-------------------------------------------------------------------------- import re import os import sys import types import getopt import pprint import datetime from string import join, split #-------------------------------------------------------------------------- """ Clean up LoadRuner script data blocks """ __version__ = '1.0.0' debug_flg = False verbose_flg = False strip_logo = True strip_css = True add_wrsp = True tmpfile = 'xxxxx' separator = """\ //--------------------------------------------------------------------------- """ save_param_pageID = """\ web_reg_save_param("pageID", "LB=input type=\\"hidden\\" name=\\"pageID\\" value=\\"", "RB=\\"", LAST); """ #-------------------------------------------------------------------------- def process(filename): in_logo = False in_ITEMDATA = False in_Resource = False spacer = None block = None l_name = 0 l_value = 0 submit_cnt = 0 print filename ifd = open(filename, 'r') ofd = open(tmpfile, 'w') p = re.compile('( *)("Name=[^,]*") *, *("Value=[^,]*") *, *ENDITEM,(.*)') for line in ifd.readlines(): line = line[:-1] line = line.replace('\t', ' ') if re.search('test001.performiq.com.au', line): line = line.replace('test001.performiq.com.au', '{System}') if re.search('web_reg_find\("Text=app', line): ofd.write(separator) #----- Cull CSS request calls ----------------------------------------------- if strip_css: if re.search('web_concurrent_start', line): in_Resource = True continue if re.search('web_concurrent_end', line): in_Resource = False continue if in_Resource: continue #----- Cull logo request call ---------------------------------------------- if strip_logo: if re.search(r'web_url\("xxxx_logo.gif', line): in_logo = True continue if in_logo and re.search('LAST\)', line): in_logo = False continue if in_logo: continue #----- Drop in web_reg_save_param(pageID) block ----------------------------- if add_wrsp: if re.search('web_submit_data\("', line): submit_cnt += 1 if submit_cnt > 2: ofd.write("%s\n" % save_param_pageID) ofd.write("%s\n" % line) continue #----- Reformat ITEMDATA block ------------------------------------------- if re.search('ITEMDATA', line): block = [] l_name = 0 l_value = 0 spacer = '' if debug_flg: print 'ITEMDATA Start' in_ITEMDATA = True ofd.write("%s\n" % line) continue if in_ITEMDATA and re.search('LAST', line): if debug_flg: print 'ITEMDATA End =========================================' in_ITEMDATA = False if debug_flg: print 'len block = %d' % len(block) block_txt = '' for b in block: line = "%s%s,%s%s,%sENDITEM,%s\n" % ( spacer, b[1], (' '*(l_name - len(b[1]) + 2)), b[2], (' '*(l_value - len(b[2]) + 2)), b[3]) block_txt += line ofd.write("%s" % block_txt) ofd.write("%sLAST);\n" % spacer) if debug_flg: print 'ITEMDATA End =========================================' spacer = None continue if in_ITEMDATA: if debug_flg: print line m = p.search(line) if m: if debug_flg: print '[%s][%s][%s][%s]' % (m.group(1), m.group(2), m.group(3), m.group(4)) if not spacer: spacer = m.group(1) name = m.group(2) value = m.group(3) extra = m.group(4) if (submit_cnt > 3) and re.search('=pageID', name): value = '"Value={pageID}"' l = len(name) if l > l_name: l_name = l l = len(value) if l > l_value: l_value = l block.append((spacer, name, value, extra)) continue ofd.write("%s\n" % line) ifd.close() ofd.close() stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") os.rename(filename, 'bak/' + filename + '__' + stamp) os.rename(tmpfile, filename) #----- Main test ---------------------------------------------------------- def main(argv): global pp print argv if not os.path.exists('bak'): os.mkdir('bak') for script in argv: process(script) #-------------------------------------------------------------------------- if __name__ == "__main__": main(sys.argv[1:]) #--------------------------------------------------------------------------