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