diff options
author | rnefyodov <rnefyodov@yandex-team.ru> | 2022-02-10 16:47:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:17 +0300 |
commit | c22320e8c4f3d7be38c504706f137034e91d31e6 (patch) | |
tree | 35cc6c382a1bd8cb932449ffa734056fcc73e6d4 /build/scripts/coverage-info.py | |
parent | 93e9e4639b6ee2afbdf45cf3927cea6d340e19b0 (diff) | |
download | ydb-c22320e8c4f3d7be38c504706f137034e91d31e6.tar.gz |
Restoring authorship annotation for <rnefyodov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'build/scripts/coverage-info.py')
-rw-r--r-- | build/scripts/coverage-info.py | 242 |
1 files changed, 121 insertions, 121 deletions
diff --git a/build/scripts/coverage-info.py b/build/scripts/coverage-info.py index 94491d9256..cca68ac27f 100644 --- a/build/scripts/coverage-info.py +++ b/build/scripts/coverage-info.py @@ -1,17 +1,17 @@ -import argparse -import os -import sys -import tarfile -import collections -import subprocess +import argparse +import os +import sys +import tarfile +import collections +import subprocess import re - - -GCDA_EXT = '.gcda' -GCNO_EXT = '.gcno' - - -def suffixes(path): + + +GCDA_EXT = '.gcda' +GCNO_EXT = '.gcno' + + +def suffixes(path): """ >>> list(suffixes('/a/b/c')) ['c', 'b/c', '/a/b/c'] @@ -24,67 +24,67 @@ def suffixes(path): >>> list(suffixes('/')) [] """ - path = os.path.normpath(path) - - def up_dirs(cur_path): - while os.path.dirname(cur_path) != cur_path: - cur_path = os.path.dirname(cur_path) - yield cur_path - - for x in up_dirs(path): - yield path.replace(x + os.path.sep, '') - - -def recast(in_file, out_file, probe_path, update_stat): - PREFIX = 'SF:' - - probed_path = None - - any_payload = False - - with open(in_file, 'r') as input, open(out_file, 'w') as output: - active = True - for line in input: - line = line.rstrip('\n') - if line.startswith('TN:'): - output.write(line + '\n') - elif line.startswith(PREFIX): - path = line[len(PREFIX):] - probed_path = probe_path(path) - if probed_path: - output.write(PREFIX + probed_path + '\n') - active = bool(probed_path) - else: - if active: - update_stat(probed_path, line) - output.write(line + '\n') - any_payload = True - - return any_payload - - -def print_stat(da, fnda, teamcity_stat_output): - lines_hit = sum(map(bool, da.values())) - lines_total = len(da.values()) - lines_coverage = 100.0 * lines_hit / lines_total if lines_total else 0 - - func_hit = sum(map(bool, fnda.values())) - func_total = len(fnda.values()) - func_coverage = 100.0 * func_hit / func_total if func_total else 0 - - print >>sys.stderr, '[[imp]]Lines[[rst]] {: >16} {: >16} {: >16.1f}%'.format(lines_hit, lines_total, lines_coverage) - print >>sys.stderr, '[[imp]]Functions[[rst]] {: >16} {: >16} {: >16.1f}%'.format(func_hit, func_total, func_coverage) - - if teamcity_stat_output: - with open(teamcity_stat_output, 'w') as tc_file: - tc_file.write("##teamcity[blockOpened name='Code Coverage Summary']\n") - tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsLTotal\' value='{}']\n".format(lines_total)) - tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsLCovered\' value='{}']\n".format(lines_hit)) - tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsMTotal\' value='{}']\n".format(func_total)) - tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsMCovered\' value='{}']\n".format(func_hit)) - tc_file.write("##teamcity[blockClosed name='Code Coverage Summary']\n") - - + path = os.path.normpath(path) + + def up_dirs(cur_path): + while os.path.dirname(cur_path) != cur_path: + cur_path = os.path.dirname(cur_path) + yield cur_path + + for x in up_dirs(path): + yield path.replace(x + os.path.sep, '') + + +def recast(in_file, out_file, probe_path, update_stat): + PREFIX = 'SF:' + + probed_path = None + + any_payload = False + + with open(in_file, 'r') as input, open(out_file, 'w') as output: + active = True + for line in input: + line = line.rstrip('\n') + if line.startswith('TN:'): + output.write(line + '\n') + elif line.startswith(PREFIX): + path = line[len(PREFIX):] + probed_path = probe_path(path) + if probed_path: + output.write(PREFIX + probed_path + '\n') + active = bool(probed_path) + else: + if active: + update_stat(probed_path, line) + output.write(line + '\n') + any_payload = True + + return any_payload + + +def print_stat(da, fnda, teamcity_stat_output): + lines_hit = sum(map(bool, da.values())) + lines_total = len(da.values()) + lines_coverage = 100.0 * lines_hit / lines_total if lines_total else 0 + + func_hit = sum(map(bool, fnda.values())) + func_total = len(fnda.values()) + func_coverage = 100.0 * func_hit / func_total if func_total else 0 + + print >>sys.stderr, '[[imp]]Lines[[rst]] {: >16} {: >16} {: >16.1f}%'.format(lines_hit, lines_total, lines_coverage) + print >>sys.stderr, '[[imp]]Functions[[rst]] {: >16} {: >16} {: >16.1f}%'.format(func_hit, func_total, func_coverage) + + if teamcity_stat_output: + with open(teamcity_stat_output, 'w') as tc_file: + tc_file.write("##teamcity[blockOpened name='Code Coverage Summary']\n") + tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsLTotal\' value='{}']\n".format(lines_total)) + tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsLCovered\' value='{}']\n".format(lines_hit)) + tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsMTotal\' value='{}']\n".format(func_total)) + tc_file.write("##teamcity[buildStatisticValue key=\'CodeCoverageAbsMCovered\' value='{}']\n".format(func_hit)) + tc_file.write("##teamcity[blockClosed name='Code Coverage Summary']\n") + + def chunks(l, n): """ >>> list(chunks(range(10), 3)) @@ -118,17 +118,17 @@ def combine_info_files(lcov, files, out_file): def probe_path_global(path, source_root, prefix_filter, exclude_files): if path.endswith('_ut.cpp'): - return None - + return None + for suff in reversed(list(suffixes(path))): if (not prefix_filter or suff.startswith(prefix_filter)) and (not exclude_files or not exclude_files.match(suff)): full_path = source_root + os.sep + suff if os.path.isfile(full_path): return full_path - + return None - - + + def update_stat_global(src_file, line, fnda, da): if line.startswith("FNDA:"): visits, func_name = line[len("FNDA:"):].split(',') @@ -179,27 +179,27 @@ def init_all_coverage_files(gcno_archive, fname2gcno, fname2info, geninfo_execut def process_all_coverage_files(gcda_archive, fname2gcno, fname2info, geninfo_executable, gcov_tool, gen_info): - with tarfile.open(gcda_archive) as gcda_tf: - for gcda_item in gcda_tf: - if gcda_item.isfile() and gcda_item.name.endswith(GCDA_EXT): - gcda_name = gcda_item.name - source_fname = gcda_name[:-len(GCDA_EXT)] - for suff in suffixes(source_fname): - if suff in fname2gcno: + with tarfile.open(gcda_archive) as gcda_tf: + for gcda_item in gcda_tf: + if gcda_item.isfile() and gcda_item.name.endswith(GCDA_EXT): + gcda_name = gcda_item.name + source_fname = gcda_name[:-len(GCDA_EXT)] + for suff in suffixes(source_fname): + if suff in fname2gcno: gcda_new_name = suff + GCDA_EXT - gcda_item.name = gcda_new_name - gcda_tf.extract(gcda_item) - if os.path.getsize(gcda_new_name) > 0: + gcda_item.name = gcda_new_name + gcda_tf.extract(gcda_item) + if os.path.getsize(gcda_new_name) > 0: coverage_info = suff + '.' + str(len(fname2info[suff])) + '.info' fname2info[suff].append(coverage_info) - geninfo_cmd = [ + geninfo_cmd = [ geninfo_executable, - '--gcov-tool', gcov_tool, - gcda_new_name, + '--gcov-tool', gcov_tool, + gcda_new_name, '-o', coverage_info + '.tmp' - ] + ] gen_info(geninfo_cmd, coverage_info) - + def gen_cobertura(tool, output, combined_info): cmd = [ @@ -241,42 +241,42 @@ def main(source_root, output, gcno_archive, gcda_archive, gcov_tool, prefix_filt output_dir = coverage_report_path else: output_dir = output + '.dir' - + if not os.path.exists(output_dir): os.makedirs(output_dir) - teamcity_stat_file = None - if teamcity_stat_output: - teamcity_stat_file = os.path.join(output_dir, 'teamcity.out') - print_stat(da, fnda, teamcity_stat_file) - - if lcov_args: + teamcity_stat_file = None + if teamcity_stat_output: + teamcity_stat_file = os.path.join(output_dir, 'teamcity.out') + print_stat(da, fnda, teamcity_stat_file) + + if lcov_args: output_trace = "combined.info" combine_info_files(os.path.join(source_root, 'devtools', 'lcov', 'lcov'), lcov_args, output_trace) cmd = [os.path.join(source_root, 'devtools', 'lcov', 'genhtml'), '-p', source_root, '--ignore-errors', 'source', '-o', output_dir, output_trace] - print >>sys.stderr, '## genhtml', ' '.join(cmd) - subprocess.check_call(cmd) + print >>sys.stderr, '## genhtml', ' '.join(cmd) + subprocess.check_call(cmd) if lcov_cobertura: gen_cobertura(lcov_cobertura, gcov_report, output_trace) - - with tarfile.open(output, 'w') as tar: - tar.add(output_dir, arcname='.') - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - - parser.add_argument('--source-root', action='store') - parser.add_argument('--output', action='store') - parser.add_argument('--gcno-archive', action='store') - parser.add_argument('--gcda-archive', action='store') - parser.add_argument('--gcov-tool', action='store') - parser.add_argument('--prefix-filter', action='store') + + with tarfile.open(output, 'w') as tar: + tar.add(output_dir, arcname='.') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + + parser.add_argument('--source-root', action='store') + parser.add_argument('--output', action='store') + parser.add_argument('--gcno-archive', action='store') + parser.add_argument('--gcda-archive', action='store') + parser.add_argument('--gcov-tool', action='store') + parser.add_argument('--prefix-filter', action='store') parser.add_argument('--exclude-regexp', action='store') - parser.add_argument('--teamcity-stat-output', action='store_const', const=True) + parser.add_argument('--teamcity-stat-output', action='store_const', const=True) parser.add_argument('--coverage-report-path', action='store') parser.add_argument('--gcov-report', action='store') parser.add_argument('--lcov-cobertura', action='store') - - args = parser.parse_args() - main(**vars(args)) + + args = parser.parse_args() + main(**vars(args)) |