forked from CppMicroServices/CppMicroServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixcoveragefilepaths.py
72 lines (65 loc) · 2.91 KB
/
fixcoveragefilepaths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# The Cobertura report generated by OpenCooCpverage tool has file paths relative
# to the drive and in case-insensitive format. This script converts the file
# paths to be relative to the build directory and case-sensitive, which is
# required for uploading reports to codecov.io
import xml.etree.ElementTree as xml
import glob, os, re, sys, getopt
#----------- Remove prefix from a string -----------
def remove_prefix(line, prefix):
if line.startswith(prefix):
return line[len(prefix):]
else:
return line
#----------- Convert case-insensitive path to case-sensitive path -----------
def casedpath_unc(path):
unc, p = os.path.splitunc(path)
r = glob.glob(unc + re.sub(r'([^:/\\])(?=[/\\]|$)', r'[\1]', p))
return r and r[0] or path
#----------------------------------------------------------------------
def modifyXML(infile, sourcepath, outfile):
tree = xml.ElementTree(file=infile)
root = tree.getroot()
source_dir = root.find('sources/source')
actual_src_drive, actual_src_tail = os.path.splitdrive(sourcepath)
src_drive, src_tail = os.path.splitdrive(source_dir.text)
if actual_src_drive.lower() == src_drive.lower():
# Replace the filename with the case sensitive path
# The result filename is relative to the provided source path
for class_elem in root.iter("class"):
filepath = "%s\%s" % (source_dir.text,class_elem.attrib['filename'])
case_sensitive_filepath = casedpath_unc(filepath)
rel_filepath = remove_prefix(case_sensitive_filepath, src_drive+actual_src_tail+"\\")
class_elem.attrib['filename'] = rel_filepath
class_elem.attrib['name'] = os.path.basename(rel_filepath)
source_dir.text = sourcepath
tree = xml.ElementTree(root)
tree.write(outfile, encoding="utf-8",xml_declaration=True)
else:
print("Error: Source drive in input file does not match the provided source path's drive")
sys.exit(-1)
def main(argv):
inputfile = ''
outputfile = ''
sourcedir = ''
try:
opts, args = getopt.getopt(argv,"hi:o:s:",["ifile=","ofile=","sdir="])
except getopt.GetoptError:
print('fixcoveragepaths.py -i <inputfile> -s <sourcedirectory> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('fixcoveragepaths.py -i <inputfile> -s <sourcedirectory> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-s", "--sdir"):
sourcedir = arg
#print('Input file is ', inputfile)
#print ('Output file is ', outputfile)
#print ('Sources directory is ', sourcedir)
modifyXML(inputfile, sourcedir, outputfile)
#----------------------------------------------------------------------
if __name__ == "__main__":
main(sys.argv[1:])