Difference between revisions of "LR item data.py"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: <pre> #!/usr/bin/env python #-------------------------------------------------------------------------- import os import re import sys import getopt #------------------------------------...) |
PeterHarding (talk | contribs) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
<pre> | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
#-------------------------------------------------------------------------- | #------------------------------------------------------------------------------- | ||
import os | import os | ||
| Line 187: | Line 187: | ||
[[Category:Python]] | [[Category:Python]] | ||
[[Category:LoadRunner]] | [[Category:LoadRunner]] | ||
[[Category:Examples]] | |||
Latest revision as of 17:11, 19 July 2009
#!/usr/bin/env python
#-------------------------------------------------------------------------------
import os
import re
import sys
import getopt
#-------------------------------------------------------------------------------
from datetime import datetime
#-------------------------------------------------------------------------------
"""
Clean up LoadRuner script data blocks
"""
#-------------------------------------------------------------------------------
debug_level = 0
verbose_flg = False
src_file = None
tmp_file = None
#-------------------------------------------------------------------------------
def replace(action):
bak_file = 'bak/%s_%s.c' % (action, datetime.now().strftime('%Y%m%d%H%M%S'))
if not os.path.exists('bak'):
os.mkdir('bak')
os.rename(src_file, bak_file)
os.rename(tmp_file, src_file)
#-------------------------------------------------------------------------------
def reformat(action):
global src_file
global tmp_file
spacer = None
block = None
l_name = 0
l_value = 0
in_ITEMDATA = False
src_file = '%s.c' % action
tmp_file = '%s.new' % action
try:
f_in = open(src_file, 'r')
except:
sys.exit(1)
try:
f_out = open(tmp_file, 'w')
except:
sys.exit(1)
p = re.compile('( *)("Name=[^"]*") *, *("Value=[^"]*") *, *ENDITEM,(.*)')
for line in f_in.readlines():
line = line[:-1]
line = line.replace('\t', ' ')
if re.search('ITEMDATA', line):
block = []
l_name = 0
l_value = 0
spacer = ''
if debug_level > 0: print 'ITEMDATA Start'
in_ITEMDATA = True
f_out.write("%s\n" % line)
continue
if in_ITEMDATA and re.search('LAST', line):
if debug_level > 0: print 'ITEMDATA End ========================================='
in_ITEMDATA = False
if debug_level > 0: 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
f_out.write("%s" % block_txt)
f_out.write("%sLAST);\n" % spacer)
if debug_level > 0: print 'ITEMDATA End ========================================='
spacer = None
continue
if in_ITEMDATA:
if debug_level > 0: print line
m = p.search(line)
if m:
if debug_level > 0: 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)
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
f_out.write("%s\n" % line)
f_in.close()
f_out.close()
#-------------------------------------------------------------------------------
def usage():
USAGE = """
Usage:
$ lr__item_data.py -a Action
"""
sys.stderr.write(USAGE)
#-------------------------------------------------------------------------------
def main(argv):
global debug_flg, verbose_flg
action = None
replace_flg = False
#----- Process command line arguments ---------------------------------------
try:
opts, args = getopt.getopt(argv, "a:dhf:rs:v",
["action=", "debug", "help", "file=", "replace", "script=", "verbose"])
except getopt.GetoptError:
usage()
sys.exit(2)
else:
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(0)
elif opt in ("-a", "--action"):
action = arg
elif opt in ("-f", "--file"):
action = arg
elif opt in ("-r", "--replace"):
replace_flg = True
elif opt in ("-s", "--script"):
action = arg
elif opt in ("-v", "--verbose"):
verbose_flg = True
reformat(action)
if replace_flg:
replace(action)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
main(sys.argv[1:])
#-------------------------------------------------------------------------------