#!/usr/bin/python
#encoding:utf-8
import os
import csv
import re
import sys
import platform
import logging
import logging.handlers
from xml.etree import ElementTree
LOG = None
LOG_LEVEL = logging.DEBUG
class ColorFormatter(logging.Formatter):
def __init__(self, msg):
logging.Formatter.__init__(self, msg)
def format(self, record):
red, green, yellow, blue = range(4)
colors = {'INFO': green, 'DEBUG': blue,
'WARNING': yellow, 'ERROR': red}
msg = record.msg
if msg[0] == "+":
msg = "\33[01m" + msg[1:] + "\033[0m"
elif msg[0] == "=":
msg = "\33[07m" + msg + "\033[0m"
levelname = record.levelname
if levelname in colors:
msg_color = "\033[0;%dm" % (
31 + colors[levelname]) + msg + "\033[0m"
record.msg = msg_color
return logging.Formatter.format(self, record)
def csv2full(csv_path, split_sign):
if not os.path.isfile(csv_path):
print '%s is not a file' % csv_path
return
name, ext = os.path.splitext(csv_path)
if not ext == '.csv':
print '%s is not a csv' % csv_path
return
# print '== Convert csv to xml start=='
LOG.info("+Convert csv to xml start ...")
csv_file = file(csv_path, 'rb')
csv_file.readline()
reader = csv.reader(csv_file)
csv_content = []
for line in reader:
csv_content.append(line)
csv_file.close()
suite_name = csv_content[0][16].split('/')[2]
category_name = csv_content[0][12]
set_name = name.split(split_sign)[-1]
folder = os.path.dirname(csv_path)
# folder = os.getcwd()
full_test_path = '%s%stests.full(%s).xml' % (folder, split_sign, set_name)
make_full_test(csv_content, full_test_path, suite_name, set_name, category_name)
# print 'General %s' % full_test_path
LOG.info('General %s' % full_test_path)
test_path = '%s%stests(%s).xml' % (folder, split_sign, set_name)
make_test(csv_content, test_path, suite_name, set_name, category_name)
# print 'General %s' % test_path
LOG.info('General %s' % test_path)
# print '== Convert csv to xml finish=='
LOG.info("== Convert csv to xml finish==")
def make_full_test(csv_content, full_test_name, suite_name, set_name, category_name):
full_test_file = open(full_test_name, 'w')
content = '<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="./testcase.xsl"?>\n<test_definition>\n <suite name="%s" launcher="WRTLauncher" category="%s">\n <set name="%s">' % (suite_name, category_name, set_name)
for line in csv_content:
content += '\n <testcase purpose="%s" type="%s" status="%s" component="%s" execution_type="%s" priority="%s" id="%s">\n <description>\n <test_script_entry>%s</test_script_entry>\n </description>\n <specs>\n <spec>\n <spec_assertion element_type="%s" element_name="%s" interface="%s" specification="%s" section="%s" category="%s"/>\n <spec_url>%s</spec_url>\n <spec_statement/>\n </spec>\n </specs>\n </testcase>' % (line[1],
line[17],
line[15],
line[2],
line[4],
line[6],
line[0],
line[16],
line[7],
line[8],
line[9],
line[10],
line[11],
line[12],
line[13])
content += '\n </set>\n </suite>\n</test_definition>
#encoding:utf-8
import os
import csv
import re
import sys
import platform
import logging
import logging.handlers
from xml.etree import ElementTree
LOG = None
LOG_LEVEL = logging.DEBUG
class ColorFormatter(logging.Formatter):
def __init__(self, msg):
logging.Formatter.__init__(self, msg)
def format(self, record):
red, green, yellow, blue = range(4)
colors = {'INFO': green, 'DEBUG': blue,
'WARNING': yellow, 'ERROR': red}
msg = record.msg
if msg[0] == "+":
msg = "\33[01m" + msg[1:] + "\033[0m"
elif msg[0] == "=":
msg = "\33[07m" + msg + "\033[0m"
levelname = record.levelname
if levelname in colors:
msg_color = "\033[0;%dm" % (
31 + colors[levelname]) + msg + "\033[0m"
record.msg = msg_color
return logging.Formatter.format(self, record)
def csv2full(csv_path, split_sign):
if not os.path.isfile(csv_path):
print '%s is not a file' % csv_path
return
name, ext = os.path.splitext(csv_path)
if not ext == '.csv':
print '%s is not a csv' % csv_path
return
# print '== Convert csv to xml start=='
LOG.info("+Convert csv to xml start ...")
csv_file = file(csv_path, 'rb')
csv_file.readline()
reader = csv.reader(csv_file)
csv_content = []
for line in reader:
csv_content.append(line)
csv_file.close()
suite_name = csv_content[0][16].split('/')[2]
category_name = csv_content[0][12]
set_name = name.split(split_sign)[-1]
folder = os.path.dirname(csv_path)
# folder = os.getcwd()
full_test_path = '%s%stests.full(%s).xml' % (folder, split_sign, set_name)
make_full_test(csv_content, full_test_path, suite_name, set_name, category_name)
# print 'General %s' % full_test_path
LOG.info('General %s' % full_test_path)
test_path = '%s%stests(%s).xml' % (folder, split_sign, set_name)
make_test(csv_content, test_path, suite_name, set_name, category_name)
# print 'General %s' % test_path
LOG.info('General %s' % test_path)
# print '== Convert csv to xml finish=='
LOG.info("== Convert csv to xml finish==")
def make_full_test(csv_content, full_test_name, suite_name, set_name, category_name):
full_test_file = open(full_test_name, 'w')
content = '<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="./testcase.xsl"?>\n<test_definition>\n <suite name="%s" launcher="WRTLauncher" category="%s">\n <set name="%s">' % (suite_name, category_name, set_name)
for line in csv_content:
content += '\n <testcase purpose="%s" type="%s" status="%s" component="%s" execution_type="%s" priority="%s" id="%s">\n <description>\n <test_script_entry>%s</test_script_entry>\n </description>\n <specs>\n <spec>\n <spec_assertion element_type="%s" element_name="%s" interface="%s" specification="%s" section="%s" category="%s"/>\n <spec_url>%s</spec_url>\n <spec_statement/>\n </spec>\n </specs>\n </testcase>' % (line[1],
line[17],
line[15],
line[2],
line[4],
line[6],
line[0],
line[16],
line[7],
line[8],
line[9],
line[10],
line[11],
line[12],
line[13])
content += '\n </set>\n </suite>\n</test_definition>