55import logging
66import os
77from xml .dom import minidom
8-
98import requests
109
1110logging .basicConfig (level = logging .INFO ,
2019def get_git_revision_hash ():
2120 import subprocess
2221
23- return subprocess .check_output (['git' , 'rev-parse' , 'HEAD' ]).strip ()
22+ return subprocess .check_output (['git' , 'rev-parse' , 'HEAD' ]).decode ("utf-8" ).strip ()
23+
24+
25+ def get_git_directory ():
26+ import subprocess
27+
28+ return subprocess .check_output (['git' , 'rev-parse' , '--show-toplevel' ]).decode ("utf-8" ).strip ()
29+
30+
31+ def file_exists (rootdir , filename ):
32+ for root , subFolders , files in os .walk (rootdir ):
33+ if filename in files :
34+ return True
35+ else :
36+ for subFolder in subFolders :
37+ return file_exists (subFolder , filename )
38+ return False
39+
40+
41+ def generate_filename (sources , filename ):
42+ def strip_prefix (line , prefix ):
43+ if line .startswith (prefix ):
44+ return line [len (prefix ):]
45+ else :
46+ return line
47+
48+ git_directory = get_git_directory ()
49+
50+ for source in sources :
51+ if file_exists (source , filename ):
52+ return strip_prefix (source , git_directory ).strip ("/" ) + "/" + filename .strip ("/" )
53+
54+ return filename
2455
2556
2657def parse_report_file (report_file ):
27- """Parse XML file and POST it to the Codacy API"""
58+ """Parse XML file and POST it to the Codacy API
59+ :param report_file:
60+ """
2861
2962 # Convert decimal string to floored int percent value
3063 def percent (s ):
@@ -39,10 +72,11 @@ def percent(s):
3972 'fileReports' : [],
4073 }
4174
75+ sources = [x .firstChild .nodeValue for x in report_xml .getElementsByTagName ('source' )]
4276 classes = report_xml .getElementsByTagName ('class' )
4377 for cls in classes :
4478 file_report = {
45- 'filename' : cls .attributes ['filename' ].value ,
79+ 'filename' : generate_filename ( sources , cls .attributes ['filename' ].value ) ,
4680 'total' : percent (cls .attributes ['line-rate' ].value ),
4781 'coverage' : {},
4882 }
0 commit comments