On Sat, 21 May 2016 14:49:41 +0100
aide...@gentoo.org wrote:

> From: Amadeusz Żołnowski <aide...@gentoo.org>
> 
> awk doesn't have the -i option like sed and if editing file in place is
> desired, additional steps are required. eawk uses tmp file to make it
> look to the caller editing happens in place.
> 
> New version of gawk (not stabilized yet) does support editing in place
> but forcing user to install specific awk implementation is not desired.
> ---
>  eclass/eutils.eclass        | 16 +++++++++++
>  eclass/tests/eutils_eawk.sh | 66 
> +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 82 insertions(+)
>  create mode 100755 eclass/tests/eutils_eawk.sh
> 
> diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass
> index dbedffe..963a692 100644
> --- a/eclass/eutils.eclass
> +++ b/eclass/eutils.eclass
> @@ -20,6 +20,22 @@ _EUTILS_ECLASS=1
>  
>  inherit multilib toolchain-funcs
>  
> +# @FUNCTION: eawk
> +# @USAGE: <file> <args>
> +# @DESCRIPTION:
> +# Edit file <file> in place with awk. Pass all arguments following <file> to
> +# awk.
> +eawk() {
> +     local f="$1"; shift
> +     local tmpf="$(emktemp)"
> +
> +     awk "$@" "${f}" >"${tmpf}" || die -n 'awk failed' || return

You can't use 'die -n' before EAPI 6. So you either need a conditional
there, or make the function available only in EAPI 6.

> +     # Following commands should always succeed unless something weird is 
> going
> +     # on.
> +     cat "${tmpf}" >"${f}" || die 'failed to replace source file' || return
> +     rm "${tmpf}" || die "failed to remove temporary file"

Any reason not to use mv here? Aside to making this simpler, it would
also prevent this from rewriting contents of hardlinked files.

> +}
> +
>  # @FUNCTION: eqawarn
>  # @USAGE: [message]
>  # @DESCRIPTION:
> diff --git a/eclass/tests/eutils_eawk.sh b/eclass/tests/eutils_eawk.sh
> new file mode 100755
> index 0000000..7e0d1d4
> --- /dev/null
> +++ b/eclass/tests/eutils_eawk.sh
> @@ -0,0 +1,66 @@
> +#!/bin/bash
> +# Copyright 1999-2016 Gentoo Foundation
> +# Distributed under the terms of the GNU General Public License v2
> +# $Id$
> +
> +source tests-common.sh
> +
> +inherit eutils
> +
> +# Mock die so it doesn't break tests.
> +die() {
> +     echo "die: $*" 1>&2
> +     return 1
> +}
> +
> +tbegin "preserves permissions"
> +
> +cd "${tmpdir}" || tend $?

This doesn't terminate tests. So you'll end up creating files
in the wrong directory.

> +
> +cat <<EOF >'test.txt'

Then, you aren't checking for failure here.

> +testme1
> +testme2
> +testme3
> +EOF
> +
> +cat <<EOF >'test_expected.txt'
> +testme1
> +foo
> +testme3
> +EOF
> +
> +chmod 704 'test.txt' || tend $?
> +eumask_push 000
> +eawk 'test.txt' '/^testme2$/ {print "foo"; next;} 1' || tend $?
> +eumask_pop
> +
> +diff 'test.txt' 'test_expected.txt'
> +expected=$?
> +
> +[[ $(stat -c '%a' 'test.txt') = 704 ]]
> +perms=$?
> +
> +[[ ${expected}${perms} = 00 ]]
> +
> +tend $?
> +
> +
> +tbegin "doesn't alter file on failure"
> +
> +cd "${tmpdir}" || tend $?
> +
> +cat <<EOF >'test.txt'
> +testme1
> +testme2
> +testme3
> +EOF
> +
> +cat 'test.txt' >'test_expected.txt'
> +
> +# eawk should file because of syntax error.
> +eawk 'test.txt' '/^testme2$/ print "foo"; next;} 1' 2>/dev/null && tend 1
> +diff 'test.txt' 'test_expected.txt'
> +
> +tend $?
> +
> +texit



-- 
Best regards,
Michał Górny
<http://dev.gentoo.org/~mgorny/>

Attachment: pgpiOnYMD5fAX.pgp
Description: OpenPGP digital signature

Reply via email to