blob: 460618fb30bbeef9f37478238f05dd6ad9783ace [file] [log] [blame]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
import json
import re
import sys
import logging
import argparse
col_tmd = 'TMD File'
col_regex = 'regex to match Trace Class'
col_tag = 'Expected Tag'
def readUtmdToJson(utmd_filename):
with open(utmd_filename, 'r') as utmdfile:
j = json.load(utmdfile)
utmdfile.close()
return j
def writeJsonToUtmd(j, utmd_filename):
with open(utmd_filename, 'w') as utmdfile:
utmdfile.write(json.dumps(j,
sort_keys=True,
ensure_ascii=True,
indent=2))
utmdfile.close()
def updateUtmd(utmd_filename, csv_filename):
if utmd_filename.startswith('./'):
utmd_filename = utmd_filename[2::]
csvfile = open(csv_filename, 'r')
j = readUtmdToJson(utmd_filename)
if j is None:
logging.error('read UTMD failed')
sys.exit()
if j['traceFamily'] == 'PS':
logging.warning('{}-{} not L1 / L2 UTMD'.format(j['module'], j['traceFamily']))
sys.exit()
fieldnames = (col_tmd, col_regex, col_tag)
reader = csv.DictReader( csvfile, fieldnames)
logging.info('Module: {} - {}'.format(j['module'], utmd_filename))
debug_UH = 'Ultra-High'
debug_H = 'High'
debug_M = 'Medium'
debug_L = 'Low'
debug_UL = 'Ultra-Low'
#update debugLevel
for traceClass in j['traceClassDefs']:
for k, v in traceClass.iteritems():
if k.endswith('_UH'):
v['debugLevel'] = debug_UH
elif k.endswith('_H'):
v['debugLevel'] = debug_H
elif k.endswith('_M'):
v['debugLevel'] = debug_M
elif k.endswith('_L'):
v['debugLevel'] = debug_L
elif k.endswith('_UL'):
v['debugLevel'] = debug_UL
#update tag
for row in reader:
if row[col_tmd] in utmd_filename:
logging.info(row)
for traceClass in j['traceClassDefs']:
for k, v in traceClass.iteritems():
logging.info('Original: {} {}'.format(k, v))
#default mapping for entire TMD
if row[col_regex] == 'N/A':
logging.info('Matching N/A: {}'.format(k))
logging.info(k)
logging.info(v)
v['tag'] = [ row[col_tag] ]
#regex to match trace class name
else:
logging.info('Matching regex: {} {}'.format(k, row[col_regex]))
tag = re.sub(row[col_regex], row[col_tag], k)
if tag:
v['tag'] = [ tag ]
logging.info('Changed: {} {}'.format(k, v))
writeJsonToUtmd(j, utmd_filename)
def init_logger(log_filename):
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M:%S',
filename=log_filename)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def main():
parser = argparse.ArgumentParser(description='update-l1-utmd:\n\
Update tag of trace class',
#formatter_class=argparse.ArgumentDefaultsHelpFormatter)
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-v", action="version", version='1.0.0')
parser.add_argument("utmd_file",
help="input L1 UTMD file")
parser.add_argument("csv_file",
help="input CSV file(survey table)")
parser.add_argument("-l", dest="log_file",
help="log file",
default='update-l1-utmd.log',
action="store")
args = parser.parse_args()
if args.utmd_file is None:
parser.print_help()
quit()
init_logger(args.log_file)
updateUtmd(args.utmd_file, args.csv_file)
if __name__ == '__main__':
main()