0

I am using Coverity created by Synopsys, consisting primarily of static code analysis and dynamic code analysis tools. I am currently running this against my code and as a result it generates a .xml file.

The .xml houses strings of numbers that each of their own meaning. I would like to make a Python33 script to print the seventh number which directly maps to the complexity of a function. I would be fine printing it to the console but as I am a C coder and not only partial in Python here is what I have:

Line example of the *.xml:

    <fnmetric>
    <file>FILE1.C</file>
    <fnmet>function1;1;12;10;21;8;9;5;1441.75;0.0557199;;;318</fnmet>
    </fnmetric>
    <fnmetric>
    <file>FILE2.C</file>
    <fnmet>function2;0;0;1;11;8;3;1;184.638;0.0175846;;;352</fnmet>
    </fnmetric>

So the 7th number for the first function would be 9 and the second would be 3

So far I have:

import fileinput
import glob
import re
import sys

#Read in all files in the directory ending in .xml
files = glob.glob('*.xml')
#search for the line beginning in <fnmet> and ending in </fnmet>
pattern = re.compile(r'<fnmet>\s+;([_A-Z]+)</fnmet>\s+$') #this line is wrong
realstdout = sys.stdout
#create .bak files of the .h files unchanged (backups)
for line in fileinput.input(files, inplace=True, backup='.bak'):
    sys.stdout.write(line)
    m = pattern.match(line)
    #print the 7th #; as  function + complextity through all .xml lines. 
    if m:
        sys.stdout.write('\n <fnmet>\%s</fnmet>\n' % m.group(1)) #this line is wrong
        realstdout.write('%s: %s\n'%(fileinput.filename(),m.group(1))) #this line is wrong

Essentially I am just trying to print the 6th number after the function_name;

Something like:

function1: 9 function2: 3

Any help is appreciated, my python is horrendous.

1 Answers1

0

It looks like your trouble is with the regex pattern. Look at this sample:

import re

source= '<fnmet>function1;1;12;10;21;8;9;5;1441.75;0.0557199;;;318</fnmet>'

pattern = re.compile(r'(\<fnmet\>)(\S+)(\<\/fnmet\>)')

match = re.findall(pattern, source)
result = match[0][1].split(';')

# result now holds
# ['function1', '1', '12', '10', '21', '8', '9', '5',
#   '1441.75', '0.0557199', '', '', '318']

func_name = 0
pos = 6
print('{}: {}'.format(result[func_name], result[pos]))

This will output:

function1: 9

I think using this regex, together with string split will solve your problem easily, because you will be able to index the data in the columns.

progmatico
  • 4,714
  • 1
  • 16
  • 27