Last active
December 17, 2015 14:29
-
-
Save kjdev/5624363 to your computer and use it in GitHub Desktop.
POST coveralls by gcov
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() | |
{ | |
echo "Usage: "${0##*/}" [-s FILES] [-t TOKEN] [-g DIR] [-h] [-v]" | |
echo "" | |
echo "Options:" | |
echo " s: source files" | |
echo " t: secret repo token for your repository" | |
echo " g: search gcov/gcda file path" | |
echo " h: show this help message" | |
echo " v: print verbose message" | |
exit 0 | |
} | |
url="https://coveralls.io/api/v1/jobs" | |
verbose= | |
if [ ! "x${TRAVIS}" = "x" ]; then | |
service_name="travis-ci" | |
service_job_id=${TRAVIS_JOB_ID} | |
fi | |
while getopts "s:t:g:hv" opt | |
do | |
case "${opt}" in | |
"s") | |
source_files=(`echo "${OPTARG}" | tr -s ',' ' '`) | |
;; | |
"t") | |
repo_token="${OPTARG}" | |
;; | |
"g") | |
gcov_dir="${OPTARG}" | |
;; | |
"v") | |
verbose=1 | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((${OPTIND}-1)) | |
if [ "x${source_files}" = "x" -o ${#source_files[@]} -eq 0 ]; then | |
usage | |
fi | |
which gcov > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo "ERR: command not found: gcov" | |
exit 1 | |
fi | |
which gawk > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo "ERR: command not found: gawk" | |
exit 1 | |
fi | |
if [ "x${repo_token}" = "x" -a "x${service_name}" = "x" ]; then | |
echo "ERR: repo token or service name" | |
exit 1 | |
fi | |
srcdir=`pwd` | |
json_file=`mktemp ${srcdir}/coveralls.XXXXX` || exit 1 | |
trap "rm -f ${json_file}" EXIT | |
echo -n '{' >> ${json_file} | |
# service_name | |
if [ ! "x${service_name}" = "x" ]; then | |
echo -n '"service_name":"'"${service_name}"'",' >> ${json_file} | |
if [ ! "x${service_job_id}" = "x" ]; then | |
echo -n '"service_job_id":"'"${service_job_id}"'",' >> ${json_file} | |
fi | |
fi | |
# repo_token | |
if [ ! "x${repo_token}" = "x" ]; then | |
echo -n '"repo_token":"'"${repo_token}"'",' >> ${json_file} | |
fi | |
# git | |
which git > /dev/null 2>&1 | |
if [ $? -eq 0 -a -d ${srcdir}/.git ]; then | |
echo -n '"git":' >> ${json_file} | |
echo -n '{"head":{"id":"'`git log -1 --pretty=format:'%H'`'",' >> ${json_file} | |
echo -n '"author_name":"'`git log -1 --pretty=format:'%aN'`'",' >> ${json_file} | |
echo -n '"author_email":"'`git log -1 --pretty=format:'%ae'`'",' >> ${json_file} | |
echo -n '"committer_name":"'`git log -1 --pretty=format:'%cN'`'",' >> ${json_file} | |
echo -n '"committer_email":"'`git log -1 --pretty=format:'%ce'`'",' >> ${json_file} | |
echo -n '"message":"'`git log -1 --pretty=format:'%s'`'"},' >> ${json_file} | |
echo -n '"branch":"'`git rev-parse --abbrev-ref HEAD`'",' >> ${json_file} | |
echo -n '"remotes":[]},' >> ${json_file} | |
fi | |
# source_files | |
echo -n '"source_files":[' >> ${json_file} | |
source_comma=0 | |
for source_file in ${source_files[@]} | |
do | |
gcov_file=${srcdir}/${gcov_dir}/${source_file}.gcov | |
gcov_path="${srcdir}/"`dirname ${source_file}`"/${gcov_dir}" | |
if [ ! -f ${gcov_file} ]; then | |
for gcda_file in `ls -1 ${gcov_path}/*.gcda 2> /dev/null` | |
do | |
if [ ! "x${verbose}" = "x" ]; then | |
(cd `dirname ${gcda_file}` && gcov `basename ${gcda_file}`) | |
else | |
(cd `dirname ${gcda_file}` && gcov `basename ${gcda_file}` > /dev/null) | |
fi | |
done | |
fi | |
if [ ! -f ${gcov_file} ]; then | |
echo "ERR: ${gcov_file}" | |
continue | |
fi | |
if [ ${source_comma} -ne 0 ]; then | |
echo -n ',' >> ${json_file} | |
fi | |
# source_files: name | |
echo -n '{"name":"'"${source_file}"'",' >> ${json_file} | |
# source_files: source | |
echo -n '"source":"' >> ${json_file} | |
gawk -F \n -v ORS='\\n' '{gsub(/\\/,"\\\\")} {gsub(/"/,"\\\"")} {print}' ${srcdir}/${source_file} >> ${json_file} | |
echo -n '",' >> ${json_file} | |
# source_files: coverage | |
echo -n '"coverage":[' >> ${json_file} | |
coverage_comma=0 | |
while read line | |
do | |
elements=(`echo "${line}" | tr -s ':' ' '`) | |
if [ ${#elements[@]} -lt 2 ]; then | |
continue | |
fi | |
if [ ${elements[1]} -gt 0 ]; then | |
if [ ${coverage_comma} -ne 0 ]; then | |
echo -n ',' >> ${json_file} | |
fi | |
case "${elements[0]}" in | |
"-") | |
echo -n 'null' >> ${json_file} | |
;; | |
"#####") | |
echo -n '0' >> ${json_file} | |
;; | |
*) | |
echo -n "${elements[0]}" >> ${json_file} | |
;; | |
esac | |
coverage_comma=1 | |
fi | |
done < ${gcov_file} | |
echo -n ']}' >> ${json_file} | |
source_comma=1 | |
done | |
echo -n ']}' >> ${json_file} | |
if [ ${source_comma} -eq 0 ]; then | |
echo "ERR: unknown coverage files" | |
exit 1 | |
fi | |
if [ ! "x${verbose}" = "x" ]; then | |
cat ${json_file} | |
echo | |
fi | |
# POST coveralls | |
if [ ! "x${url}" = "x" ] ; then | |
if [ ! "x${verbose}" = "x" ]; then | |
curl -v -k -F "json_file=@${json_file}" -F "Filename=json_file" ${url} | |
else | |
curl -k -F "json_file=@${json_file}" -F "Filename=json_file" ${url} > /dev/null 2>&1 | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment