Skip to content

Commit

Permalink
[action] add missing python script
Browse files Browse the repository at this point in the history
This patch adds missing python script gen_coverage_badge.py

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Jan 3, 2025
1 parent 78cd75c commit 7a65e47
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/daily-build-gbs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: gbs daily build and test

on:
push:
schedule:
# 05:00 AM (KST) Mon-Fri
- cron: "00 20 * * 0-4"
Expand Down
79 changes: 79 additions & 0 deletions .github/workflows/gen_coverage_badge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
##
# SPDX-License-Identifier: LGPL-2.1-only
# Copyright (C) 2024 Samsung Electronics
#
# @file gen_github_badge_svg.py
# @brief A tool for generating a github badge image in svg format
# @author Wook Song <[email protected]>
# @author Gichan Jang <[email protected]>
# @note
# usage : python gen_badge.py {gcov-index-file} {output.svg}
#
import sys
import os
from bs4 import BeautifulSoup
from pybadges import badge

##
# @brief Get a gradient colorcode from green to red of given value (scaled)
# @param[in] val A value to be conveted to a gradient colorcode (i.e., #FFFFFF)
# @param[in] scale A limit for the val, 0 <= val <= scale
def get_code_g_y_r(val, scale):
"""get_code_g_y_r(val, scale) -> str"""
if val <= 50:
red = 255
green = val * (255 / (float(scale) / 2))
else:
green = 255 * val / scale
red = 255 - (val - 50) * (255 / (float(scale) / 2))

rgb = (int(red), int(green), int(0))

return '#%02x%02x%02x' % rgb

##
# @brief Generate a github badge svg file representing code coverage
# @param[in] html A concatenated string of the whole contents in index.html that is the result of LCOV
# @param[in] path A file path to save the svg file
def gen_coverage_badge(html, path):
#parse LCOV html
soup = BeautifulSoup(html, 'html.parser')
lines, line_hits, funcs, func_hits = \
soup.find('table').find_all('td', {'class': 'headerCovTableEntry'})
line_hits = float(line_hits.text)
lines = float(lines.text)
line_coverage = line_hits / lines
rgb_code = get_code_g_y_r(line_coverage * 100, 100)
coverage_str = str(format(line_coverage * 100, '.2f')) + '%'
s = badge(left_text='coverage', right_text=coverage_str, right_color=rgb_code)

file = open(path, "w")
file.write(s)
file.close()


if __name__ == '__main__':
# argv[1]: [url/file] a path or url of LCOV html to get information for badge generation
# argv[2]: [file] a file path to save the generated svg file
if len(sys.argv) < 3:
exit(1)

str_html = ''
if os.path.isfile(sys.argv[1]):
with open(sys.argv[1], 'r') as f:
str_html = f.read()
if not BeautifulSoup(str_html, "html.parser").find():
exit(1)
else:
exit(1)

path_out_svg=''
if not os.access(os.path.dirname(sys.argv[2]) or os.getcwd(), os.W_OK):
exit(1)
else:
path_out_svg = os.path.abspath(sys.argv[2])
if os.path.isdir(path_out_svg) or os.path.islink(path_out_svg):
exit(1)

gen_coverage_badge(str_html, path_out_svg)

0 comments on commit 7a65e47

Please sign in to comment.