I was looking at the commented out code in makepkg to unset all vars in PKGBUILD before including and I think I've cracked it. So I went to test it on arch's main, extra and unstable repo, and there's several PKGBUILDs with miss spelled vars. As a result I've created check-buildscript, this just runs some sanity/spell checks on a PKGBUILD script. People should run it before submitting a PKGBUILD. Andrew
From 1d77fdf087d37ed839c892484df26c471ed79f9a Mon Sep 17 00:00:00 2001 From: Andrew Fyfe <andrew@neptune-one.net> Date: Thu, 29 Mar 2007 16:24:59 +0100 Subject: [PATCH 1/1] Added check-buildscript
check-buildscript does some basic sanity checks on a build script. People should run check-buildscript before they submit a build script. Signed-off-by: Andrew Fyfe <andrew@neptune-one.net> --- scripts/Makefile.am | 3 +- scripts/check-buildscript | 261 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 263 insertions(+), 1 deletions(-) create mode 100755 scripts/check-buildscript diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 57baee4..68d9ff6 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -4,6 +4,7 @@ bin_SCRIPTS = \ abs \ + check-buildscript \ gensync \ makepkg \ makeworld \ @@ -15,4 +16,4 @@ bin_SCRIPTS = \ EXTRA_DIST = $(bin_SCRIPTS) -abs gensync makepkg makeworld pacman-optimize rankmirrors repo-add repo-remove re-pacman updatesync: +abs check-buildscript gensync makepkg makeworld pacman-optimize rankmirrors repo-add repo-remove re-pacman updatesync: diff --git a/scripts/check-buildscript b/scripts/check-buildscript new file mode 100755 index 0000000..fddd2bf --- /dev/null +++ b/scripts/check-buildscript @@ -0,0 +1,261 @@ +#!/bin/bash +# +# check-buildscript - checks build script for errors. +# +# Copyright (c) 2007 by Andrew Fyfe <andrew@neptune-one.net> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +# USA. +# + +# TODO: _PACMAN_VERSION needs to match version of pacman package. +# some autoconf foo is needed. +_PACMAN_VERSION='3.0.0' +_VERSION='1.0' +_BUG_REPORT_EMAIL='pacman-dev@archlinux.org' + +BUILDSCRIPT='PKGBUILD' + +set -e +trap 'echo -e "\033[1;31mBANG! \033[1;37mThat didnt sound good. \033[1;33m:(\033[1;0m" >&2' ERR + +_DEFAULT_VARS=(pkgname pkgver pkgrel pkgdesc arch url license depends + makedepends provides conflicts replaces backup install + options source noextract md5sums sha1sums groups) + +E_OK=0 +E_UNKNOWN=1 +E_BUILDSCRIPT_ERROR=2 +E_HAS_WARNINGS=4 + +_WARNINGS=0 + +# Source makepkg.conf; fail if it is not found +if [ -r /etc/makepkg.conf ]; then + source /etc/makepkg.conf +else + error "/etc/makepkg.conf not found. cannot continue" + exit 1 # $E_CONFIG_ERROR # TODO: error codes +fi + +# Source user-specific makepkg.conf overrides +[ -r ~/.makepkg.conf ] && source ~/.makepkg.conf + +plain() { + if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then + echo -e " \033[1;37m$1\033[1;0m" >&2 + else + echo " $1" >&2 + fi +} + +msg() { + if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then + echo -e "\033[1;32m==> \033[1;37m$1\033[1;0m" >&2 + else + echo "==> $1" >&2 + fi +} + +msg2() { + if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then + echo -e " \033[1;34m-> \033[1;37m$1\033[1;0m" >&2 + else + echo " -> $1" >&2 + fi +} + +warning() { + if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then + echo -e "\033[1;33m==> WARNING: \033[1;37m$1\033[1;0m" >&2 + else + echo "==> WARNING: $1" >&2 + fi +} + +error() { + if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then + echo -e "\033[1;31m==> ERROR: \033[1;37m$1\033[1;0m" >&2 + else + echo "==> ERROR: $1" >&2 + fi +} + +in_array() { + local needle=$1 + shift 1 + [ -z "$1" ] && return 1 + for i in $*; do + [ "$i" = "$needle" ] && return 0 + done + return 1 +} + +# check if option is present in BUILDENV +check_buildenv() { + local needle=$(echo $1 | tr [:upper:] [:lower:]) + local i + # use options from makepkg.conf + for i in ${BUILDENV[@]}; do + local lc=$(echo $i | tr [:upper:] [:lower:]) + if [ "$lc" = "$needle" ]; then + echo "y" + return + elif [ "$lc" = "!$needle" ]; then + echo "n" + return + fi + done + echo "unknown" + return +} + +usage () { + cat << EOF +check-buildscript version $_VERSION + +Usage: $0 [options] [build script] + +Options: + -m, --nocolor Disable colourized output messages. + + -h, --help Display this help then exit. + -V, --version Output version information then exit. + +If no build script is specified on the command line then +check-buildscript will check ./$BUILDSCRIPT + +Report bugs to <${_BUG_REPORT_EMAIL}> + +EOF +} + +version () { + echo "check-buildscript $_VERSION" + echo "pacman $_PACMAN_VERSION" +} + +check_hasBuild () { + msg2 "Checking for build() function..." + if ! grep -q ' *build *\(\)' $_BUILDSCRIPT; then + error "The build() function was not found." + plain "Please make sure you have a build() function in your build script." + exit $E_BUILDSCRIPT_ERROR + fi +} + +check_varNames () { + msg2 "Checking variable names..." + + local count=$(( \ + $(grep -n 'build *()' "$_BUILDSCRIPT" | cut -d : -f 1) )) + local vars="$(head -n $count "$_BUILDSCRIPT" | \ + grep '^[A-Za-z0-9\-\_]*=.*' | \ + sed 's/^\([A-Za-z0-9\-\_]*\)=.*/\1/')" + + [ -z "$vars" ] && return + + local var extra_vars + for var in $vars; do + ! in_array $var ${_DEFAULT_VARS[@]} && extra_vars="$extra_vars $var" + done + + if [ -n "$extra_vars" ]; then + _WARNINGS=$(($_WARNINGS + 1)) + warning "$_BUILDSCRIPT contains the following non standard variables:" + + for var in $extra_vars; do + msg2 "$var" + done + + plain "" + plain "Please make sure these aren't misspelled variables." + plain "If they aren't just ignore this warning." + plain "" + fi +} + +check_hasChecksums () { + msg2 "Checking for checksums..." + + if [ "$(source "$_BUILDSCRIPT"; \ + [ -z "$md5sums" -a -z "$sha1sums" ] || \ + [ "$md5sums" = "generate" -a -z "$sha1sums" ]; \ + echo $?)" -eq 0 ]; then + _WARNINGS=$(($_WARNINGS + 1)) + warning "$_BUILDSCRIPT contains no checksums for the source files." + plain "Unless this package gets its source from cvs/svn please run" + plain "makepkg -g >>PKGBUILD" + plain "" + fi +} + + +## Begin script... +_OPT_SHORT="mhV" +_OPT_LONG="nocolor,help,version" +_OPT_TEMP="$(getopt -o $_OPT_SHORT --long $_OPT_LONG -n "$0" -- "$@" || true)" +eval set -- "$_OPT_TEMP" +unset _OPT_SHORT _OPT_LONG _OPT_TEMP + +while true; do + case $1 in + -m|--nocolor) USE_COLOR='n';; + + -h|--help) usage; exit $E_OK;; + -V|--version) version; exit $E_OK;; + + --) OPTIND=0; shift; break;; + *) usage; exit $E_UNKNOWN;; + esac +done + +if [ $# -eq 0 ]; then + if [ ! -r "$BUILDSCRIPT" ]; then + usage + exit $E_UNKNOWN + fi + + _FILES=$BUILDSCRIPT +else + _FILES=$* +fi + +for _BUILDSCRIPT in $_FILES; do + if [ ! -r "$_BUILDSCRIPT" ]; then + error "Unable to find $_BUILDSCRIPT" + exit $E_UNKNOWN + fi + + msg "Checking ${_BUILDSCRIPT}..." + + check_hasBuild + check_varNames + check_hasChecksums +done + +plain "" +if [ $_WARNINGS -eq 0 ]; then + msg "Your build script(s) looks ok. \033[1;33m:)" + exit $E_OK +elif [ $_WARNINGS -eq 1 ]; then + warning "There might be a problem. There was 1 warning." + exit $E_HAS_WARNINGS +else + warning "There might be a problem. There were $_WARNINGS warnings." + exit $E_HAS_WARNINGS +fi + +# vim:set ts=4 noet: -- 1.5.0.6