1*11be35a1SLionel Sambuc#! __SH__ 2*11be35a1SLionel Sambuc# Copyright 2011 Google Inc. 3*11be35a1SLionel Sambuc# All rights reserved. 4*11be35a1SLionel Sambuc# 5*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without 6*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions are 7*11be35a1SLionel Sambuc# met: 8*11be35a1SLionel Sambuc# 9*11be35a1SLionel Sambuc# * Redistributions of source code must retain the above copyright 10*11be35a1SLionel Sambuc# notice, this list of conditions and the following disclaimer. 11*11be35a1SLionel Sambuc# * Redistributions in binary form must reproduce the above copyright 12*11be35a1SLionel Sambuc# notice, this list of conditions and the following disclaimer in the 13*11be35a1SLionel Sambuc# documentation and/or other materials provided with the distribution. 14*11be35a1SLionel Sambuc# * Neither the name of Google Inc. nor the names of its contributors 15*11be35a1SLionel Sambuc# may be used to endorse or promote products derived from this software 16*11be35a1SLionel Sambuc# without specific prior written permission. 17*11be35a1SLionel Sambuc# 18*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19*11be35a1SLionel Sambuc# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20*11be35a1SLionel Sambuc# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21*11be35a1SLionel Sambuc# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22*11be35a1SLionel Sambuc# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23*11be35a1SLionel Sambuc# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24*11be35a1SLionel Sambuc# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25*11be35a1SLionel Sambuc# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26*11be35a1SLionel Sambuc# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27*11be35a1SLionel Sambuc# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28*11be35a1SLionel Sambuc# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29*11be35a1SLionel Sambuc 30*11be35a1SLionel Sambuc# \file atf2kyua.sh 31*11be35a1SLionel Sambuc# Converts Atffiles to Kyuafiles for a particular test suite. 32*11be35a1SLionel Sambuc 33*11be35a1SLionel Sambuc 34*11be35a1SLionel Sambuc. "${KYUA_ATF_COMPAT_PKGDATADIR:-__PKGDATADIR__}/lib.subr" 35*11be35a1SLionel Sambuc 36*11be35a1SLionel Sambuc 37*11be35a1SLionel Sambuc# Prunes all Kyuafiles from a test suite in preparation for regeneration. 38*11be35a1SLionel Sambuc# 39*11be35a1SLionel Sambuc# \param target_root The path to the test suite. 40*11be35a1SLionel Sambucremove_kyuafiles() { 41*11be35a1SLionel Sambuc local target_root="${1}"; shift 42*11be35a1SLionel Sambuc 43*11be35a1SLionel Sambuc if [ -d "${target_root}" ]; then 44*11be35a1SLionel Sambuc lib_info "Removing stale Kyuafiles from ${target_root}" 45*11be35a1SLionel Sambuc find "${target_root}" -name Kyuafile -exec rm -f {} \; 46*11be35a1SLionel Sambuc fi 47*11be35a1SLionel Sambuc} 48*11be35a1SLionel Sambuc 49*11be35a1SLionel Sambuc 50*11be35a1SLionel Sambuc# Obtains the list of test programs and subdirectories referenced by an Atffile. 51*11be35a1SLionel Sambuc# 52*11be35a1SLionel Sambuc# Any globs within the Atffile are expanded relative to the directory in which 53*11be35a1SLionel Sambuc# the Atffile lives. 54*11be35a1SLionel Sambuc# 55*11be35a1SLionel Sambuc# \param atffile The path to the Atffile to process. 56*11be35a1SLionel Sambuc# 57*11be35a1SLionel Sambuc# \post Prints the list of files referenced by the Atffile on stdout. 58*11be35a1SLionel Sambucextract_files() { 59*11be35a1SLionel Sambuc local atffile="${1}"; shift 60*11be35a1SLionel Sambuc 61*11be35a1SLionel Sambuc local dir="$(dirname "${atffile}")" 62*11be35a1SLionel Sambuc 63*11be35a1SLionel Sambuc local globs="$(grep '^tp-glob:' "${atffile}" | cut -d ' ' -f 2-)" 64*11be35a1SLionel Sambuc local files="$(grep '^tp:' "${atffile}" | cut -d ' ' -f 2-)" 65*11be35a1SLionel Sambuc 66*11be35a1SLionel Sambuc for file in ${files} $(cd "$(dirname "${atffile}")" && echo ${globs}); do 67*11be35a1SLionel Sambuc if test -d "${dir}/${file}" -o -x "${dir}/${file}"; then 68*11be35a1SLionel Sambuc echo "${file}" 69*11be35a1SLionel Sambuc fi 70*11be35a1SLionel Sambuc done 71*11be35a1SLionel Sambuc} 72*11be35a1SLionel Sambuc 73*11be35a1SLionel Sambuc 74*11be35a1SLionel Sambuc# Converts an Atffile to a Kyuafile. 75*11be35a1SLionel Sambuc# 76*11be35a1SLionel Sambuc# \param atffile The path to the Atfffile to convert. 77*11be35a1SLionel Sambuc# \param kyuafile The path to where the Kyuafile will be written. 78*11be35a1SLionel Sambucconvert_atffile() { 79*11be35a1SLionel Sambuc local atffile="${1}"; shift 80*11be35a1SLionel Sambuc local kyuafile="${1}"; shift 81*11be35a1SLionel Sambuc 82*11be35a1SLionel Sambuc lib_info "Converting ${atffile} -> ${kyuafile}" 83*11be35a1SLionel Sambuc 84*11be35a1SLionel Sambuc local test_suite="$(grep 'prop:.*test-suite.*' "${atffile}" \ 85*11be35a1SLionel Sambuc | cut -d \" -f 2)" 86*11be35a1SLionel Sambuc 87*11be35a1SLionel Sambuc local dir="$(dirname "${atffile}")" 88*11be35a1SLionel Sambuc 89*11be35a1SLionel Sambuc local subdirs= 90*11be35a1SLionel Sambuc local test_programs= 91*11be35a1SLionel Sambuc for file in $(extract_files "${atffile}"); do 92*11be35a1SLionel Sambuc if test -f "${dir}/${file}/Atffile"; then 93*11be35a1SLionel Sambuc subdirs="${subdirs} ${file}" 94*11be35a1SLionel Sambuc elif test -x "${dir}/${file}"; then 95*11be35a1SLionel Sambuc test_programs="${test_programs} ${file}" 96*11be35a1SLionel Sambuc fi 97*11be35a1SLionel Sambuc done 98*11be35a1SLionel Sambuc 99*11be35a1SLionel Sambuc mkdir -p "$(dirname "${kyuafile}")" 100*11be35a1SLionel Sambuc 101*11be35a1SLionel Sambuc echo "syntax('kyuafile', 1)" >"${kyuafile}" 102*11be35a1SLionel Sambuc echo >>"${kyuafile}" 103*11be35a1SLionel Sambuc echo "test_suite('${test_suite}')" >>"${kyuafile}" 104*11be35a1SLionel Sambuc if [ -n "${subdirs}" ]; then 105*11be35a1SLionel Sambuc echo >>"${kyuafile}" 106*11be35a1SLionel Sambuc for dir in ${subdirs}; do 107*11be35a1SLionel Sambuc echo "include('${dir}/Kyuafile')" >>"${kyuafile}" 108*11be35a1SLionel Sambuc done 109*11be35a1SLionel Sambuc fi 110*11be35a1SLionel Sambuc if [ -n "${test_programs}" ]; then 111*11be35a1SLionel Sambuc echo >>"${kyuafile}" 112*11be35a1SLionel Sambuc for tp in ${test_programs}; do 113*11be35a1SLionel Sambuc echo "atf_test_program{name='${tp}'}" >>"${kyuafile}" 114*11be35a1SLionel Sambuc done 115*11be35a1SLionel Sambuc fi 116*11be35a1SLionel Sambuc} 117*11be35a1SLionel Sambuc 118*11be35a1SLionel Sambuc 119*11be35a1SLionel Sambuc# Adds Kyuafiles to a test suite by converting any existing Atffiles. 120*11be35a1SLionel Sambuc# 121*11be35a1SLionel Sambuc# \param source_root The path to the existing test suite root. Must contain 122*11be35a1SLionel Sambuc# an Atffile and the test programs. 123*11be35a1SLionel Sambuc# \param target_root The path to the directory where the Kyuafiles will be 124*11be35a1SLionel Sambuc# written. The layout will mimic that of source_root. 125*11be35a1SLionel Sambucadd_kyuafiles() { 126*11be35a1SLionel Sambuc local source_root="${1}"; shift 127*11be35a1SLionel Sambuc local target_root="${1}"; shift 128*11be35a1SLionel Sambuc 129*11be35a1SLionel Sambuc for atffile in $(cd "${source_root}" && find . -name Atffile); do 130*11be35a1SLionel Sambuc local subdir="$(echo "${atffile}" | sed 's,Atffile$,,;s,^\./,,')" 131*11be35a1SLionel Sambuc convert_atffile "${source_root}/${subdir}Atffile" \ 132*11be35a1SLionel Sambuc "${target_root}/${subdir}Kyuafile" 133*11be35a1SLionel Sambuc done 134*11be35a1SLionel Sambuc} 135*11be35a1SLionel Sambuc 136*11be35a1SLionel Sambuc 137*11be35a1SLionel Sambuc# Prints program usage to stdout. 138*11be35a1SLionel Sambuc# 139*11be35a1SLionel Sambuc# \param progname The name of the program to use for the syntax help. 140*11be35a1SLionel Sambucusage() { 141*11be35a1SLionel Sambuc local progname="${1}"; shift 142*11be35a1SLionel Sambuc echo "Usage: ${progname} [-s source_root] [-t target_root]" 143*11be35a1SLionel Sambuc} 144*11be35a1SLionel Sambuc 145*11be35a1SLionel Sambuc 146*11be35a1SLionel Sambuc# Entry point for the program. 147*11be35a1SLionel Sambuc# 148*11be35a1SLionel Sambuc# \param ... The user-provided arguments. 149*11be35a1SLionel Sambucmain() { 150*11be35a1SLionel Sambuc local source_root= 151*11be35a1SLionel Sambuc local target_root= 152*11be35a1SLionel Sambuc 153*11be35a1SLionel Sambuc while getopts ':s:t:' arg "${@}"; do 154*11be35a1SLionel Sambuc case "${arg}" in 155*11be35a1SLionel Sambuc s) 156*11be35a1SLionel Sambuc source_root="${OPTARG}" 157*11be35a1SLionel Sambuc ;; 158*11be35a1SLionel Sambuc t) 159*11be35a1SLionel Sambuc target_root="${OPTARG}" 160*11be35a1SLionel Sambuc ;; 161*11be35a1SLionel Sambuc \?) 162*11be35a1SLionel Sambuc lib_usage_error "Unknown option -${OPTARG}" 163*11be35a1SLionel Sambuc ;; 164*11be35a1SLionel Sambuc esac 165*11be35a1SLionel Sambuc done 166*11be35a1SLionel Sambuc shift $((${OPTIND} - 1)) 167*11be35a1SLionel Sambuc 168*11be35a1SLionel Sambuc [ -n "${source_root}" ] || source_root=. 169*11be35a1SLionel Sambuc [ -n "${target_root}" ] || target_root="${source_root}" 170*11be35a1SLionel Sambuc 171*11be35a1SLionel Sambuc [ ${#} -eq 0 ] || lib_usage_error "No arguments allowed" 172*11be35a1SLionel Sambuc 173*11be35a1SLionel Sambuc [ -f "${source_root}/Atffile" ] || \ 174*11be35a1SLionel Sambuc lib_error "${source_root} is not a test suite; missing Atffile" 175*11be35a1SLionel Sambuc 176*11be35a1SLionel Sambuc remove_kyuafiles "${target_root}" 177*11be35a1SLionel Sambuc add_kyuafiles "${source_root}" "${target_root}" 178*11be35a1SLionel Sambuc} 179*11be35a1SLionel Sambuc 180*11be35a1SLionel Sambuc 181*11be35a1SLionel Sambucmain "${@}" 182